implemented composite pattern

This commit is contained in:
ismayilmalik
2018-01-01 17:05:45 +03:00
parent d6a877df7d
commit 6c870679a4
3 changed files with 133 additions and 6 deletions

View 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)
}

View 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!")
}