From 6c3aa8e9482a0bf6c076e2fa39504a0050759ae5 Mon Sep 17 00:00:00 2001 From: crocxdued Date: Sun, 11 Jan 2026 17:00:28 +0300 Subject: [PATCH 1/3] add README.md --- Michailov/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Michailov/README.md diff --git a/Michailov/README.md b/Michailov/README.md new file mode 100644 index 0000000..2fb36d8 --- /dev/null +++ b/Michailov/README.md @@ -0,0 +1 @@ +# Hello, git! From e2a5e4a3b6bd945859cee51f8ede1efcef226414 Mon Sep 17 00:00:00 2001 From: crocxdued Date: Sun, 11 Jan 2026 20:24:32 +0300 Subject: [PATCH 2/3] fix: implement creational patterns for lab 3 --- lab_2_ci_cd | 1 + lab_3_patterns/abstract_factory.go | 35 ++++++++++++++++++++++++++++++ lab_3_patterns/builder.go | 17 +++++++++++++++ lab_3_patterns/factory_method.go | 20 +++++++++++++++++ lab_3_patterns/go.mod | 3 +++ lab_3_patterns/main.go | 23 ++++++++++++++++++++ lab_3_patterns/singleton.go | 25 +++++++++++++++++++++ 7 files changed, 124 insertions(+) create mode 160000 lab_2_ci_cd create mode 100644 lab_3_patterns/abstract_factory.go create mode 100644 lab_3_patterns/builder.go create mode 100644 lab_3_patterns/factory_method.go create mode 100644 lab_3_patterns/go.mod create mode 100644 lab_3_patterns/main.go create mode 100644 lab_3_patterns/singleton.go diff --git a/lab_2_ci_cd b/lab_2_ci_cd new file mode 160000 index 0000000..9609aa6 --- /dev/null +++ b/lab_2_ci_cd @@ -0,0 +1 @@ +Subproject commit 9609aa68384875a97e52d0595a02aa222e66db64 diff --git a/lab_3_patterns/abstract_factory.go b/lab_3_patterns/abstract_factory.go new file mode 100644 index 0000000..5b16cd9 --- /dev/null +++ b/lab_3_patterns/abstract_factory.go @@ -0,0 +1,35 @@ +package main + +type Chair interface{ SitOn() string } +type Sofa interface{ LieOn() string } + +type FurnitureFactory interface { + CreateChair() Chair + CreateSofa() Sofa +} + +type ModernChair struct{} + +func (c ModernChair) SitOn() string { return "Sitting on a modern chair" } + +type ModernSofa struct{} + +func (s ModernSofa) LieOn() string { return "Lying on a modern sofa" } + +type ModernFactory struct{} + +func (f ModernFactory) CreateChair() Chair { return ModernChair{} } +func (f ModernFactory) CreateSofa() Sofa { return ModernSofa{} } + +type VictorianChair struct{} + +func (c VictorianChair) SitOn() string { return "Sitting on a victorian chair" } + +type VictorianSofa struct{} + +func (s VictorianSofa) LieOn() string { return "Lying on a victorian sofa" } + +type VictorianFactory struct{} + +func (f VictorianFactory) CreateChair() Chair { return VictorianChair{} } +func (f VictorianFactory) CreateSofa() Sofa { return VictorianSofa{} } diff --git a/lab_3_patterns/builder.go b/lab_3_patterns/builder.go new file mode 100644 index 0000000..11357e6 --- /dev/null +++ b/lab_3_patterns/builder.go @@ -0,0 +1,17 @@ +package main + +type House struct { + HasPool bool + HasGarage bool + Floors int +} + +type HouseBuilder struct { + house House +} + +func NewHouseBuilder() *HouseBuilder { return &HouseBuilder{} } +func (b *HouseBuilder) SetPool() *HouseBuilder { b.house.HasPool = true; return b } +func (b *HouseBuilder) SetGarage() *HouseBuilder { b.house.HasGarage = true; return b } +func (b *HouseBuilder) SetFloors(n int) *HouseBuilder { b.house.Floors = n; return b } +func (b *HouseBuilder) Build() House { return b.house } diff --git a/lab_3_patterns/factory_method.go b/lab_3_patterns/factory_method.go new file mode 100644 index 0000000..e8c7f40 --- /dev/null +++ b/lab_3_patterns/factory_method.go @@ -0,0 +1,20 @@ +package main + +type Button interface { + Render() string +} + +type HTMLButton struct{} + +func (b HTMLButton) Render() string { return "" } + +type WindowsButton struct{} + +func (b WindowsButton) Render() string { return "[Windows Button]" } + +func CreateButton(osType string) Button { + if osType == "windows" { + return WindowsButton{} + } + return HTMLButton{} +} diff --git a/lab_3_patterns/go.mod b/lab_3_patterns/go.mod new file mode 100644 index 0000000..409bfa4 --- /dev/null +++ b/lab_3_patterns/go.mod @@ -0,0 +1,3 @@ +module lab_3 + +go 1.24.1 diff --git a/lab_3_patterns/main.go b/lab_3_patterns/main.go new file mode 100644 index 0000000..3b0686f --- /dev/null +++ b/lab_3_patterns/main.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +func main() { + fmt.Println("--- 1. Singleton ---") + l1, l2 := GetLogger(), GetLogger() + l1.AddLog("System start") + l2.AddLog("User login") + fmt.Printf("Is same instance? %v\n\n", l1 == l2) + + fmt.Println("--- 2. Factory Method ---") + fmt.Println(CreateButton("windows").Render()) + fmt.Println(CreateButton("web").Render() + "\n") + + fmt.Println("--- 3. Abstract Factory ---") + var factory FurnitureFactory = ModernFactory{} + fmt.Println(factory.CreateChair().SitOn() + "\n") + + fmt.Println("--- 4. Builder ---") + house := NewHouseBuilder().SetFloors(2).SetPool().Build() + fmt.Printf("House: Floors: %d, Pool: %v\n", house.Floors, house.HasPool) +} diff --git a/lab_3_patterns/singleton.go b/lab_3_patterns/singleton.go new file mode 100644 index 0000000..c423f27 --- /dev/null +++ b/lab_3_patterns/singleton.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "sync" +) + +type Logger struct { + messages []string +} + +var instance *Logger +var once sync.Once + +func GetLogger() *Logger { + once.Do(func() { + instance = &Logger{messages: []string{}} + }) + return instance +} + +func (l *Logger) AddLog(msg string) { + l.messages = append(l.messages, msg) + fmt.Printf("Add log: %s\n", msg) +} From 10da02bce2a8f68df4c9c0b7f89a453ff828791b Mon Sep 17 00:00:00 2001 From: crocxdued Date: Sun, 11 Jan 2026 21:01:31 +0300 Subject: [PATCH 3/3] fix: implement strategy, chain, iterator, proxy, bridge, and adapter for lab 4 --- lab_4_patterns/Chain of Responsibility.go | 27 +++++++++++++++++++ lab_4_patterns/Iterator.go | 26 ++++++++++++++++++ lab_4_patterns/adapter.go | 17 ++++++++++++ lab_4_patterns/bridge.go | 26 ++++++++++++++++++ lab_4_patterns/go.mod | 3 +++ lab_4_patterns/main.go | 32 +++++++++++++++++++++++ lab_4_patterns/proxy.go | 24 +++++++++++++++++ lab_4_patterns/strategy.go | 22 ++++++++++++++++ 8 files changed, 177 insertions(+) create mode 100644 lab_4_patterns/Chain of Responsibility.go create mode 100644 lab_4_patterns/Iterator.go create mode 100644 lab_4_patterns/adapter.go create mode 100644 lab_4_patterns/bridge.go create mode 100644 lab_4_patterns/go.mod create mode 100644 lab_4_patterns/main.go create mode 100644 lab_4_patterns/proxy.go create mode 100644 lab_4_patterns/strategy.go diff --git a/lab_4_patterns/Chain of Responsibility.go b/lab_4_patterns/Chain of Responsibility.go new file mode 100644 index 0000000..310d87d --- /dev/null +++ b/lab_4_patterns/Chain of Responsibility.go @@ -0,0 +1,27 @@ +package main + +type Handler interface { + Handle(request string) string + SetNext(handler Handler) Handler +} + +type BaseHandler struct { + next Handler +} + +func (b *BaseHandler) SetNext(h Handler) Handler { + b.next = h + return h +} + +type TechnicalHandler struct{ BaseHandler } + +func (h *TechnicalHandler) Handle(req string) string { + if req == "technical" { + return "Technical support handled it" + } + if h.next != nil { + return h.next.Handle(req) + } + return "" +} diff --git a/lab_4_patterns/Iterator.go b/lab_4_patterns/Iterator.go new file mode 100644 index 0000000..a42b22a --- /dev/null +++ b/lab_4_patterns/Iterator.go @@ -0,0 +1,26 @@ +package main + +type Iterator interface { + HasNext() bool + Next() string +} + +type NameCollection struct { + names []string +} + +func (c *NameCollection) GetIterator() Iterator { + return &NameIterator{names: c.names, index: 0} +} + +type NameIterator struct { + names []string + index int +} + +func (i *NameIterator) HasNext() bool { return i.index < len(i.names) } +func (i *NameIterator) Next() string { + name := i.names[i.index] + i.index++ + return name +} diff --git a/lab_4_patterns/adapter.go b/lab_4_patterns/adapter.go new file mode 100644 index 0000000..b0d731d --- /dev/null +++ b/lab_4_patterns/adapter.go @@ -0,0 +1,17 @@ +package main + +type USPlug interface { + Connect110v() string +} + +type EuroSocket struct{} + +func (s *EuroSocket) Get220v() string { return "220v" } + +type PowerAdapter struct { + socket *EuroSocket +} + +func (a *PowerAdapter) Connect110v() string { + return a.socket.Get220v() + " converted to 110v" +} diff --git a/lab_4_patterns/bridge.go b/lab_4_patterns/bridge.go new file mode 100644 index 0000000..0b6956c --- /dev/null +++ b/lab_4_patterns/bridge.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +type Device interface { + IsEnabled() bool + Enable() +} + +type Radio struct{ on bool } + +func (r *Radio) IsEnabled() bool { return r.on } +func (r *Radio) Enable() { r.on = true } + +type Remote struct { + device Device +} + +func (r *Remote) TogglePower() { + if r.device.IsEnabled() { + fmt.Println("Turning off") + } else { + r.device.Enable() + fmt.Println("Turning on") + } +} diff --git a/lab_4_patterns/go.mod b/lab_4_patterns/go.mod new file mode 100644 index 0000000..42a3746 --- /dev/null +++ b/lab_4_patterns/go.mod @@ -0,0 +1,3 @@ +module lab_4 + +go 1.24.1 diff --git a/lab_4_patterns/main.go b/lab_4_patterns/main.go new file mode 100644 index 0000000..a04a796 --- /dev/null +++ b/lab_4_patterns/main.go @@ -0,0 +1,32 @@ +package main + +import "fmt" + +func main() { + fmt.Println("--- Strategy ---") + cart := &ShoppingCart{strategy: &PayPal{}} + cart.Checkout(500) + + fmt.Println("\n--- Chain of Responsibility ---") + h1 := &TechnicalHandler{} + fmt.Println(h1.Handle("technical")) + + fmt.Println("\n--- Iterator ---") + coll := &NameCollection{names: []string{"Andrew", "Nemanja", "Ivan"}} + it := coll.GetIterator() + for it.HasNext() { + fmt.Println(it.Next()) + } + + fmt.Println("\n--- Proxy ---") + dbProxy := &DatabaseProxy{role: "user"} + fmt.Println(dbProxy.Query()) + + fmt.Println("\n--- Bridge ---") + remote := &Remote{device: &Radio{}} + remote.TogglePower() + + fmt.Println("\n--- Adapter ---") + adapter := &PowerAdapter{socket: &EuroSocket{}} + fmt.Println(adapter.Connect110v()) +} diff --git a/lab_4_patterns/proxy.go b/lab_4_patterns/proxy.go new file mode 100644 index 0000000..eca89f8 --- /dev/null +++ b/lab_4_patterns/proxy.go @@ -0,0 +1,24 @@ +package main + +type Database interface { + Query() string +} + +type RealDatabase struct{} + +func (r *RealDatabase) Query() string { return "Data from Real DB" } + +type DatabaseProxy struct { + realDB *RealDatabase + role string +} + +func (p *DatabaseProxy) Query() string { + if p.role != "admin" { + return "Access Denied" + } + if p.realDB == nil { + p.realDB = &RealDatabase{} + } + return "Proxy: " + p.realDB.Query() +} diff --git a/lab_4_patterns/strategy.go b/lab_4_patterns/strategy.go new file mode 100644 index 0000000..5a02c7a --- /dev/null +++ b/lab_4_patterns/strategy.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +type PaymentStrategy interface { + Pay(amount int) string +} + +type CreditCard struct{} + +func (c CreditCard) Pay(a int) string { return fmt.Sprintf("Paid %d using Credit Card", a) } + +type PayPal struct{} + +func (p PayPal) Pay(a int) string { return fmt.Sprintf("Paid %d using PayPal", a) } + +type ShoppingCart struct { + strategy PaymentStrategy +} + +func (s *ShoppingCart) SetStrategy(st PaymentStrategy) { s.strategy = st } +func (s *ShoppingCart) Checkout(a int) string { return s.strategy.Pay(a) }