设计模式综述   4 - 迭代器和组合模式 5 - 策略模式 6 - 观察者模式 7 - 装饰者模式 8 - 命令模式 9 - 适配器模式 10 - 外观模式

《java23种设计模式总结》http://blog.csdn.net/lc2470348636/article/details/8935758

《设计模式大集锦 程序员面试全攻略》http://www.csdn.net/article/2012-06-04/2806324-software-design-interview-questions

====================================================================================================

设计模式:构建OO系统的隐含经验。

 

特性:可复用,可扩充,可维护。

 

模式:历经验证的OO设计经验。针对设计问题的通用解决方案。

思想:将变化的部分抽出来封装。

 

面向对象编程基本概念:1.抽象 2.封装 3.继承 4.多态

面向对象原则是目标,设计模式是做法。

经验复用:有人已经解决你的问题了。

 

1.找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起。(封装变化)

2.针对接口编程,而不是针对具体实现编程。(关键在多态)

 

3 - 模板方法模式

Head First设计模式》8.模板方法模式 P311

目标:封装算法块。

public abstract class CaffeineBeverage{

 void final prepareRecipe(){

  boilWater();

  brew();

  pourInCup();

  addCondinents();

 }

 

 abstract void brew();

 abstract void addCondinents();

 void boilWater(){

  //实现此函数

 }

 

}

模板方法模式细节:P325

钩子:作为条件控制,影响抽象类中的算法流程。

 

实例

要排序鸭子, 就必须实现compareTo()方法 P338

Arrays.sort(ducks);

或者Java API中 java.io中的read()方法。

 

P342 自己实现Swing JFrame中的paint()方法,通过覆盖paint()方法将自己代码插入JFrame算法中。

 

工厂方法是模板方法的一种特殊版本。

 

4 - 迭代器和组合模式

管理良好的集合。

P370 实现java.util.Iterator接口。

 

迭代器模式:

        提供一种方法,顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

5 - 策略模式

定义了算法族,并分别封装起来,让它们之间可以互相替换。

此模式让算法的变化独立于使用算法的客户。

 

6 - 观察者模式

让你的对象熟悉现状。

出版者+订阅者。

 

定义了对象之间的一对多依赖;这样一来,当一个对象改变状态的时候,它的所有依赖者都会受到通知并自动更新。

 

观察者模式哒代表:MVC模式。

 

Java API中java.util包中:Observer接口与Observable类。

 

——Java内置的观察者模式:

 

import java.util.Observerable;

import java.util.Observer;

//天气数据:被观察的对象。

public class WeatherData extends Observerable{

 private float temperature,humidity,pressure;

 public WeatherData(){}

 public void measurementsChanged(){

  setChanged();

  notifyObervers();

 }

 public void setMeasurements(float temperature){

  this.temperature = temperature;

  measurementsChanged();

 }

}

public class CurrentConditionsDisplay implements Observer,DisplayElement{

 Observerable observer;

 private float temperature,humidity;

 public CurrentConditionsDisplay(Observerable observable){

  this.observable = observable;

  observable.addObserver(this);

 }

 public void update(Observerable obs,Object arg){

  if(obs instanceof WeatherData){

   WeatherData weatherData = (WeatherData)obs;

   this.temperature = weatherData.getTemperature();

   ...

  }

 }

}

7 - 装饰者模式

 

Java类库中的例子》

 

 

 

 

 

 

 

使用对象组合的方式,在运行时装饰类。

给对象赋予新的职责。

 

设计原则:类应该对拓展开放,对修改关闭。拓展行为的OO设计技巧。

 

装饰者模式:动态地将责任附加到对象上。若要扩展功能,装饰者比继承更有弹性的替代方案。

 

public abstract class Beverage{

 String description = "Unkonw ....";

 public getDescription(){};

 public abstract double cost();

}

装饰者取代Beverge,获类型

public abstract class ComdimentDecorator extends Beverage{

 public abstract String getDescription();

}

写饮料的代码:

public class Espresso extends Beverage{

 public Espresso(){description = "Espresso"};

 public double cost(){

  return 1.99;

 }

}

写调料代码【摩卡:装饰者】

public class Mocha extends ComdimentDecorator{

 Beverage beverage;//组合

 public Mocha(Beverage bev){

  this.beverage = bev;

 }

 public String getDescription(){return beverage.getDescription()+"Mocha";}

 public double cost(){return 20+beverage.cost();}

}

 

缺点:

会造成大量的小类。

 

当介绍“工厂”和“生成器”模式时,将有更好的方式建立被装饰者对象。

8 - 命令模式

封装调用 P229

 

把方法调用method invocation 封装起来。

 

命令模式:将请求封装成对象,以便使用不同的请求,队列或者日志来参数化其他对象。

 

命令模式也支持可撤销的操作。

 

public class RemoteControlTest{

 public static void main(String[] args){

  SimpleRemoteControl remote = new GarageDoor();

  Light light = new Light();

  GarageDoor garageDoor = new GarageDoor();

 

  //下两行代码明显显示了如何将命令封装起来

  LightOnCommond lightOn = new LightOnCommond(light);

  GarageDoorOpenCommond garageOpen = new GarageDoorOpenCommond(garageDoor);

 

  //发出请求的对象

  remote.setCommond(lightOn);

  remote.buttonWasPressed();

  remote.setCommond(garageOpen);

  remote.buttonWasPressed();

 }

}

 ConcreteCommond 定义了动作和接收者之间的绑定关系。

 

 将发出请求的对象和执行请求的对象解耦。

 

public class MacroCommand implements Command{

 Command[] commands;

 public MacroCommand (Command[] commands){

  this.commands = commands;

 }

 public void execute(){

  for(int i=0;i<commands.length;i++){

   commands[i].execute();

  }

 }

}

Command[] partyOn = {LightOn,stereoOn,tvOn,hottubOn};

Command[] partyOff = {LightOff,stereoOff,tvOff,hottubOff};

MacroCommand partyOnMacro = new MacroCommand(partyOn);

remoteControl.setCommond(0,partyOnMacro,partyOffMarco);

 

9 - 适配器模式

将类的接口转换成想要的接口。

将对象包装起来以简化其接口。

 

适配器模式:将一个类的接口,转换成客户期望的另一个接口。

 

适配器让原来接口不兼容的类可以合作。

 

类适配器:多重继承。

对象适配器:利用组合的方式将请求传送给被适配器类。

 

10 - 外观模式

300页

外观模式:提供了一个统一的接口,用来访问子系统中的一群接口。

 

外观定义了一个高层接口,让子系统更容易使用。

 

“最少知识”原则:

减少对象之间的交互。

不要让太多的实体类耦合。