-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ChainResponsability | ||
|
||
type ComponentWithContextualHelp interface { | ||
ShowHelp() string | ||
} | ||
|
||
type Container struct { | ||
*Component | ||
children []ComponentWithContextualHelp | ||
} | ||
|
||
func (c *Container) add(child ComponentWithContextualHelp) { | ||
c.children = append(c.children, child) | ||
switch v := child.(type) { | ||
case *Component: | ||
v.container = c | ||
} | ||
} | ||
|
||
type Component struct { | ||
tooltipText string | ||
container *Container | ||
} | ||
|
||
func (c *Component) ShowHelp() string { | ||
if c.tooltipText != "" { | ||
return c.tooltipText | ||
} | ||
return c.container.ShowHelp() | ||
} | ||
|
||
type Dialog struct { | ||
Container | ||
wikiPageUrl string | ||
} | ||
|
||
func NewDialog(wikiPageUrl string) *Dialog { | ||
return &Dialog{wikiPageUrl: wikiPageUrl} | ||
} | ||
func (d *Dialog) ShowHelp() string { | ||
return d.Component.ShowHelp() | ||
} | ||
|
||
type Panel struct { | ||
Container | ||
modalHelpText string | ||
x, y, z, w int | ||
} | ||
|
||
func NewPanel(modalHelpText string, x, y, z, w int) *Panel { | ||
return &Panel{modalHelpText: modalHelpText, x: x, y: y, z: z, w: w} | ||
} | ||
|
||
func (p *Panel) ShowHelp() string { | ||
return p.modalHelpText | ||
} | ||
|
||
func buildPanel() *Panel { | ||
dialog := NewDialog("https://www.wikipedia.com") | ||
panel := NewPanel("Panel Help", 0, 0, 400, 800) | ||
okButton := Component{tooltipText: "This is an OK button that..."} | ||
cancelButton := Component{tooltipText: "Cancel"} | ||
|
||
panel.add(&okButton) | ||
panel.add(&cancelButton) | ||
panel.add(dialog) | ||
return panel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ChainResponsability | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestExample(t *testing.T) { | ||
result := buildPanel() | ||
result.ShowHelp() | ||
|
||
assert.Equal(t, "Panel Help", result.ShowHelp()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Chain Responsibility Pattern | ||
|
||
![](chain-of-responsability.png) | ||
|
||
|
||
1. The Handler declares the interface, common for all concrete handlers. It usually contains just a single method for handling requests, but sometimes it may also have another method for setting the next handler on the chain. | ||
2. The Base Handler is an optional class where you can put the boilerplate code that’s common to all handler classes. Usually, this class defines a field for storing a reference to the next handler. The clients can build a chain by passing a han- dler to the constructor or setter of the previous handler. The class may also implement the default handling behavior: it can pass execution to the next handler after checking for its existence. | ||
3. Concrete Handlers contain the actual code for processing requests. Upon receiving a request, each handler must decide whether to process it and, additionally, whether to pass it along the chain. Handlers are usually self-contained and immutable, accepting all necessary data just once via the constructor. | ||
4. The Client may compose chains just once or compose them dynamically, depending on the application’s logic. Note that a request can be sent to any handler in the chain—it doesn’t have to be the first one. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.