---
title: "Design Patterns and OOP"
author: "CherryRemind"
date: "2007-10-23T10:49:35Z"
tags: ["OO", "OOP", "prototype", "算法", "设计模式"]
slug: "design-patterns-and-oop"
draft: false
lastmod: "2007-10-23T10:49:35Z"
id: 134674
url: "https://todjiang.iteye.com/blog/134674"
---

# Design Patterns

  A design pattern is a recurring solution to a standard problem.

The most well-known design patterns (in total 23) were documented by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides also called The Gang of Four (GOF) in the book Design Patterns -- Elements of Reusable Software (1995).

There a hunderds of design patterns described in the literature. This tutorial only describes the design patterns documented by the GOF.

The GOF has divided the design patterns into three types.

Type name Description [Creational patterns](http://www.mobilefish.com/tutorials/design_patterns/design_patterns.html#creational_patterns) Deal with initializing and configuring classes and objects

- Abstract Factory
- Builder Factory
- Factory Method
- Prototype Factory
- Singleton

[Structural patterns](http://www.mobilefish.com/tutorials/design_patterns/design_patterns.html#structural_patterns) Deal with decoupling interface and implementation of classes and objects

- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy

[Behavioral patterns](http://www.mobilefish.com/tutorials/design_patterns/design_patterns.html#behavioral_patterns) Deal with dynamic interactions among societies of classes and objects

- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template
- Visitor

[Creational patterns.  
\
Name Description Factory Method Method in a derived class creates associates Abstract Factory Factory for building related objects Builder Factory for building complex objects incrementally Prototype Factory for cloning new instances from a prototype Singleton Factory for a singular (sole) instance]()

 

 

 

 

 

knowledge of pattern  
#  creational  
1\. factory method: lazy initialize specific instance

2\. abstract factory: supply a interface for the system

3\. builder: builder or create a object apart from its descrition

4\. prototype: create new object which type base on the type of specific instance

5\. singleton: make sure one class only have one instance, supply for every visitor  
 allways used with factory pattern together.

\# structural  
6\. adapter: give a interface of class for get another required interface, implement compatibility

7\. bridge: extranet abstract class from implement class, make it suit for their single change  
public abtract class AbstractRoot  
{  
    operation();  
}

8\. composite: make a object as tree structure, make "part-all" clearly. 

组合模式:  ----继承

9\. decorator: change extend(8) to delegate

装饰器模式: --- 委托

10\. facade: supply a interface for visiting the sub-system

11\. flyweight: it seem logic like cache.  
     if (flyweight\[key] exists)  
     {  
        return existing flyweight;  
     }  
     else  
     {  
        create new flyweight;  
        add it to pool of flyweights;  
        return the new flyweight;  
     }  
       
12\. proxy:   a, remote proxy       visit  
             b, virtual proxy      performance  
             c, protection proxy   control  
             d, smart reference    get reference number  
               
\# behavioral  
13\. interpreter 解释模式

14\. Template Method 模版模式   
AIM: 定义一个操作中的算法的骨架，而将一些步骤延迟到子类中。  
ENV: 一次性实现一个算法的不变的部分，并将可变的行为留给子类来实现.  
TODO: 比较组合模式  
   
15\. Chain of Responsibility  
AIM: 使多个对象都有机会处理请求，从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链，并沿着这条链传递该请求，直到有一个对象处理它为止.  
有多个的对象可以处理一个请求，哪个对象处理该请求运行时刻自动确定。  
你想在不明确指定接收者的情况下，向多个对象中的一个提交一个请求。  
可处理一个请求的对象集合应被动态指定。

16\. Command 命令模式  
将一个请求封装为一个对象，从而使你可用不同的请求对客户进行参数化；对请求排队或记录请求日志，以及支持可撤消的操作。

17\. Iterator 迭代模式

18\. Mediator 仲裁模式

19\. Memento  记忆模式

20\. Observer 观察者模式

21\. State 状态模式  
允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。

22\. Strategy 策略模式  
TODO: 比较State  
定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。

23\. Vistor 访问模式

 

  &lt;head first ooad&gt;

感觉OO原则其实我们平时编码的最基本原则, 设计模式可以就是遵循的OO原则来运用的.  
OO principle  
#1 the open-closed principle(ocp)  
classed shoulde be open for extension, and closed for modification.

#2 the don't repeat yourself principle (DRY)  
have each piece of imformation and behavior in your system in a single,sensible place.

#3 the single responsibility principle(SRP)  
Single responsibility priciple correctly when each of your objects has only one reason to change

#4 the Liskov substitution priinciple(LSP)  
感觉LSP就是用委托....