Skip to content

Commit

Permalink
chain responsability pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ferminhg committed Dec 25, 2023
1 parent 88603c0 commit c294aa8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@ Notes:

### List of Design Patterns:

- SOLID Principles
- [SOLID Principles](patterns/solid/solid.srp.go)

## Creational Patterns
- [Factory](patterns/factory/structure.go)
- Abstract Factory
- Builder
- Factory
- Prototype
- Singleton


## Structural Patterns

- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Chain of Responsibility

## Behavioral Patterns

- [Chain of Responsibility](patterns/ChainResponsability/README.md)
- Command
- Interpreter
- Iterator
Expand Down
68 changes: 68 additions & 0 deletions patterns/ChainResponsability/GUIElements.go
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
}
13 changes: 13 additions & 0 deletions patterns/ChainResponsability/GUIElements_test.go
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())
}
9 changes: 9 additions & 0 deletions patterns/ChainResponsability/README.md
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.

0 comments on commit c294aa8

Please sign in to comment.