behavorial.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // behavorial
  2. package pattern
  3. import (
  4. "fmt"
  5. )
  6. // 13. Chain of Responsibility Pattern
  7. type Handler interface {
  8. SetNext(handler Handler)
  9. Handle(request string) string
  10. }
  11. type AbstractHandler struct {
  12. next Handler
  13. }
  14. func (h *AbstractHandler) SetNext(handler Handler) {
  15. h.next = handler
  16. }
  17. type ConcreteHandler1 struct {
  18. AbstractHandler
  19. }
  20. func (h *ConcreteHandler1) Handle(request string) string {
  21. if request == "one" {
  22. return "Handler1: handled"
  23. }
  24. if h.next != nil {
  25. return h.next.Handle(request)
  26. }
  27. return ""
  28. }
  29. // 14. Command Pattern
  30. type Command interface {
  31. Execute() string
  32. }
  33. type Light struct{}
  34. func (l *Light) TurnOn() string { return "Light is on" }
  35. func (l *Light) TurnOff() string { return "Light is off" }
  36. type LightOnCommand struct {
  37. light *Light
  38. }
  39. func (c *LightOnCommand) Execute() string {
  40. return c.light.TurnOn()
  41. }
  42. // 15. Interpreter Pattern
  43. type Expression interface {
  44. Interpret() bool
  45. }
  46. type TerminalExpression struct {
  47. data string
  48. }
  49. func (t *TerminalExpression) Interpret() bool {
  50. return len(t.data) > 0
  51. }
  52. // 16. Iterator Pattern
  53. type Iterator interface {
  54. HasNext() bool
  55. Next() interface{}
  56. }
  57. type Collection struct {
  58. items []interface{}
  59. }
  60. type CollectionIterator struct {
  61. collection *Collection
  62. index int
  63. }
  64. func (i *CollectionIterator) HasNext() bool {
  65. return i.index < len(i.collection.items)
  66. }
  67. func (i *CollectionIterator) Next() interface{} {
  68. if i.HasNext() {
  69. item := i.collection.items[i.index]
  70. i.index++
  71. return item
  72. }
  73. return nil
  74. }
  75. // 17. Mediator Pattern
  76. type Mediator interface {
  77. Notify(sender string, event string)
  78. }
  79. type ConcreteMediator struct {
  80. component1 *Component1
  81. component2 *Component2
  82. }
  83. type Component1 struct {
  84. mediator Mediator
  85. }
  86. type Component2 struct {
  87. mediator Mediator
  88. }
  89. func (m *ConcreteMediator) Notify(sender string, event string) {
  90. fmt.Printf("Mediator reacts on %s and triggers event: %s\n", sender, event)
  91. }
  92. // 18. Memento Pattern
  93. type Memento struct {
  94. state string
  95. }
  96. type Originator struct {
  97. state string
  98. }
  99. func (o *Originator) CreateMemento() *Memento {
  100. return &Memento{state: o.state}
  101. }
  102. func (o *Originator) RestoreMemento(m *Memento) {
  103. o.state = m.state
  104. }
  105. // 19. Observer Pattern
  106. type Observer interface {
  107. Update(string)
  108. }
  109. type Subject2 struct {
  110. observers []Observer
  111. state string
  112. }
  113. func (s *Subject2) Attach(o Observer) {
  114. s.observers = append(s.observers, o)
  115. }
  116. func (s *Subject2) Notify() {
  117. for _, observer := range s.observers {
  118. observer.Update(s.state)
  119. }
  120. }
  121. // 20. State Pattern
  122. type State interface {
  123. Handle() string
  124. }
  125. type Context struct {
  126. state State
  127. }
  128. type ConcreteStateA struct{}
  129. type ConcreteStateB struct{}
  130. func (s *ConcreteStateA) Handle() string { return "State A" }
  131. func (s *ConcreteStateB) Handle() string { return "State B" }
  132. // 21. Strategy Pattern
  133. type Strategy interface {
  134. Execute() string
  135. }
  136. type ConcreteStrategyA struct{}
  137. type ConcreteStrategyB struct{}
  138. func (s *ConcreteStrategyA) Execute() string { return "Strategy A" }
  139. func (s *ConcreteStrategyB) Execute() string { return "Strategy B" }
  140. // 22. Template Method Pattern
  141. type AbstractClass interface {
  142. TemplateMethod() string
  143. PrimitiveOperation1() string
  144. PrimitiveOperation2() string
  145. }
  146. type ConcreteClass struct {
  147. PrimitiveOperation2 func() string
  148. }
  149. type ConcreteOperation2 struct {
  150. //PrimitiveOperation2 func() string
  151. }
  152. func (c *ConcreteClass) TemplateMethod() string {
  153. return c.PrimitiveOperation1() + " " + c.PrimitiveOperation2()
  154. }
  155. func (c *ConcreteClass) PrimitiveOperation1() string { return "Step 1" }
  156. func (c *ConcreteOperation2) PrimitiveOperation2() string { return "Step 2" }
  157. // 23. Visitor Pattern
  158. type Visitor interface {
  159. VisitConcreteElementA(*ConcreteElementA)
  160. VisitConcreteElementB(*ConcreteElementB)
  161. }
  162. type Element interface {
  163. Accept(Visitor)
  164. }
  165. type ConcreteElementA struct{}
  166. type ConcreteElementB struct{}
  167. func (e *ConcreteElementA) Accept(v Visitor) { v.VisitConcreteElementA(e) }
  168. func (e *ConcreteElementB) Accept(v Visitor) { v.VisitConcreteElementB(e) }
  169. type ConcreteVisitor struct{}
  170. func (v *ConcreteVisitor) VisitConcreteElementA(e *ConcreteElementA) {
  171. fmt.Println("Visited ConcreteElementA")
  172. }
  173. func (v *ConcreteVisitor) VisitConcreteElementB(e *ConcreteElementB) {
  174. fmt.Println("Visited ConcreteElementB")
  175. }