73 lines
1.1 KiB
Go
73 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
/*
|
|
实例
|
|
汤宝宝教你番茄炒蛋~
|
|
*/
|
|
|
|
type TomatoEgg struct{}
|
|
|
|
func (t *TomatoEgg) Serve() {
|
|
fmt.Println("番茄炒蛋上桌啦!")
|
|
}
|
|
|
|
type CutTomato struct{}
|
|
|
|
func (c *CutTomato) Cut() {
|
|
fmt.Println("番茄切好了。")
|
|
}
|
|
|
|
type BeatEgg struct{}
|
|
|
|
func (b *BeatEgg) Beat() {
|
|
fmt.Println("鸡蛋打散了。")
|
|
}
|
|
|
|
type Cook struct {
|
|
cutTomato *CutTomato
|
|
beatEgg *BeatEgg
|
|
}
|
|
|
|
func (c *Cook) CookDish() {
|
|
c.cutTomato.Cut()
|
|
c.beatEgg.Beat()
|
|
fmt.Println("食材放入锅中,开火加热。")
|
|
fmt.Println("番茄炒蛋炒好了。")
|
|
}
|
|
|
|
// 外观
|
|
type ChefFacade struct {
|
|
cook *Cook
|
|
dish *TomatoEgg
|
|
}
|
|
|
|
func NewChefFacade() *ChefFacade {
|
|
return &ChefFacade{
|
|
cook: &Cook{
|
|
cutTomato: &CutTomato{},
|
|
beatEgg: &BeatEgg{},
|
|
},
|
|
dish: &TomatoEgg{},
|
|
}
|
|
}
|
|
|
|
func (c *ChefFacade) PrepareAndServe() {
|
|
fmt.Println("准备食材...")
|
|
c.cook.CookDish()
|
|
c.dish.Serve()
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println("============= 外观模式 =============")
|
|
|
|
chef := NewChefFacade()
|
|
chef.PrepareAndServe()
|
|
|
|
fmt.Println()
|
|
fmt.Println("=========== 外观模式结束 ===========")
|
|
}
|