设计模式总览 —— 导论

什么是 GOF

  • 对接口编程而不是对实现编程。
  • 优先使用对象组合而不是继承。

设计模式的基石

  • 封装
  • 继承
  • 多态
  • 顺序
  • 判断
  • 循环

设计模式总览

  • 创建型模式(Creational Patterns)

    • 单例(Singleton)模式
    • 原型(Prototype)模式
    • 工厂方法(FactoryMethod)模式
    • 抽象工厂(AbstractFactory)模式
    • 建造者(Builder)模式
  • 结构型模式(Structural Patterns)

    • 代理(Proxy)模式
    • 适配器(Adapter)模式
    • 桥接(Bridge)模式
    • 装饰(Decorator)模式
    • 外观(Facade)模式
    • 享元(Flyweight)模式
    • 组合(Composite)模式
    • 过滤器模式(Filter Pattern)
  • 行为型模式(Behavioral Patterns)

    • 模板方法(Template Method)模式
    • 策略(Strategy)模式
    • 命令(Command)模式
    • 职责链(Chain of Responsibility)模式
    • 状态(State)模式
    • 观察者(Observer)模式
    • 中介者(Mediator)模式
    • 迭代器(Iterator)模式
    • 访问者(Visitor)模式
    • 备忘录(Memento)模式
    • 解释器(Interpreter)模式
      在这里插入图片描述

设计的7大原则

  • 开闭原则(Open Closed Principle,OCP)

    • 软件实体应当对扩展开放,对修改关闭(Software entities should be open for extension,but closed for modification)
    • 合成复用原则、里氏替换原则相辅相成,都是开闭原则的具体实现规范
    • 扩展新类而不是修改旧类
  • 里氏替换原则(Liskov Substitution Principle,LSP)

    • 继承必须确保超类所拥有的性质在子类中仍然成立(Inheritance should ensure that any property proved about supertype objects also holds for subtype objects)
    • 继承父类而不去改变父类
  • 依赖倒置原则(Dependence Inversion Principle,DIP)

    • 高层模块不应该依赖低层模块,两者都应该依赖其抽象;抽象不应该依赖细节,细节应该依赖抽象(High level modules shouldnot depend upon low level modules.Both should depend upon abstractions.Abstractions should not depend upon details. Details should depend upon abstractions)
    • 面向接口编程,而不是面向实现类
  • 单一职责原则(Single Responsibility Principle,SRP)

    • 一个类应该有且仅有一个引起它变化的原因,否则类应该被拆分(There should never be more than one reason for a class to change)
    • 每个类只负责自己的事情,而不是变成万能
  • 接口隔离原则(Interface Segregation Principle,ISP)

    • 一个类对另一个类的依赖应该建立在最小的接口上(The dependency of one class to another one should depend on the smallest possible interface)。
    • 各个类建立自己的专用接口,而不是建立万能接口
  • 迪米特法则(Law of Demeter,LoD)

    • 最少知识原则(Least Knowledge Principle,LKP)
    • 只与你的直接朋友交谈,不跟“陌生人”说话(Talk only to your immediate friends and not to strangers)
    • 无需直接交互的两个类,如果需要交互,使用中间者
    • 过度使用迪米特法则会使系统产生大量的中介类,从而增加系统的复杂性,使模块之间的通信效率降低
  • 合成复用原则(Composite Reuse Principle,CRP)

    • 又叫组合/聚合复用原则(Composition/Aggregate Reuse Principle,CARP)
    • 软件复用时,要尽量先使用组合或者聚合等关联关系来实现,其次才考虑使用继承关系来实现
    • 组合:把要引用的对象放在类里面当成属性。
    • 优先组合,其次继承
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值