• 设计模式 简介


    1简介:     
         设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。
    笔记:
    1基本要素
    模式名称(Pattern Name)、问题(Problem)、解决方案(Solution)、效果(Consequences
     
    2分类
     
     
    3原则
    (1)单一职责原则SRP(Single Responsibility Principle)
    (2)里氏替换原则LSP(Liskov Substitution Principle)
    (3)依赖倒置原则(High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstraction should not depend upon details. Detail should depend upon abstractions.)
    (4)接口隔离原则
    (Clients should not be forced to depend upon interfaces that they don't use.)
    (The dependency of one class to another one should depend on the smallest possible interface.)
    (5)迪米特法则(Only talk to your immedate friends.)
    (6)开闭原则(Software entities like classes, modules and functions should be open for extension but closed for modifications.)
     
    4设计模式_创建型
    (1)Singleton(单例模式)
    Ensure a class has only one instance, and provide a global point of access to it.
    保证一个类只有一个实例,并提供一个访问它的全局访问点。
    代码清单:
    class Singleton{
    protect:
         Singleton(){}
    private:
         static Singleton* _instance;
    public:
         static Singleton* Instance(){
              if(_instance == NULL)
                   _instance = new Singleton();
              return _instance;
         }
    };
    (2)Factory Method(工厂方法模式)
    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)Abstract Factory(抽象工厂模式)
    Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
    提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们的具体类。
    (4)Builder(建造模式)
    Separate the construction of a complex object from its representation process can create different representations.
    将一个复杂对象的构建与他的表示相分离,使得同样的构建过程可以创建不同的表示。
    (5)Prototype(原型模式)
    Specify the kinds of objects to create using a prototypical instance, and create new objects by copying his prototype.
    用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建新的对象。
     
    5设计模式_结构型
    (1)Adapter(适配器模式)
    Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
    将一类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作那些类可以一起工作。
    (2)Bridge(桥接模式)
    Decouple an abstraction from its implementation so that the two can vary independently.
    将抽象部分与它的实现部分相分离,使他们可以独立的变化。
    (3)Composite(组合模式)
    Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
    将对象组合成树形结构以表示“部分-整体”的关系,使得用户对单个对象和组合对象的使用具有一致性。
    [只要是树形结构或者体现局部和整体关系时,就要考虑使用组合模式。]
    (4)Decrator(装饰模式)
    Attach additional responsibilities to an object dynamically keeping the same interface. Decrators provide a flexible alternative to subclassing for extending functionality.     
    动态地给一个对象增加一些额外的职责,就增加的功能来说,相比生成子类更加灵活。
    (5)Facade(外观模式)
    Provide a inifiled interface to a set of interfaces in a subsystem. Facade defines a higher-lever interface that makes the subsystem easier to use.
    为子系统中的一组接口提供一致的界面,facade模式定义了一高层接口,这个接口使得这一子系统更容易使用。
    (6)Flyweight(享元模式)
    Using sharing to support large numbers of fine-grained objects efficiently.
    运用共享技术有效地支持大量细粒度的对象。
    [需要缓冲池的场景或者系统中存在大量的相似对象。]
    (7)Proxy(代理模式)
    Provide a surrogate or placeholder for another object to control access to it.
    为其他对象提供一种代理以控制对这个对象的访问。
     
    5设计模式_行为型
    (1)Interpreter(解释器模式)
    Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
    给定一个语言,定义他的文法的一个表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
    [一个语法需要解释的场景]
    (2)Template Method(模板方法)
    Define the skeleton of an algorithm in an operation, deferring some steps to subelasses. Temp;late Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
    定义一个操作中的算法的骨架,而将一些步骤延迟到子类中,TemplateMethod使得子类可以不改变一个算法的结构即可以重定义该算法得某些特定步骤。
    (3)Chain of Responsibility(职责链模式)
    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.
    使多个对象都有机会处理请求,从而避免请求的送发者和接收者之间的耦合关系。
    (4)Command(命令模式)
    Encapsulate a request as an object, thereby letting you paramterize clients with different requests, queue or log requests, and support undoable operations.
    将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化,对请求排队和记录请求日志,以及支持可撤销的操作。
    (5)Iterator(迭代器模式)
    Provide a way to access the elements of an aggregate object sequentially without exposing its underlying represeentation.
    提供一个方法顺序访问一个聚合对象的各个元素,而又不需要暴露该对象的内部表示。
    (6)Mediator(中介者模式)
    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.
    用一个中介对象封装一些列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
    (7)Memento(备忘录模式)
    Without violating encapsulation,capture and externalize an object's internal state so that the object can be restored to this state later.
    在不破坏对象的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。
    (8)Observer(观察者模式)
    Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
    定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知自动更新。
    (9)State(状态模式)
    Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
    允许对象在其内部状态改变时改变他的行为。对象看起来似乎改变了他的类。
    (10)Strategy(策略模式)
    Define a family of algorithms, encapsulate each one, and make them interchangeable.
    定义一系列的算法,把他们一个个封装起来,并使他们可以互相替换,本模式使得算法可以独立于它的客户而变化。
    (11)Visitor(访问者模式)
    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.
    表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素类的前提下定义作用于这个元素的新操作。 
     
    后记:
    1设计原理:

         (1)德墨忒尔律(The Law of Demeter):模块不应了解它所操作对象的内部情形。

    DRY原则(Don't Repeat youself)
         (2)“最小惊异原则”(The Principle of Least Surprise)函数或类应该实现其他程序员有理由期待的行为
        (3)长尾理论:长尾理论是指,商业和文化的未来不在于传统需求曲线上那个代表“畅销商品”的头部;而是那条代表“冷门商品”经常为人遗忘的长尾。
    2概念
    RBAC(Role-Based Access Control)基于角色的访问控制
    BO(Bussiness Object)业务对象
    Biz(Bussiness Logic)业务逻辑
    AOP(Aspect Oriented Programming)面向横切面的编程
     
    书籍:
    《设计模式——可复用面向对象软件的基础》
    《设计模式之禅》(Java)
    “If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.”
  • 相关阅读:
    ftp>ls 未连接
    Error running 'run': data.userName must not be null
    Excel另存为_有些Excel打开时会出现一些提示
    23种设计模式通俗理解
    清除html中的标记,只留下文字
    将DataTable中的数据导出成Excel
    C#读取文件
    系统性能瓶颈分析
    Angularjs的My97DatePicker扩展
    Memcache修改端口
  • 原文地址:https://www.cnblogs.com/Scorpio989/p/4071524.html
Copyright © 2020-2023  润新知