Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Michailov/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello, git!
1 change: 1 addition & 0 deletions lab_2_ci_cd
Submodule lab_2_ci_cd added at 9609aa
35 changes: 35 additions & 0 deletions lab_3_patterns/abstract_factory.go
Original file line number Diff line number Diff line change
@@ -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{} }
17 changes: 17 additions & 0 deletions lab_3_patterns/builder.go
Original file line number Diff line number Diff line change
@@ -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 }
20 changes: 20 additions & 0 deletions lab_3_patterns/factory_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

type Button interface {
Render() string
}

type HTMLButton struct{}

func (b HTMLButton) Render() string { return "<button>HTML Button</button>" }

type WindowsButton struct{}

func (b WindowsButton) Render() string { return "[Windows Button]" }

func CreateButton(osType string) Button {
if osType == "windows" {
return WindowsButton{}
}
return HTMLButton{}
}
3 changes: 3 additions & 0 deletions lab_3_patterns/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module lab_3

go 1.24.1
23 changes: 23 additions & 0 deletions lab_3_patterns/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions lab_3_patterns/singleton.go
Original file line number Diff line number Diff line change
@@ -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)
}
27 changes: 27 additions & 0 deletions lab_4_patterns/Chain of Responsibility.go
Original file line number Diff line number Diff line change
@@ -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 ""
}
26 changes: 26 additions & 0 deletions lab_4_patterns/Iterator.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions lab_4_patterns/adapter.go
Original file line number Diff line number Diff line change
@@ -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"
}
26 changes: 26 additions & 0 deletions lab_4_patterns/bridge.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
3 changes: 3 additions & 0 deletions lab_4_patterns/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module lab_4

go 1.24.1
32 changes: 32 additions & 0 deletions lab_4_patterns/main.go
Original file line number Diff line number Diff line change
@@ -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())
}
24 changes: 24 additions & 0 deletions lab_4_patterns/proxy.go
Original file line number Diff line number Diff line change
@@ -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()
}
22 changes: 22 additions & 0 deletions lab_4_patterns/strategy.go
Original file line number Diff line number Diff line change
@@ -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) }