1
0
Fork 0
mirror of https://github.com/tmrts/go-patterns.git synced 2025-04-02 20:56:12 +03:00
This commit is contained in:
Bruno Henrique Gusmão Vasconcelos 2020-09-03 02:45:26 +00:00 committed by GitHub
commit 9643439e97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 1 deletions

View file

@ -36,7 +36,7 @@ A curated collection of idiomatic design & application patterns for Go language.
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
| [Chain of Responsibility](/behavioral/chain_of_responsibility.md) | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | |
| [Chain of Responsibility](/behavioral/chain_of_responsibility.md) | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | |
| [Command](/behavioral/command.md) | Bundles a command and arguments to call later | ✘ |
| [Mediator](/behavioral/mediator.md) | Connects objects and acts as a proxy | ✘ |
| [Memento](/behavioral/memento.md) | Generate an opaque token that can be used to go back to a previous state | ✘ |

View file

@ -0,0 +1,12 @@
# Observer Pattern
The [chain of responsibility pattern](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern) is a design pattern consisting in a series of independent processes that will run in in sequence. It gives power to rearange the sequence or add new processes at the end of it without changing its structure.
## Implementation
Each process in the chain must implement a function to be executed and call the next process at the end.
It will call each process recursivelly until there is no more porcesses in the chain of responsibility.
## Usage
For usage, see [chain_of_responsibility/main.go](chain_of_responsibility/main.go) or [view in the Playground](https://play.golang.org/p/XA2v0XlenAi).

View file

@ -0,0 +1,91 @@
package main
import "fmt"
type task interface {
execute()
setNext(task)
}
type turnOnLights struct {
next task
}
func (turnOnLights *turnOnLights) execute() {
fmt.Println("Turning the lights on...")
if turnOnLights.next != nil {
turnOnLights.next.execute()
}
}
func (turnOnLights *turnOnLights) setNext(next task) {
turnOnLights.next = next
}
type turnOnComputer struct {
next task
}
func (turnOnComputer *turnOnComputer) execute() {
fmt.Println("Turning the computer on...")
if turnOnComputer.next != nil {
turnOnComputer.next.execute()
}
}
func (turnOnComputer *turnOnComputer) setNext(next task) {
turnOnComputer.next = next
}
type openCodeEditor struct {
next task
}
func (openCodeEditor *openCodeEditor) execute() {
fmt.Println("Opening the code editor...")
if openCodeEditor.next != nil {
openCodeEditor.next.execute()
}
}
func (openCodeEditor *openCodeEditor) setNext(next task) {
openCodeEditor.next = next
}
type code struct {
next task
}
func (code *code) execute() {
fmt.Println("Start coding in go...")
if code.next != nil {
code.next.execute()
}
}
func (code *code) setNext(next task) {
code.next = next
}
func main() {
turnOnLights := &turnOnLights{}
turnOnComputer := &turnOnComputer{}
openCodeEditor := &openCodeEditor{}
code := &code{}
turnOnLights.setNext(turnOnComputer)
turnOnComputer.setNext(openCodeEditor)
openCodeEditor.setNext(code)
turnOnLights.execute()
// Out:
// Turning the lights on...
// Turning the computer on...
// Opening the code editor...
// Start coding in go...
openCodeEditor.execute()
// Out:
// Opening the code editor...
// Start coding in go...
}