implemented composite pattern
This commit is contained in:
35
structural/composition/byembeding.go
Normal file
35
structural/composition/byembeding.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package composition
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Car struct {
|
||||
WheelCount uint8
|
||||
Speed int
|
||||
DoorCount uint8
|
||||
Brand string
|
||||
}
|
||||
|
||||
func (c *Car) Drive() {
|
||||
fmt.Println("Goes with speed: %d", c.Speed)
|
||||
}
|
||||
|
||||
type FlyingCar struct {
|
||||
Car
|
||||
WingLength float32
|
||||
FlySpeed int
|
||||
}
|
||||
|
||||
func (c *FlyingCar) Fly() {
|
||||
fmt.Println("Flies with speed: %d", c.FlySpeed)
|
||||
}
|
||||
|
||||
type SwimmingCar struct {
|
||||
Car
|
||||
SwimSpeed int
|
||||
}
|
||||
|
||||
func (c *SwimmingCar) Swimm() {
|
||||
fmt.Println("Swimm with speed: %d", c.SwimSpeed)
|
||||
}
|
56
structural/composition/byinterface.go
Normal file
56
structural/composition/byinterface.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package composition
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Driver interface {
|
||||
Drive()
|
||||
}
|
||||
|
||||
type Cleaner interface{
|
||||
Clean()
|
||||
}
|
||||
|
||||
type Killer interface {
|
||||
Kill()
|
||||
}
|
||||
|
||||
type Barker interface {
|
||||
Bark()
|
||||
}
|
||||
|
||||
type Dog struct {}
|
||||
func (d *Dog) Bark() {
|
||||
fmt.Println("Wow!")
|
||||
}
|
||||
|
||||
type RobotDog struct {}
|
||||
func (r *RobotDog) Drive() {
|
||||
fmt.Println("Driving...")
|
||||
}
|
||||
func (r *RobotDog) Bark() {
|
||||
fmt.Println("Wow!")
|
||||
}
|
||||
|
||||
type CleanerRobotDog struct {}
|
||||
func (r *CleanerRobotDog) Drive() {
|
||||
fmt.Println("Driving...")
|
||||
}
|
||||
func (r *CleanerRobotDog) Bark() {
|
||||
fmt.Println("Wow!")
|
||||
}
|
||||
func (r *CleanerRobotDog) Clean() {
|
||||
fmt.Println("Cleaning...")
|
||||
}
|
||||
|
||||
type KillerRobotDog struct {}
|
||||
func (r *KillerRobotDog) Drive() {
|
||||
fmt.Println("Driving...")
|
||||
}
|
||||
func (r *KillerRobotDog) Bark() {
|
||||
fmt.Println("Wow!")
|
||||
}
|
||||
func (r *KillerRobotDog) Kill() {
|
||||
fmt.Println("You gonna die!")
|
||||
}
|
Reference in New Issue
Block a user