structural.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // structural
  2. package pattern
  3. import (
  4. "fmt"
  5. )
  6. // 6. Adapter Pattern
  7. type LegacyPrinter interface {
  8. Print(s string) string
  9. }
  10. type MyLegacyPrinter struct{}
  11. func (p *MyLegacyPrinter) Print(s string) string {
  12. return fmt.Sprintf("Legacy: %s", s)
  13. }
  14. type ModernPrinter interface {
  15. PrintModern(s string) string
  16. }
  17. type PrinterAdapter struct {
  18. LegacyPrinter
  19. }
  20. func (p *PrinterAdapter) PrintModern(s string) string {
  21. return p.Print(s)
  22. }
  23. // 7. Bridge Pattern
  24. type DrawAPI interface {
  25. DrawCircle(x, y, radius int)
  26. }
  27. type RedCircle struct{}
  28. type GreenCircle struct{}
  29. func (r *RedCircle) DrawCircle(x, y, radius int) {
  30. fmt.Printf("Drawing red circle at (%d,%d)\n", x, y)
  31. }
  32. func (g *GreenCircle) DrawCircle(x, y, radius int) {
  33. fmt.Printf("Drawing green circle at (%d,%d)\n", x, y)
  34. }
  35. type Shape struct {
  36. drawAPI DrawAPI
  37. }
  38. type Circle struct {
  39. Shape
  40. x, y, radius int
  41. }
  42. // 8. Composite Pattern
  43. type Component interface {
  44. Operation() string
  45. }
  46. type Leaf struct {
  47. name string
  48. }
  49. type Composite struct {
  50. children []Component
  51. }
  52. func (l *Leaf) Operation() string {
  53. return l.name
  54. }
  55. func (c *Composite) Operation() string {
  56. result := "Branch("
  57. for _, child := range c.children {
  58. result += child.Operation() + " "
  59. }
  60. return result + ")"
  61. }
  62. // 9. Decorator Pattern
  63. type Coffee interface {
  64. Cost() int
  65. Description() string
  66. }
  67. type SimpleCoffee struct{}
  68. func (s *SimpleCoffee) Cost() int { return 5 }
  69. func (s *SimpleCoffee) Description() string { return "Simple coffee" }
  70. type MilkDecorator struct {
  71. coffee Coffee
  72. }
  73. func (m *MilkDecorator) Cost() int {
  74. return m.coffee.Cost() + 2
  75. }
  76. func (m *MilkDecorator) Description() string {
  77. return m.coffee.Description() + ", milk"
  78. }
  79. // 10. Facade Pattern
  80. type SubsystemA struct{}
  81. type SubsystemB struct{}
  82. type SubsystemC struct{}
  83. func (s *SubsystemA) OperationA() string { return "Subsystem A" }
  84. func (s *SubsystemB) OperationB() string { return "Subsystem B" }
  85. func (s *SubsystemC) OperationC() string { return "Subsystem C" }
  86. type Facade struct {
  87. sysA *SubsystemA
  88. sysB *SubsystemB
  89. sysC *SubsystemC
  90. }
  91. func (f *Facade) Operation() string {
  92. return f.sysA.OperationA() + " " + f.sysB.OperationB() + " " + f.sysC.OperationC()
  93. }
  94. // 11. Flyweight Pattern
  95. type CharacterFlyweight struct {
  96. char rune
  97. }
  98. type CharacterFactory struct {
  99. chars map[rune]*CharacterFlyweight
  100. }
  101. func NewCharacterFactory() *CharacterFactory {
  102. return &CharacterFactory{chars: make(map[rune]*CharacterFlyweight)}
  103. }
  104. func (f *CharacterFactory) GetCharacter(c rune) *CharacterFlyweight {
  105. if f.chars[c] == nil {
  106. f.chars[c] = &CharacterFlyweight{char: c}
  107. }
  108. return f.chars[c]
  109. }
  110. // 12. Proxy Pattern
  111. type Subject interface {
  112. Request() string
  113. }
  114. type RealSubject struct{}
  115. func (s *RealSubject) Request() string {
  116. return "RealSubject: Handling request"
  117. }
  118. type Proxy struct {
  119. realSubject *RealSubject
  120. }
  121. func (p *Proxy) Request() string {
  122. if p.realSubject == nil {
  123. p.realSubject = &RealSubject{}
  124. }
  125. return "Proxy: " + p.realSubject.Request()
  126. }