Design Pattern 简明教程
Design Pattern - Overview
设计模式代表了经验丰富的面向对象软件开发人员所使用的最佳实践。设计模式是软件开发人员在软件开发过程中面对的一般问题的解决方案。这些解决方案是由众多软件开发人员经过相当长一段时间通过反复试验获得的。
Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.
What is Gang of Four (GOF)?
1994 年,四位作者 Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides 出版了一本名为 Design Patterns - Elements of Reusable Object-Oriented Software 的书,它开创了软件开发中的设计模式概念。
In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development.
这些作者被统称为 Gang of Four (GOF) 。根据这些作者,设计模式主要基于面向对象设计下列原则。
These authors are collectively known as Gang of Four (GOF). According to these authors design patterns are primarily based on the following principles of object orientated design.
-
Program to an interface not an implementation
-
Favor object composition over inheritance
Usage of Design Pattern
设计模式在软件开发中有两种主要用法。
Design Patterns have two main usages in software development.
Common platform for developers
设计模式提供一个标准术语,并且针对特定场景。例如,单例设计模式表示使用单一对象,因此,所有熟悉单一设计模式的开发人员都将使用单一对象,并且他们可以互诉该程序遵循单例模式。
Design patterns provide a standard terminology and are specific to particular scenario. For example, a singleton design pattern signifies use of single object so all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.
Best Practices
设计模式是长期演变的结果,它们为软件开发中面临的特定问题提供了最佳解决方案。学习这些模式有助于经验不足的开发者以一种简单快速的方式学习软件设计。
Design patterns have been evolved over a long period of time and they provide best solutions to certain problems faced during software development. Learning these patterns helps unexperienced developers to learn software design in an easy and faster way.
Types of Design Patterns
如设计模式参考书 Design Patterns - Elements of Reusable Object-Oriented Software 所述,有 23 种设计模式,可分为三类:创建型模式、结构型模式和行为型模式。我们还将讨论另一类设计模式:J2EE 设计模式。
As per the design pattern reference book Design Patterns - Elements of Reusable Object-Oriented Software , there are 23 design patterns which can be classified in three categories: Creational, Structural and Behavioral patterns. We’ll also discuss another category of design pattern: J2EE design patterns.
S.N. |
Pattern & Description |
1 |
Creational Patterns These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case. |
2 |
Structural Patterns These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities. |
3 |
Behavioral Patterns These design patterns are specifically concerned with communication between objects. |
4 |
J2EE Patterns These design patterns are specifically concerned with the presentation tier. These patterns are identified by Sun Java Center. |