Su warm small talk design patterns

First, what is design patterns

Design patterns are set to be used repeatedly, most people know, after classification written summary of the code design experience. Use design patterns to reusable code, make the code easier to understand others, to ensure the reliability of the code. There is no doubt designed to make the code programming model real engineering, design patterns is the cornerstone of software engineering, like a piece of a brick building of the same. Project rational use of design patterns can be the perfect solution to many problems, how each model has a corresponding response, each pattern describes a constantly recurring problem around us, and the core of the solution to the problem, which the reason it is widely used.

Second, the design and pattern classification of the three key points

1, to create schema: instantiation of an object model, creating schema instantiation object for decoupling.

  1. Singleton : an instance of a class has intelligence, a global access point.
  2. The factory pattern : a factory class decided to create an instance of the class of product which according to the parameters passed.
  3. Abstract factory pattern : create a family related or dependent objects without having to explicitly specify a specific class.
  4. The builder pattern : the creation of a complex object package, and may be constructed by step.
  5. Prototype mode : to create a new instance by copying an existing instance.

2, structural model: the class or object bound together to form a larger structure.

  1. Decorator : dynamically add new features to the object.
  2. Proxy Mode : Provides a proxy for the other objects in order to control access to the object.
  3. Bridge mode : an abstraction and separation from its implementation, so that they can vary independently.
  4. Adapter Mode : The method for a class of client interface into another interface desired.
  5. Combined mode : combining objects in a tree structure to represent "part - whole" hierarchy.
  6. Aspect Mode : External provide a unified approach to access a group of interfaces subsystem.
  7. Flyweight : to effectively support large numbers of fine-grained objects through the sharing of technology.

3, behavioral patterns: how classes and objects interact, and the division of responsibilities and algorithms.

  1. Strategy Mode : Define a series of algorithms, encapsulate them, and make them interchangeable.
  2. Profile mode : the definition of a structure of the algorithm to delay some steps implemented subclasses.
  3. Command Mode : command request encapsulated as an object, making it possible to request a different parameterization.
  4. Iterator pattern : one kind of the polymerization method of accessing an object traversing each element, the internal structure of the object is not exposed.
  5. Observer pattern : many dependencies between objects.
  6. Arbiter mode : with a mediation object that encapsulates set of objects interact.
  7. Memo mode : without destruction of the package, maintaining an internal state of the object.
  8. Interpreter pattern : a given language, which defines a grammar representation and define an interpreter.
  9. State mode : allows an object to change its behavior when its internal state change objects.
  10. Chain of Responsibility pattern : the request of the sender and receiver decoupling, enabling multiple objects have the opportunity to deal with this request.
  11. Visitors mode : data without changing the structure of the premise, add new functions to act on a set of object elements.

Several three principles, design patterns

1, the principle of opening and closing

(1) For the extension is open (Open for extension). This behavior means that the module can be extended. When changing application requirements, we can extend the module, it has a new behavior to meet those changes. In other words, we can change the function of the module.

(2) Modifications are closed (Closed for modification). When the expansion behavior of the module, without changing the source code or binary code module. Binary executable version of the module, whether it is linked library, DLL or .EXE file, all without changes.

2, in the style substitution principle

Place any base class can appear, sub-class will be able to appear. Richter substitution principle is the cornerstone of inheritance reuse, and only when the derived class can replace the base class, the software unit of function is not affected, the base class can really be reused, derived class can also be on the basis of the base class adding new behavior. Richter substitution principle is the principle of complementarity closed off. Implement the principle of opening and closing is a key step in abstraction. The inheritance relationship with a base class is a subclass of the abstract concrete realization, so Richter substitution principle is the specification for concrete steps to achieve the abstract.

3, Dependency Inversion principle

Dependency Inversion principle of the program is to rely on abstract interface, do not depend on the specific implementation. Simply put, it is to ask for an abstract program, not to achieve programming, thus reducing the coupling between the client and the implementation module.

4, the interface segregation principle

The client should not rely on its unwanted interfaces, a class dependent on another class should be based on the smallest interface.

5, Demeter

Demeter is also called the principle of least knowledge, that an object should be to other objects and to know as little as possible, do not talk to strangers.

6, the principle of multiplexing Synthesis

Synthesis of complex software reuse when required, to try to achieve using other association or polymeric composition, followed before considering the use of the principle of inheritance be implemented. If you are using inheritance, we must strictly follow the Richter substitution principle. Synthesis of the complex with the complementary Richter substitution principle with principles, both the opening and closing principle embodied specification.

