Latest web development tutorials

Introduction to Design Patterns

Design patterns (Design pattern) represents a best practice, it is often adopted by experienced object-oriented software developers. Design patterns are solutions to common problems of software developers in the software development process in the face. These solutions are many software developers through quite a long period of trial and error summed up.

Design patterns are set to be used repeatedly, most people know, after cataloging, code design experience summary. Use design patterns to reuse code, make the code easier to understand others, to ensure the reliability of the code. Undoubtedly, design patterns have on others in the system are win-win, design patterns makes code compiled real engineering, design patterns is the cornerstone of software engineering, as building a brick stone same. Project rational use of design patterns can be the perfect solution to many problems, each mode corresponding to the principle of reality have corresponding, each mode describes a constantly recurring problem around us, as well as the problem core solution, which is widely used design patterns that can be the reason.

What is the GOF (Gang of Four, spelling Gang of Four)?

1994 by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides four co-published a book calledDesign Patterns - Elements of Reusable Object- Oriented Software ( Chinese translation: Design Patterns - Reusable Object-Oriented Software element)the book that first mentioned the concept of software development design patterns.

Four authors collectivelyGOF (Gang of Four, spelling Gang of Four).They proposed design model is mainly based on object-oriented design principles.

  • Rather than programming interfaces for programming.
  • Priority object composition rather than inheritance.

The use of design patterns

Design Patterns in software development for two main purposes.

Common platform developers

Design pattern provides a standard terminology system, and in particular to a specific scene. For example, a single design pattern implies the use of a single object, so that all are familiar with singleton design pattern developers can use a single object, and can tell each other this way, the program uses the singleton pattern.

Best Practice

Design patterns have evolved over a long period of time, they provide the best solutions to common problems faced by the software development process. These learning mode helps inexperienced developer through a quick and easy way to learn the software design.

Types of design patterns

According to the reference book of design patternsDesign Patterns - Elements of Reusable Object- Oriented Software ( Chinese translation: Design Patterns - Object-oriented software elements reusable)mentioned, a total of 23 design patterns. These patterns can be divided into three categories: create schema (Creational Patterns), structural model (Structural Patterns), type of model (Behavioral Patterns). Of course, we will discuss another type of design patterns: J2EE design patterns.

序号模式 & 描述包括
1创建型模式
这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用新的运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。
  • 工厂模式(Factory Pattern)
  • 抽象工厂模式(Abstract Factory Pattern)
  • 单例模式(Singleton Pattern)
  • 建造者模式(Builder Pattern)
  • 原型模式(Prototype Pattern)
2结构型模式
这些设计模式关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。
  • 适配器模式(Adapter Pattern)
  • 桥接模式(Bridge Pattern)
  • 过滤器模式(Filter、Criteria Pattern)
  • 组合模式(Composite Pattern)
  • 装饰器模式(Decorator Pattern)
  • 外观模式(Facade Pattern)
  • 享元模式(Flyweight Pattern)
  • 代理模式(Proxy Pattern)
3行为型模式
这些设计模式特别关注对象之间的通信。
  • 责任链模式(Chain of Responsibility Pattern)
  • 命令模式(Command Pattern)
  • 解释器模式(Interpreter Pattern)
  • 迭代器模式(Iterator Pattern)
  • 中介者模式(Mediator Pattern)
  • 备忘录模式(Memento Pattern)
  • 观察者模式(Observer Pattern)
  • 状态模式(State Pattern)
  • 空对象模式(Null Object Pattern)
  • 策略模式(Strategy Pattern)
  • 模板模式(Template Pattern)
  • 访问者模式(Visitor Pattern)
4J2EE 模式
这些设计模式特别关注表示层。这些模式是由 Sun Java Center 鉴定的。
  • MVC 模式(MVC Pattern)
  • 业务代表模式(Business Delegate Pattern)
  • 组合实体模式(Composite Entity Pattern)
  • 数据访问对象模式(Data Access Object Pattern)
  • 前端控制器模式(Front Controller Pattern)
  • 拦截过滤器模式(Intercepting Filter Pattern)
  • 服务定位器模式(Service Locator Pattern)
  • 传输对象模式(Transfer Object Pattern)

Here with a whole picture to describe the relationship between design patterns:

设计模式之间的关系

Six principles of design patterns

1. Open Closed Principle (Open Close Principle)

Open Closed Principle means: open for extension, but closed for modification. When the program needs to expand, not to modify the original code to achieve the effect of a hot-swappable. In short, in order to make the program's scalability, ease of maintenance and upgrades. Want to achieve this effect, we need to use interfaces and abstract classes, concrete design later we will mention this point.

2, Richter substitution principle (Liskov Substitution Principle)

Richter substitution principle is one of the basic principles of object-oriented design. Richter substitution principle that where any base class can appear, subclasses will be able to appear. LSP is inherited cornerstone multiplexed only when the derived class can replace the base class, and functional software unit is not affected, the base class can really be reused, and the derived class can be the basis of the base class adds new behavior. Richter substitution principle is the principle of complementarity of opening and closing. The key step is the realization of the principle of opening and closing abstraction, inheritance and base class and subclass of the abstract concrete realization, so Richter substitution principle is abstract concrete steps to implement the specification.

3. Dependency Inversion principle (Dependence Inversion Principle)

This principle is the basis of the principle of opening and closing, the specific content: For programming interfaces, depending on the abstract does not depend on specific.

4, Interface Segregation Principle (Interface Segregation Principle)

Meaning of this principle is: the use of multiple isolated interface is better than using a single interface. It also has another meaning: to reduce the degree of coupling between classes. Thus, the fact that design patterns starting from large-scale software architecture, easy to upgrade and maintain the software design, which emphasizes reducing dependence, reducing the coupling.

5, Demeter, also known as the least known principles (Demeter Principle)

Minimum know principle means: an entity should interact as little as possible between other entities, making the system relatively independent function modules.

6, synthesis multiplexing principles (Composite Reuse Principle)

Synthesis multiplexing refers to the principle: as much as possible the use of synthetic / polymeric way, instead of using inheritance.