aiyoudiao aiyoudiao
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档

码二

扫微信二维码,认识一下码二吧😉。
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档
  • LeetCode

  • 算法

  • 数据结构

  • 设计模式

    • 浅聊设计理念
    • 谈一谈设计原则
    • 工厂方法模式
    • 抽象工厂模式
    • 建造者模式
    • 原型模式
    • 单例模式
    • 桥接模式
    • 代理模式
    • 装饰器模式
    • 适配器模式
    • 享元模式
    • 组合模式
    • 外观模式
      • 前言
      • 外观模式
    • 观察者模式
    • 模板方法模式
    • 策略模式
    • 职责链模式
    • 状态模式
    • 迭代器模式
    • 访问者模式
    • 备忘录模式
    • 命令模式
    • 解释器模式
    • 中介者模式
  • Other

  • vue3设计与实现

  • 算法与设计
  • 设计模式
aiyoudiao
2022-02-23

外观模式

# 前言

外观模式属于结构型模式,这个类型的设计模式总结出了 类、对象组合后的经典结构,将类、对象的结构和使用解耦了,花式的去借用对象。

# 外观模式

使用场景:这个设计模式在日常生活中很常见,比如10086服务热线,这种设计模式将很多分开的功能进行了汇总,将细粒度的功能分别别类的进行了综合,你只需要通过简单的方式就能使用它。一般都在高层次的地方使用这种设计模式,因为综合之后就不太好拆分了,也许它综合的功能完全是你不喜欢的搭配。如果它综合的功能是你喜欢的搭配,你可以通过代理、适配等方式重组出新的功能来。

理解:类、对象的结构和使用解耦,通过这种汇总的方式,在应用层提供了很方便的功能,虽然缺点是它汇总的方式并不是你期望的,但是你可以参照它内部的实现方式自己再来重新汇总呀,照虎画豹子。

namespace struct_mode_07 {

    // 做饭接口
    interface ICook {
        // 洗碗、洗菜、做菜
        washVegetable(): string
        washDishes(): string
        cookAdish(): string
    }

    class Mom implements ICook {

        washVegetable(): string {
            return '妈妈洗碗'
        }
        washDishes(): string {
            return '妈妈洗菜'
        }
        cookAdish(): string {
            return '妈妈做菜'
        }
    }

    class Dad implements ICook {
        washVegetable(): string {
            return '爸爸洗碗'
        }
        washDishes(): string {
            return '爸爸洗菜'
        }
        cookAdish(): string {
            return '爸爸做菜'
        }
    }

    // 晚餐接口
    interface IDinner {
        cook(who: string): IDinner
    }

    // 外观类
    class DinnerFacade implements IDinner {

        private mom = new Mom()
        private dad = new Dad()

        cook(who: string): IDinner {
            console.log('制作晚餐中...')

            if (who === 'mom') {
                this.cookMom()
            } else if (who === 'dad') {
                this.cookDad()
            } else if (who === 'dad mom') {
                this.cookDadMom()
            } else {
                console.log('无人制作晚餐,请到外面饭店就餐')
                return this
            }
            
            console.log('制作晚餐完毕...')

            return this
        }

        private cookMom(): void {

            console.log(this.mom.washVegetable())
            console.log(this.mom.washDishes())
            console.log(this.mom.cookAdish())

        }

        private cookDad(): void {

            console.log(this.dad.washVegetable())
            console.log(this.dad.washDishes())
            console.log(this.dad.cookAdish())

        }

        private cookDadMom(): void {

            console.log(this.dad.washVegetable())
            console.log(this.dad.washDishes())
            console.log(this.mom.cookAdish())

        }
    }

    // 使用
    new DinnerFacade().cook('mom').cook('dad').cook('dad mom').cook('')
}
#设计模式
上次更新时间: 10年18月2023日 01时57分53秒
组合模式
观察者模式

← 组合模式 观察者模式 →

最近更新
01
01.数据结构导论一览.md
10-16
02
30.2023年06月04日.md
06-04
03
08.与测量相关.md
05-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 aiyoudiao 码二 备案号: 鄂ICP备2022002654号-1