From 4f8d653153f0d49e0e051d3f26322d25c6080597 Mon Sep 17 00:00:00 2001 From: IvisTang Date: Fri, 2 Jan 2026 01:01:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=A4=96=E8=A7=82=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E7=A4=BA=E4=BE=8B=EF=BC=8C=E6=B7=BB=E5=8A=A0=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E7=B1=BB=E5=92=8C=E6=96=B9=E6=B3=95=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/facade/main.go | 72 ++++++++++++++++++++++++++++++++++++++++++ python/facade/main.py | 49 ++++++++++++++++++++++++++++ ts/src/facade/index.ts | 63 ++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 go/facade/main.go create mode 100644 python/facade/main.py create mode 100644 ts/src/facade/index.ts diff --git a/go/facade/main.go b/go/facade/main.go new file mode 100644 index 0000000..50b5eb0 --- /dev/null +++ b/go/facade/main.go @@ -0,0 +1,72 @@ +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("=========== 外观模式结束 ===========") +} diff --git a/python/facade/main.py b/python/facade/main.py new file mode 100644 index 0000000..416d7a4 --- /dev/null +++ b/python/facade/main.py @@ -0,0 +1,49 @@ +""" +实例 +汤宝宝教你番茄炒蛋~ +""" + +class TomatoEgg: + def serve(self): + print("番茄炒蛋上桌啦!") + +class CutTomato: + def cut(self): + print("番茄切好了!") + +class BeatEgg: + def beat(self): + print("鸡蛋打好了!") + +class Cook: + cutTomato: CutTomato + beatEgg: BeatEgg + + def __init__(self,cutTomato: CutTomato, beatEgg: BeatEgg) -> None: + self.cutTomato = cutTomato + self.beatEgg = beatEgg + + def cook_dish(self): + self.cutTomato.cut() + self.beatEgg.beat() + print("食材放入锅中,开火加热。") + print("番茄炒蛋炒好了。") + +# 外观 +class ChefFacade: + cook: Cook + dish: TomatoEgg + def __init__(self) -> None: + self.cook = Cook(CutTomato(), BeatEgg()) + self.dish = TomatoEgg() + + def prepare_and_serve(self): + print("准备食材...") + self.cook.cook_dish() + self.dish.serve() + +if __name__ == "__main__": + print("============= 外观模式 =============") + chef = ChefFacade() + chef.prepare_and_serve() + print("\n=========== 外观模式结束 ===========") \ No newline at end of file diff --git a/ts/src/facade/index.ts b/ts/src/facade/index.ts new file mode 100644 index 0000000..26143e1 --- /dev/null +++ b/ts/src/facade/index.ts @@ -0,0 +1,63 @@ +/* +实例 +汤宝宝教你番茄炒蛋~ +*/ + +class TomatoEgg { + serve() { + console.log("番茄炒蛋上桌啦!") + } +} + +class CutTomato { + cut() { + console.log("番茄切好了。"); + } +} + +class BeatEgg { + beat() { + console.log("鸡蛋打散了。"); + } +} + +class Cook { + private cutTomato: CutTomato; + private beatEgg: BeatEgg; + + constructor() { + this.cutTomato = new CutTomato(); + this.beatEgg = new BeatEgg(); + } + + cookDish() { + this.cutTomato.cut(); + this.beatEgg.beat(); + console.log("食材放入锅中,开火加热。"); + console.log("番茄炒蛋炒好了。"); + } +} + +// 外观 +class ChefFacade { + private tomatoEgg: TomatoEgg; + private cook: Cook; + + constructor() { + this.tomatoEgg = new TomatoEgg(); + this.cook = new Cook(); + } + + prepareAndServe() { + console.log("准备食材...") + this.cook.cookDish(); + this.tomatoEgg.serve(); + } +} + +(function(){ + console.log("============= 外观模式 =============") + const chef = new ChefFacade(); + chef.prepareAndServe(); + console.log("\n=========== 外观模式结束 ===========") +})()