实现桥接模式和组合模式示例,添加相关类和方法,增强代码可读性
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
/*
|
||||
实例
|
||||
汤宝宝成为了一个电子画家,但是不同形状的笔刷和不同颜色颜料的组合让他犯了难...
|
||||
*/
|
||||
|
||||
// 抽象类
|
||||
type Paint interface {
|
||||
Use() string
|
||||
}
|
||||
|
||||
type Brush interface {
|
||||
Draw()
|
||||
}
|
||||
|
||||
// 具体实现类
|
||||
type RedPaint struct{}
|
||||
|
||||
func (r *RedPaint) Use() string {
|
||||
return "红色颜料"
|
||||
}
|
||||
|
||||
type BluePaint struct{}
|
||||
|
||||
func (b *BluePaint) Use() string {
|
||||
return "蓝色颜料"
|
||||
}
|
||||
|
||||
type RoundBrush struct {
|
||||
paint Paint
|
||||
}
|
||||
|
||||
func (r *RoundBrush) Draw() {
|
||||
fmt.Printf("使用圆形笔刷和%s进行绘图!\n", r.paint.Use())
|
||||
}
|
||||
|
||||
type SquareBrush struct {
|
||||
paint Paint
|
||||
}
|
||||
|
||||
func (s *SquareBrush) Draw() {
|
||||
fmt.Printf("使用方形笔刷和%s进行绘图!\n", s.paint.Use())
|
||||
}
|
||||
|
||||
// 绘图函数
|
||||
func Painting(brush Brush) {
|
||||
brush.Draw()
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("============= 桥接模式 =============")
|
||||
|
||||
redPaint := &RedPaint{}
|
||||
bluePaint := &BluePaint{}
|
||||
|
||||
Painting(&RoundBrush{paint: redPaint})
|
||||
Painting(&SquareBrush{paint: redPaint})
|
||||
Painting(&RoundBrush{paint: bluePaint})
|
||||
Painting(&SquareBrush{paint: bluePaint})
|
||||
|
||||
fmt.Println("\n=========== 桥接模式结束 ===========")
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
/*
|
||||
实例
|
||||
汤宝宝成为了上市集团老总,他们集团有好多好多家分公司...
|
||||
*/
|
||||
|
||||
// 抽象接口
|
||||
type Company interface {
|
||||
// 显示公司名称
|
||||
Show(prefix string)
|
||||
// 开设分公司
|
||||
Add(company Company)
|
||||
// 分公司关门
|
||||
Remove(company Company)
|
||||
// 获取分公司
|
||||
GetChild(i int) Company
|
||||
}
|
||||
|
||||
// 具体实现类
|
||||
type ConcreteCompany struct {
|
||||
name string
|
||||
children []Company
|
||||
}
|
||||
|
||||
func (c *ConcreteCompany) Show(prefix string) {
|
||||
fmt.Print(prefix)
|
||||
fmt.Println(c.name)
|
||||
for _, child := range c.children {
|
||||
child.Show(prefix + "-> ")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ConcreteCompany) Add(company Company) {
|
||||
c.children = append(c.children, company)
|
||||
fmt.Printf("在%s下开设了分公司%s\n", c.name, company.(*ConcreteCompany).name)
|
||||
}
|
||||
|
||||
func (c *ConcreteCompany) Remove(company Company) {
|
||||
for i, child := range c.children {
|
||||
if child == company {
|
||||
c.children = append(c.children[:i], c.children[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Printf("在%s下关闭了分公司%s\n", c.name, company.(*ConcreteCompany).name)
|
||||
}
|
||||
|
||||
func (c *ConcreteCompany) GetChild(i int) Company {
|
||||
if i < 0 || i >= len(c.children) {
|
||||
return nil
|
||||
}
|
||||
return c.children[i]
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("============= 组合模式 =============")
|
||||
|
||||
headOffice := &ConcreteCompany{name: "汤氏总公司"}
|
||||
headOffice.Add(&ConcreteCompany{name: "汤氏A分公司"})
|
||||
headOffice.Add(&ConcreteCompany{name: "汤氏B分公司"})
|
||||
headOffice.Add(&ConcreteCompany{name: "汤氏C分公司"})
|
||||
headOffice.GetChild(0).Add(&ConcreteCompany{name: "汤氏A分公司-子公司1"})
|
||||
headOffice.GetChild(0).Add(&ConcreteCompany{name: "汤氏A分公司-子公司2"})
|
||||
headOffice.Remove(headOffice.GetChild(2))
|
||||
fmt.Println()
|
||||
headOffice.Show("~ ")
|
||||
fmt.Println()
|
||||
headOffice.GetChild(0).Show("~ ")
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("=========== 组合模式结束 ===========")
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user