Design Patterns Overview

来源:互联网 发布:免费博客绑定域名 编辑:程序博客网 时间:2024/05/21 17:30

Design Patterns Overview

目录

    • Design Patterns Overview
    • 目录
    • 什么是GoF
    • 用途
    • 划分依据
    • 具体分类1
    • Summary


什么是GoF?

提到设计模式,就不得不提到GoF。
1994年,Erich Gamma, Richard Helm, Ralph Johnson 和 John Vlissides
出版了一本名叫Design Patterns - Elements of Reusable Object-Oriented Software 的书,而这恐怕也是设计模式这个词的起源了。
四位牛哄哄的大侠从此有了响当当的名号——GoF(Gang of Four,四人帮!)

根据四位大神的大作,设计模式基本上基于以下OOD的原则:

  • 面向接口编程而不是面向实现编程
  • 优先使用组合而不是继承

用途

在软件开发中,设计模式主要用于以下两方面:

  • 开发者之间的交流

    设计模式提供了一些通用的术语,并且每种模式都适用于特定的场景。这样开发者交流起来就方便多了。比如一个人说我的代码用了单例模式,那么其他人不用追究细节就知道他是怎么实现的了(我觉得这个是最重要的)。

  • 最佳实践

    设计模式已经被证明在软件开发过程中能够为一些问题提供最好的解决方案。并且学习设计模式会让经验不足的开发者简单快速的了解软件设计。


划分依据

参考Design Patterns - Elements of Reusable Object-Oriented Software,设计模式可以分为23种,共3大类。

Creational, Structural and Behavioral patterns。

当然还有其他的分类。

J2EE design patterns

  • Creational Patterns

    划分依据:创建对象

    这类模式关注的是对象的创建,并且在创建对象的同时隐藏创建的逻辑,而不是直接使用new操作符。这样对象的创建就更加灵活了,可以根据给定的场景来创建合适的对象。

  • Structural Patterns

    划分依据:类和对象的组合

    这类模式关注的是类和对象的组合。通过不同的组合带来更多功能上的变化。

  • Behavioral Patterns

    划分依据:对象之间的交互

    这类模式关注的是对象与对象之间的交互(communication)。

  • J2EE Patterns

    划分依据:用于Java EE


具体分类1

CreationalPatterns Description Abstract Factory Sets of methods to make various objects. Builder Make and return one object various ways. Factory Methods to make and return components of one object various ways. Prototype Make new objects by cloning the objects which you set as prototypes. Singleton A class distributes the only instance of itself.
StructuralPatterns Description Adapter A class extends another class, takes in an object, and makes the taken object behave like the extended class. Bridge An abstraction and implementation are in different class hierarchies. Composite Assemble groups of objects with the same signature. Decorator One class takes in another class, both of which extend the same abstract class, and adds functionality. Facade One class has a method that performs a complex process calling several other classes. Flyweight The reusable and variable parts of a class are broken into two classes to save resources. Proxy One class controls the creation of and access to objects in another class.
BehavioralPatterns Description Chain of Resp. A method called in one class can move up a hierarchy to find an object that can properly execute the method. Command An object encapsulates everything needed to execute a method in another object. Interpreter Define a macro language and syntax, parsing input into objects which perform the correct opertaions. Iterator One object can traverse the elements of another object. Mediator An object distributes communication between two or more objects. Memento One object stores another objects state. Observer An object notifies other object(s) if it changes. State An object appears to change its` class when the class it passes calls through to switches itself for a related class. Strategy An object controls which of a family of methods is called. Each method is in its` own class that extends a common base class. Template An abstract class defines various methods, and has one non-overridden method which calls the various methods. Visitor One or more related classes have the same method, which calls a method specific for themselves in another class.

Summary

关于设计模式的描述最好还是看英文文档,中文的很多说明都不是很清楚或者说不是很准确,因为中文讲究的是意会,并不是很注重语法和精确性。而设计模式中有些模式之间的差别真的需要仔细甄别才能够体会出来,所以还是建议看英文文档。


  1. http://www.fluffycat.com/Java-Design-Patterns/ ↩
0 0