123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- // creational
- package pattern
- import (
- "fmt"
- "sync"
- )
- // 1. Singleton Pattern
- type singleton struct {
- data string
- }
- var (
- instance *singleton
- once sync.Once
- )
- func GetInstance() *singleton {
- once.Do(func() {
- instance = &singleton{data: "I am singleton"}
- })
- return instance
- }
- // 2. Factory Method Pattern
- type Animal interface {
- Speak() string
- }
- type Dog struct{}
- type Cat struct{}
- func (d *Dog) Speak() string { return "Woof!" }
- func (c *Cat) Speak() string { return "Meow!" }
- func CreateAnimal(animalType string) Animal {
- switch animalType {
- case "dog":
- return &Dog{}
- case "cat":
- return &Cat{}
- default:
- return nil
- }
- }
- // 3. Abstract Factory Pattern
- type Button interface {
- Paint()
- }
- type WinButton struct{}
- type MacButton struct{}
- func (w *WinButton) Paint() { fmt.Println("Windows button") }
- func (m *MacButton) Paint() { fmt.Println("Mac button") }
- type GUIFactory interface {
- CreateButton() Button
- }
- type WinFactory struct{}
- type MacFactory struct{}
- func (w *WinFactory) CreateButton() Button { return &WinButton{} }
- func (m *MacFactory) CreateButton() Button { return &MacButton{} }
- // 4. Builder Pattern
- type House struct {
- windows int
- doors int
- roof string
- }
- type HouseBuilder struct {
- house *House
- }
- func NewHouseBuilder() *HouseBuilder {
- return &HouseBuilder{house: &House{}}
- }
- func (b *HouseBuilder) SetWindows(count int) *HouseBuilder {
- b.house.windows = count
- return b
- }
- func (b *HouseBuilder) SetDoors(count int) *HouseBuilder {
- b.house.doors = count
- return b
- }
- func (b *HouseBuilder) SetRoof(style string) *HouseBuilder {
- b.house.roof = style
- return b
- }
- func (b *HouseBuilder) Build() *House {
- return b.house
- }
- // 5. Prototype Pattern
- type Prototype interface {
- Clone() Prototype
- }
- type ConcretePrototype struct {
- name string
- }
- func (p *ConcretePrototype) Clone() Prototype {
- return &ConcretePrototype{name: p.name}
- }
|