implemented strategy pattern
This commit is contained in:
parent
3de32e8ec5
commit
8f5f035a6f
@ -14,6 +14,7 @@
|
|||||||
- [Composition](#composition)
|
- [Composition](#composition)
|
||||||
- [Behavioral patterns](#behavioral-patterns)
|
- [Behavioral patterns](#behavioral-patterns)
|
||||||
- [Chain of responsibility](#chain-of-responsibility)
|
- [Chain of responsibility](#chain-of-responsibility)
|
||||||
|
- [Strategy](#strategy)
|
||||||
- [Concurrency patterns](#concurrency-patterns)
|
- [Concurrency patterns](#concurrency-patterns)
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -44,10 +45,15 @@ between objects. Behavio ral patterns describe not just patterns of objects or c
|
|||||||
but also the patterns of communication between them. These patterns characterize
|
but also the patterns of communication between them. These patterns characterize
|
||||||
complex control flow that's difficult to follow at run-time. They shift your focus away
|
complex control flow that's difficult to follow at run-time. They shift your focus away
|
||||||
from flow of control to let you concent ratejust on the way objects are interconnected.
|
from flow of control to let you concent ratejust on the way objects are interconnected.
|
||||||
|
|
||||||
### Chain of responsibility
|
### Chain of responsibility
|
||||||
Avoid coupling the sender of a request to its receiver by giving more than one
|
Avoid coupling the sender of a request to its receiver by giving more than one
|
||||||
object a chance to handle the request. Chain the receiving objects and pass the
|
object a chance to handle the request. Chain the receiving objects and pass the
|
||||||
request along the chain until an object handles it.
|
request along the chain until an object handles it.
|
||||||
|
|
||||||
|
### Strategy
|
||||||
|
Define a family oafl gorit h ms, encapsulate each one, and make them interchangeable.
|
||||||
|
Strategy lets the algorithm vary independently from clients that use it.
|
||||||
|
|
||||||
## Concurrency patterns
|
## Concurrency patterns
|
||||||
Pattenrs for concurrent work and parallel execution in Go.
|
Pattenrs for concurrent work and parallel execution in Go.
|
||||||
|
34
behavioral/strategy/strategy.go
Normal file
34
behavioral/strategy/strategy.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package strategy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Abstraction
|
||||||
|
type LoggerStrategy interface {
|
||||||
|
Write(string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strategy 1
|
||||||
|
type FileLogger struct {}
|
||||||
|
func (l FileLogger) Write(data string) error {
|
||||||
|
fmt.Println("Writed to file...")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strategy 2
|
||||||
|
type DbLogger struct {}
|
||||||
|
func (l DbLogger) Write(data string) error {
|
||||||
|
fmt.Println("Writed to db..")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consumer
|
||||||
|
type Logger struct{
|
||||||
|
logger LoggerStrategy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Log(data string) error {
|
||||||
|
l.logger.Write(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
0
behavioral/strategy/strategy_test.go
Normal file
0
behavioral/strategy/strategy_test.go
Normal file
Reference in New Issue
Block a user