go-pattern-examples/structure/04_flyweight/flyweight.go

74 lines
1.5 KiB
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package flyweight
2020-05-01 14:46:07 +03:00
/*
* @Description: github.com/crazybber
* @Author: Edward
* @Date: 2020-05-01 18:10:57
* @Last Modified by: Edward
* @Last Modified time: 2020-05-01 19:38:21
*/
import (
"fmt"
)
2020-04-21 17:50:21 +03:00
2020-05-01 14:46:07 +03:00
//IDeliverCompany 公司的能力
type IDeliverCompany interface {
//雇人
Hire(name string)
//送货任务
DeliverTask(name string, packets []string)
2020-04-21 17:50:21 +03:00
2020-05-01 14:46:07 +03:00
GetDeliver(name string) IDeliver
2020-04-21 17:50:21 +03:00
}
2020-05-01 14:46:07 +03:00
//DeliverCompany 快递公司
type DeliverCompany struct {
Employees map[string]IDeliver
}
2020-04-21 17:50:21 +03:00
2020-05-01 14:46:07 +03:00
//IDeliver 快递员能做的事情
type IDeliver interface {
//送货
DeliverPackets(packets []string)
2020-04-21 17:50:21 +03:00
}
2020-05-01 14:46:07 +03:00
//Deliver 快递员工,员工是一个享元对象
type Deliver struct {
Name string //快递员的名字
Packets []string //快递员的携带的快递,这一部分是变化的
2020-04-21 17:50:21 +03:00
}
2020-05-01 14:46:07 +03:00
//Hire 雇佣新员工,员工是全公司共享
func (d *DeliverCompany) Hire(name string) {
if d.Employees == nil || len(d.Employees) == 0 {
d.Employees = make(map[string]IDeliver)
2020-04-21 17:50:21 +03:00
}
2020-05-01 14:46:07 +03:00
if _, ok := d.Employees[name]; ok {
fmt.Println("already hired")
return
}
d.Employees[name] = &Deliver{Name: name}
2020-04-21 17:50:21 +03:00
2020-05-01 14:46:07 +03:00
fmt.Println("hired")
2020-04-21 17:50:21 +03:00
}
2020-05-01 14:46:07 +03:00
//GetDeliver return Deliver
func (d *DeliverCompany) GetDeliver(name string) IDeliver {
return d.Employees[name]
2020-04-21 17:50:21 +03:00
}
2020-05-01 14:46:07 +03:00
//DeliverTask 派员工送货
func (d *DeliverCompany) DeliverTask(name string, packets []string) {
d.Employees[name].DeliverPackets(packets)
2020-04-21 17:50:21 +03:00
}
2020-05-01 14:46:07 +03:00
//DeliverPackets 送货了
func (d *Deliver) DeliverPackets(packets []string) {
fmt.Println(d.Name, ": Delivered:", packets)
2020-04-21 17:50:21 +03:00
}