1
0
Fork 0

Implemented the design-pattern exercise

main
Oliver Stolle 2026-04-13 12:13:51 +00:00
parent 625a2da5fe
commit 380ffe1ba6
1 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,82 @@
package main
import "fmt"
//Implementation of the factory design pattern:
type Transporter interface {
transport()
load()
unload()
}
type Truck struct {
name string
}
type Ship struct {
name string
}
var _ Transporter = Truck{} //Ensures that the "Transporter" Interface is implemented.
var _ Transporter = Ship{} //Ensures that the "Transporter" Interface is implemented.
func (t Truck) transport() {
fmt.Println("Transporting via ", t.name)
}
func (t Truck) load() {
fmt.Println("Loading cago on ", t.name)
}
func (t Truck) unload() {
fmt.Println("Unloading cargo from ", t.name)
}
func (t Ship) transport() {
fmt.Println("Transporting via ", t.name)
}
func (t Ship) load() {
fmt.Println("Loading cago on ", t.name)
}
func (t Ship) unload() {
fmt.Println("Unloading cargo from ", t.name)
}
type TransportFactory interface {
create(string) Transporter
}
type TruckFactory struct{}
type ShipFactory struct{}
func (TruckFactory) create(name string) Transporter {
return Truck{name}
}
func (ShipFactory) create(name string) Transporter {
return Ship{name}
}
var _ TransportFactory = TruckFactory{} //Ensures that the "TransportFactory" Interface is implemented.
var _ TransportFactory = ShipFactory{} //Ensures that the "TransportFactory" Interface is implemented.
func main() {
var shipFactory TransportFactory = ShipFactory{}
var truckFactory TransportFactory = TruckFactory{}
ship1 := shipFactory.create("evergreen")
ship1.load()
ship1.transport()
ship1.unload()
truck1 := truckFactory.create("scania")
truck1.load()
truck1.transport()
truck1.unload()
}