Fourth, the relationship between design patterns

Fifth, the design mode thoughts

一共有23种设计模式,可以说都是为了提高代码的可读性、可扩展性、可复用性、类的可替换性、组件化、可移植性等等特性。通过接口、抽象类、继承、实现、委托、抽象、面向接口编程、多态、重载、重写等方式使得代码的这些特性得以彰显,可以说只有深刻的理解了这些概念背后的哲学思想才能更好的理解设计模式。在设计模式中有很多思想,比如可以使用委托的不要使用继承、开闭原则,面向扩展开放,面向修改关闭,里式代换原则,父类一定能被子类代替并使用,反置则不然,面向接口编程,功能层次和实现层次分离(桥接模式)、高内聚低耦合等思想,这些思想都是宝贵的,正是因为这样的思想的存在才使得代码的更新换代的时候能够尽可能少的甚至不用修改之前的代码,直接加入新的内容。提高软件的开发周期,便于维护和升级,便于查找和纠错,易于扩展和使用。

同样的设计模式主要分为三大类,创建型、行为型、结构型。我们可以简单的这样分类,只不过这样的分类似乎并不准确,不能一语道出所有的本质,设计模式是相互关联的,有的设计模式内部其实是使用了别的设计模式作为支撑的,但是大体上这样的一种划分便于我们去记忆,仅此而已。

仔细的回顾一下我们前面学习的设计模式,看看我们能想到多少,从迭代器开始,我们将类中数据结构的遍历和类的功能实现分离出来,本质上使用了工厂模式。其次我们学习了适配器模式,它将不同的接口进行适配,从而便于版本的兼容性以及其他功能,然后我们学习了模板方法,使用模板面向抽象编程,便于新的子类的实现和管理;之后学习了工厂模式,其实借用了模板模式来创建产品,是一种非常重要用处很广的一种方法;然后我们学习了单例模式,有懒汉式、饿汉式等,生成关于某个类全局唯一的对象,注意多线程的影响;之后是原型模式,用来复制复杂的对象,使用了clone方法,然后是builder模式,用一个新的类对已有的抽象接口进行整合和编程,从而构建出我们想要的东西;然后是抽象工厂模式,使用了工厂模式,组合模式等模式,面向抽象编程,将抽象零件组装成抽象产品,便于具体工厂的创建,提高了代码的组件化和复用性;然后是桥接模式,将类的功能层次和实现层次分割开来,便于对应的扩展和使用;然后是策略模式,可以整体的替换策略,使用也很广泛;然后是组合模式,保证了同根同源,通过委托添加自己构成递归,树形结构,将具有树形特点的对象组合起来;然后是装饰器模式,和组合模式的结构类似,同样是递归结构,从而可以不断的装饰,增加新的功能,很好用;接着是visitor访问者模式,通过在类外访问类中的数据结构从而得到想要的结果,便于程序的可扩展性和组件化;接着是责任链模式,推卸责任,根据问题的大小来考虑自己释放处理,本质是链表,便于职责分明;然后是外观模式,通过整合各个类之间的调用关系,组建成了统一的接口(API),便于外部类的调用;接着是仲裁者模式,将很多类之间互相关联的关系交给仲裁者处理,省去了各个类之间的嵌套和调动,有利于高内聚和低耦合,思路清晰,便于扩展;然后是观察者模式,通过互相委托从而能够在被观察的类发生改变的时候得到相应的改变的信息并且处理;然后是备忘录模式,通过在某一时刻的状态保存下来,便于恢复,在游戏中使用的比较多;然后是状态模式,将状态当做类,从而职责分明,解除了很多繁琐的if和else这些分支逻辑,便于扩展;然后是享元模式,轻量级对象,通过共用不变对象来实现;然后是代理模式,懒加载真正的服务器,加快访问速度,代理是帮助服务器代理的;然后是命令模式,将命令当做类,通过保存一些列命令,从而能够随时执行这些命令,需要清除命令的本质就是一些操作和数据;最后是解释器模式,利用编程原理的方法,来更高层次的封装代码,将自己开发的java代码当做编译系统,从而不用改变java代码只修改更高语言层次的代码就能实现不同的功能。

发布了110 篇原创文章 · 获赞 8 · 访问量 6895

Guess you like

Origin blog.csdn.net/guorui_java/article/details/104026988