[Design Patterns] (1) Overview

Design Patterns - Overview


2019-07-17 22:43:32 by rushing

 

1 Introduction

① design pattern is a software developer in the software development process, aimed at the general question of optimal solution , this solution can be programmers repeatedly applied to solve similar problems.

② 1994 by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides four (collectively  GOF ) co-published a book called  Design Patterns - Elements of Reusable Object- Oriented Software ( Chinese translation: design patterns - Reusable with object-oriented software elements) of the book, the book for the first time referred to the concept of software development design patterns. They proposed two design patterns based on object-oriented design principles: First, to achieve programming interface rather than programming. Second, the priority object composition rather than inheritance.

 

2. Type

(1) GOF design patterns total of 23 species, divided into three specific categories, including: create a schema (Creational Patterns), structural model (Structural Patterns), type of model (Behavioral Patterns) . Require additional learning design patterns as well: J2EE design patterns.

(2) describes the classification and

①  create schema
design patterns provide a way to create a hidden at the same time creating a logical object, instead of using the new operator directly instantiated objects. This makes the program more flexible when determining which objects need to be created for a given instance.

  • Factory pattern (Factory Pattern)
  • Abstract Factory (Abstract Factory Pattern)
  • Singleton (Singleton Pattern)
  • Builders mode (Builder Pattern)
  • Prototype model (Prototype Pattern)

②  structural model
design patterns focus on a combination of classes and objects. The concept of inheritance is used to define a combination of interfaces and combinations of new features of the object.

  • Adapter mode (Adapter Pattern)
  • Bridge mode (Bridge Pattern)
  • Filter mode (Filter, Criteria Pattern)
  • Combined mode (Composite Pattern)
  • Decorator (Decorator Pattern)
  • Appearance Model (Facade Pattern)
  • Flyweight (Flyweight Pattern)
  • Agent mode (Proxy Pattern)

③  behavioral patterns
of these design mode is particularly concerned about the communication between objects.

  • Chain of Responsibility pattern (Chain of Responsibility Pattern)
  • Command Mode (Command Pattern)
  • Interpreter pattern (Interpreter Pattern)
  • Iterator pattern (Iterator Pattern)
  • Intermediary model (Mediator Pattern)
  • Memo mode (Memento Pattern)
  • The observer pattern (Observer Pattern)
  • Mode state (State Pattern)
  • Null Object mode (Null Object Pattern)
  • Strategy Mode (Strategy Pattern)
  • Template mode (Template Pattern)
  • Visitor pattern (Visitor Pattern)

④  the J2EE patterns
of these design mode is particularly concerned about the presentation layer. These modes are identified by the Sun Java Center.

  • MVC pattern (MVC Pattern)
  • Agent mode (Business Delegate Pattern)
  • Composition entity mode (Composite Entity Pattern)
  • Mode data access object (Data Access Object Pattern)
  • Front-end controller mode (Front Controller Pattern)
  • Interceptor filter pattern (Intercepting Filter Pattern)
  • Service Locator pattern (Service Locator Pattern)
  • Object transfer mode (Transfer Object Pattern)

(3) Contact

 

3. The six principles of design patterns

① Open Closed Principle (Open Close Principle)

The principle of opening and closing means: open for extension, but closed for modification . When the program needs to expand, not to modify the existing code to achieve the effect of a hot-swappable. In short, the program is to improve the scalability, easy maintenance and upgrades. Want to achieve this effect, we need to use interfaces and abstract classes, follow the detailed design, we will mention this point.

② Richter substitution principle (Liskov Substitution Principle)

Richter substitution principle is one of the basic principles of object-oriented design. Richter substitution principle in that place any base class can appear, sub-class will be able to appear. LSP is inherited cornerstone multiplexed only when the derived class can replace the base class, and the software unit of functionality is not affected, the base class can really be reused, and the derived class can add new on the basis of the base class on behavior. Richter substitution principle is the principle of complementarity closed off. Implement the principle of opening and closing is a key step in abstraction, inheritance and base class and sub-class is the abstract concrete realization, so Richter substitution principle is the specification for concrete steps to achieve the abstract.

③ Dependency Inversion Principle (Dependence Inversion Principle)

This principle is the basis of the principle of opening and closing, the specific content: for interface programming, depend on the abstract rather than relying on specific.

④ Interface Segregation Principle (Interface Segregation Principle)

