behavioral.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // behavioral
  2. package pattern
  3. import "fmt"
  4. // 13. Chain of Responsibility
  5. type Handler interface {
  6. SetNext(handler Handler)
  7. Handle(request string) string
  8. }
  9. type BaseHandler struct {
  10. next Handler
  11. }
  12. func (b *BaseHandler) SetNext(h Handler) { b.next = h }
  13. type ConcreteHandler1 struct{ BaseHandler }
  14. func (h *ConcreteHandler1) Handle(req string) string {
  15. if req == "one" { return "Handled by 1" }
  16. if h.next != nil { return h.next.Handle(req) }
  17. return "Not handled"
  18. }
  19. type ConcreteHandler2 struct{ BaseHandler }
  20. func (h *ConcreteHandler2) Handle(req string) string {
  21. if req == "two" { return "Handled by 2" }
  22. if h.next != nil { return h.next.Handle(req) }
  23. return "Not handled"
  24. }
  25. // 14. Command
  26. type Command interface { Execute() string }
  27. type Light struct{}
  28. func (l *Light) On() string { return "Light On" }
  29. type LightOnCommand struct { Light *Light }
  30. func (c *LightOnCommand) Execute() string { return c.Light.On() }
  31. // 15. Interpreter
  32. type Expression interface { Interpret() bool }
  33. type TerminalExpression struct { Data string }
  34. func (t *TerminalExpression) Interpret() bool { return len(t.Data) > 0 }
  35. // 16. Iterator
  36. type Iterator interface {
  37. HasNext() bool
  38. Next() interface{}
  39. }
  40. type Collection struct { Items []interface{} }
  41. func (c *Collection) CreateIterator() Iterator {
  42. return &CollectionIterator{collection: c}
  43. }
  44. type CollectionIterator struct {
  45. collection *Collection
  46. index int
  47. }
  48. func (i *CollectionIterator) HasNext() bool { return i.index < len(i.collection.Items) }
  49. func (i *CollectionIterator) Next() interface{} {
  50. item := i.collection.Items[i.index]
  51. i.index++
  52. return item
  53. }
  54. // 17. Mediator
  55. type Mediator interface { Notify(sender interface{}, event string) }
  56. type ConcreteMediator struct {
  57. c1 *Component1
  58. }
  59. func (m *ConcreteMediator) SetComponent1(c *Component1) { m.c1 = c }
  60. func (m *ConcreteMediator) Notify(s interface{}, e string) {
  61. fmt.Printf("Mediator: reacting to %s\n", e)
  62. }
  63. type Component1 struct { Mediator Mediator }
  64. func (c *Component1) Trigger() { c.Mediator.Notify(c, "click") }
  65. // 18. Memento
  66. type Memento struct { State string }
  67. type Originator struct { State string }
  68. func (o *Originator) CreateMemento() *Memento { return &Memento{State: o.State} }
  69. func (o *Originator) RestoreMemento(m *Memento) { o.State = m.State }
  70. // 19. Observer
  71. type Observer interface { Update(string) }
  72. type Subject2 struct {
  73. observers []Observer
  74. state string
  75. }
  76. func (s *Subject2) Attach(o Observer) { s.observers = append(s.observers, o) }
  77. func (s *Subject2) SetState(st string) {
  78. s.state = st
  79. for _, o := range s.observers { o.Update(st) }
  80. }
  81. type ConcreteObserver struct{ ID int }
  82. func (o *ConcreteObserver) Update(s string) { fmt.Printf("Observer %d: state is %s\n", o.ID, s) }
  83. // 20. State
  84. type State interface { Handle() string }
  85. type Context struct { State State }
  86. func (c *Context) Request() string { return c.State.Handle() }
  87. type ConcreteStateA struct{}
  88. func (s *ConcreteStateA) Handle() string { return "State A" }
  89. // 21. Strategy
  90. type Strategy interface { Execute() string }
  91. type ConcreteStrategyA struct{}
  92. func (s *ConcreteStrategyA) Execute() string { return "Strategy A" }
  93. type ContextStrat struct { Strategy Strategy }
  94. func (c *ContextStrat) Execute() string { return c.Strategy.Execute() }
  95. // 22. Template Method (Idiomatique Go)
  96. type WorkerInterface interface {
  97. Work() string
  98. }
  99. type TemplateWorker struct {
  100. provider WorkerInterface
  101. }
  102. func NewWorker(p WorkerInterface) *TemplateWorker { return &TemplateWorker{provider: p} }
  103. func (t *TemplateWorker) Execute() string {
  104. return "Prep -> " + t.provider.Work() + " -> Clean"
  105. }
  106. type ConcreteWorker struct{}
  107. func (w *ConcreteWorker) Work() string { return "Specific Task" }
  108. // 23. Visitor
  109. type Visitor interface {
  110. VisitA(*ConcreteElementA)
  111. }
  112. type Element interface { Accept(Visitor) }
  113. type ConcreteElementA struct{}
  114. func (e *ConcreteElementA) Accept(v Visitor) { v.VisitA(e) }
  115. type ConcreteVisitor struct{}
  116. func (v *ConcreteVisitor) VisitA(e *ConcreteElementA) { fmt.Println("Visited Element A") }