Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bridge Pattern 🎉 #7

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package patterns

import (
"fmt"
)

// RequesterAPI interface
type RequesterAPI interface {
Request(string) error
}

// ReactService struct
type ReactService struct{}

// Request method on ReactService
func (s *ReactService) Request(msg string) error {
fmt.Printf("ReactService: %s\n", msg)
return nil
}

// VueService structy
type VueService struct {
}

// Request method on VueService
func (s *VueService) Request(msg string) error {
fmt.Printf("VueService: %s\n", msg)
return nil
}

// AngularService structy
type AngularService struct {
}

// Request method on AngularService
func (s *AngularService) Request(msg string) error {
fmt.Printf("AngularService: %s\n", msg)
return nil
}

// RequesterAbstraction interface
type RequesterAbstraction interface {
CallAPI() error
}

// HTTPRequest struct
type HTTPRequest struct {
URL string
Requester RequesterAPI
}

// CallAPI method on HTTPRequest struct
func (c *HTTPRequest) CallAPI() error {
c.Requester.Request(c.URL)
return nil
}
26 changes: 26 additions & 0 deletions bridge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package patterns

func ExampleHTTPRequest() {
httpRequest := &HTTPRequest{
URL: "URL",
Requester: &ReactService{},
}
httpRequest.CallAPI()

httpRequest = &HTTPRequest{
URL: "URL",
Requester: &VueService{},
}
httpRequest.CallAPI()

httpRequest = &HTTPRequest{
URL: "URL",
Requester: &AngularService{},
}
httpRequest.CallAPI()

// Output:
// ReactService: URL
// VueService: URL
// AngularService: URL
}