This principle means that: a plurality of interface isolation, is better than using a single interface. It also has another meaning is: to reduce the coupling between classes. Thus, the fact that design patterns from large-scale software architecture, easy to upgrade and maintain the software design, which emphasizes reducing reliance, reduce coupling.

⑤ Demeter, also known as the least known principle (Demeter Principle)

最少知道原则是指:一个实体应当尽量少地与其他实体之间发生相互作用,使得系统功能模块相对独立。

⑥ 合成复用原则(Composite Reuse Principle)

合成复用原则是指:尽量使用合成/聚合的方式,而不是使用继承。

 

 

4. 设计模式类型事例

① 创建型模式

FACTORY?加工工厂:给它“M4A1”,它给你产把警枪,给它“AK47”,你就端了把匪枪。CS里买枪的程序一定是用这个模式的。

BUILDER?生产流水线:以前是手工业作坊式的人工单个单个的生产零件然后一步一步组装做,好比有了工业革命,现在都由生产流水线代替了。如要造丰田汽车,先制定汽车的构造如由车胎、方向盘、发动机组成。再以此构造标准生产丰田汽车的车胎、方向盘、发动机。然后进行组装。最后得到丰田汽车。

PROTOTYPE?印刷术的发明:以前只能临贴抄写费时费力,效率极低,有了印刷术,突突的。

SINGLETON?确保唯一:不是靠new的,是靠instance的,而且要instance地全世界就这么一个实例(这可怜的类,也配叫“类”)。 看SingleTon类代码。

② 结构型模式

ADAPTER?翻译官:胡哥只会汉语,布什只会美语,翻译官既通汉又通美,Adapter了 。

DECORATOR?装饰:名字可以标识一个人,为了表示对一个人的尊重,一般会称其为“尊敬的”,有了装饰,好看多了。

BRIDGE?白马非马:马之颜色有黑白,马之性别有公母。我们说"这是马"太抽象,说"这是黑色的公马"又太死板,只有将颜色与性别和马动态组合,"这是(黑色的或白色的)(公或母)马"才显得灵活而飘逸,如此bridge模式精髓得矣。

COMPOSITE?大家族:子又生孙,孙又生子,子子孙孙,无穷尽也,将众多纷杂的人口组织成一个按辈分排列的大家族即是此模式的实现。

FACADE?求同存异:高中毕业需读初中和高中,博士也需读初中和高中,因此国家将初中和高中普及成九年制义务教育。

FLYWEIGHT?一劳永逸:认识三千汉字,可以应付日常读书与写字,可见头脑中存在这个汉字库的重要。

PROXY?垂帘听政:犹如清朝康熙年间的四大府臣,很多权利不在皇帝手里,必须通过辅佐大臣去办。

③ 行为型模式

CHAIN OF RESPONSIBLEITY?租房:以前为了找房到处打听,效率低且找不到好的房源。现在有了房屋中介,于是向房屋中介提出租房请求,中介提供一个合适的房源,满意则不再请求,不满意继续看房,直到满意为止。

COMMAND?借刀杀人:以前是想杀谁就杀,但一段时间后领悟到,长此以往必将结仇太多,于是假手他人,挑拨他人之间的关系从而达到自己的目的。

INTERPRETER?文言文注释:一段文言文,将它翻译成白话文。

ITERATOR?赶尽杀绝:一个一个的搜索,绝不放掉一个。

MEDIATOR?三角债:本来千头万绪的债务关系,忽出来一中介,包揽其一切,于是三角关系变成了独立的三方找第四方中介的关系。

MEMENTO?有福同享:我有多少,你就有多少。

OBSERVER?看守者:一旦被看守者有什么异常情况,定会及时做出反应。

STATE?进出自由:如一扇门,能进能出,如果有很多人随时进进出出必定显得杂乱而安全,如今设一保安限制其进出,如此各人进出才显得规范。

STRATEGY?久病成良医:如人生病可以有各种症状,但经过长期摸索,就可以总结出感冒、肺病、肝炎等几种。

TEMPLATE METHOD?理论不一定要实践:教练的学生会游泳就行了,至于教练会不会则无关紧要。

VISITOR?依法治罪:因张三杀人要被处死,李四偷窃要被罚款。由此势必制定处罚制度,故制定法律写明杀人、放火、偷窃等罪要受什么处罚,经通过后须变动要小。今后有人犯罪不管是谁,按共条例处罚即是,这就是访问者模式诞生的全过程。

 

参考:

https://www.runoob.com/design-pattern/design-pattern-intro.html

Guess you like

Origin www.cnblogs.com/yadiel-cc/p/11204397.html