设计模式笔记01

七大设计原则和二十四种设计模式

七大设计原则

1.开闭原则
在为了不破坏系统稳定性,使类的函数以及实现对扩展开发,对修改关闭;
2.依赖倒置原则
上层模块不应该依赖下层模块,二者都应该依赖其抽象,这样可以利用运行时多态的特性,利用接口实现低耦合;
3.单一职责原则
单一职责指在类层面,方法层面,接口层面的职能都是单一的,根据职责来划分接口;
4.接口隔离原则
避免使用将多种职能的方法都集中在一个接口,一个类对一个类的依赖应该建立在最小的接口上;
5.迪米特原则
类的成员变量越少越好,尽量保持对朋友对象的关注,其他的交给中介类;
6.里氏替换
体现在类中成员对象的解耦,也就是当成员对象发生替换,类自己的行为还是保持不变。
7.合成/复用原则
尽量使用对象组合,而不是继承来达到复用的目的。

二十四种设计模式

1.单例模式
Ensure a class has only one instance,and provide a global point of access to it.
确保一个类只要一个实例,而且自行实例化并向整个系统提供这个实例。
双检锁懒汉式

/**
 * @description 线程安全 懒汉式 double-check locking
 */
public class DoubleCheckSingleton implements Serializable {
    private static final long serialVersionUID = -6639300923461560175L;

    private volatile static  DoubleCheckSingleton instance=null;

    private DoubleCheckSingleton(){
    }

    public static DoubleCheckSingleton getInstance(){
        if(instance==null){
            synchronized (DoubleCheckSingleton.class){
                if(instance==null){
                    instance=new DoubleCheckSingleton();
                }
            }
        }
        return instance;
    }
    //在序列化完成后readResolve返回的对象将替代readObject的
    private Object readResolve(){
        return instance;
    }
}

2.工厂方法模式
Define an interface for creating an object,but let subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation to subclasses.
定义一个用于创建对象的接口,让子类决定实例化哪一个类。
工厂方法使一个类的实例化延迟到其子类。

3.抽象工厂模式
Provide an interface for creating families of related or dependent objects without specifying their concrete classes
为创建一组相关或相互依赖的对象提供一个接口,而且无须指定它们的具体类。
个人理解:在实现类中指定的,由抽象类声明实现类实例调用指定方法,来获取指定对象,其中产品类也是抽象的。

4.模板方法模式
Define the skeleton of an algorithm in an operation, defering some steps to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
定义一个操作中的算法的框架,而将一些步骤延迟到子类中。
模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

5.建造者模式
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

6.代理模式
Provide a surrogate or placeholder for another object to control access of it.
为其他对象提供一种代理以控制对这个对象的访问。

7.原型模式
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

8.中介者模式
Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by
keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
用一个中介对象封装一系列的对象交互,终结者使各对象不需要显式地相互作用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

9.命令模式
Encapsulate a request as an object, thereby letting you parameterize clients with different requests,queue or log requests,and support undoable operations.
将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

10.责任链模式
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.Chain the receiving objects and pass the request along the chain until an object handles it.
使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。

11.装饰模式
Attach additional responsibilities to an object dynamically keeping the same interface.Decorators provide a flexible alternative to subclassing for extending functionality.
动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵猴。

12.策略模式
Define a family of algorithms,encapsulate each one, and make them interchangeable.
定义一组算法,将每个算法都封装起来,并且使他们之间可以互换。

13.适配器模式
Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn`t otherwise beacuse of incompatible interfaces.
将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。

14.迭代器模式
Provide a way to access the elements of an aggregate object sequentially without exposing its underlyinig representation.
它提供一个方法访问一个容器对象中各个元素,而又不需暴露该对象的内部细节。

15.组合模式
Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of object uniformly.
将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

16.观察者模式
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新。

17,门面模式
Provide a unified interface to a set of interfaces in a subsystem.Facade defines a higher-level interface taht makes the subsystem easier to use.
要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用。

18.备忘录模式
Without violating encapsulation,capture and externalize an object`s internal state so that the object can be restored to this state later.
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

19.访问者模式
Represent an operation to be performed on the elements of an object structure.Visitor lets you define a new operation without changing the classes of the elements on which it operates.
封装一些作用于某种数据结构中的各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作。

20.状态模式
Allow an object to alter its behavior when its internal state changes.The object will appear to change its class.
当一个对象内在状态改变时允许其改变行为,这个对象看起来像改变了其类。

21.解释器模式
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
给定一门语言,定义它的文法的一种表示,并顶一个解释器,该解释器使用该表示来解释语言中的句子。

22.享元模式
Use sharing to support large numbers of fine-grained objects efficiently.
使用共享对象可有效地支持大量细粒度的对象。

23.桥梁模式
Decouple an abstraction from its implemention so that the two can vary indepnedently.
将抽象和实现解耦,使得两者可以独立的变化。

24.容器模式
Sidecar 模式(或者说Sidecar容器)的主要作用就是把业务容器与非业务容器解耦,使得应用的打包、发布工作能够更加user-friendly。在定义Pod的yaml文件中,我们可以设置相应的initContainer字段,这就是所谓的Sidecar容器,它总是比其他应用容器先启动。

设计模式三大类

1.创建类模式
工厂方法模式、建造者模式、抽象工厂模式、单例模式和原型模式。

2.结构类模式
适配器模式、桥梁模式、组合模式、装饰模式、门面模式、享元模式和代理模式。

3.行为类模式
责任链模式、命令模式、解释器模式、迭代器模式、中介者模式、备忘录模式、观察者模式、状态模式、策略模式、模板方法模式、访问者模式。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

边学习边学着写点博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值