Strategy Pattern

阅读更多

买的第一本head first 系列的书---Head First Design Patterns 终于开始看了,今天看完了第一个模式:Strategy Pattern:定义的算法簇,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户端.

     书中的例子挺经典的,看完这一章后,把书中的例子自己写了遍,练习了策略模式的使用,下面是代码:

鸭子类,有会飞,会叫等一系列行为:

package pro.pattern.strategy.example.duck;

import pro.pattern.strategy.example.duck.flybehavior.FlyBehavior;
import pro.pattern.strategy.example.duck.quackbehavior.QuackBehavior;

public abstract class Duck {
	FlyBehavior flyBehavior;
	
	QuackBehavior quackBehavior;
	
	public Duck(){
		
	}
	
	public abstract void display() ;
	
	public void performFly() {
		flyBehavior.fly();
	}
	
	public void performQuack(){
		quackBehavior.quack();
	}
	
	public void setFlyBehavior(FlyBehavior fly){
		flyBehavior = fly;
	}
	
	public void setQuackBehavior(QuackBehavior quack){
		quackBehavior = quack;
	}
}

 由于鸭子的种类很多(橡皮鸭,真鸭等,)其飞行,叫的行为不尽相同,可以将飞行和叫声分别定义为算法簇:

 飞行行为:

//封装飞行行为
package pro.pattern.strategy.example.duck.flybehavior;

public interface FlyBehavior {
	public void fly();
}


package pro.pattern.strategy.example.duck.flybehavior;

public class FlyNoWay implements FlyBehavior {

	@Override
	public void fly() {
		System.out.println("I can't fly!");
	}

}


package pro.pattern.strategy.example.duck.flybehavior;

public class FlyWithWings implements FlyBehavior{

	@Override
	public void fly() {
		System.out.println("I'm flying!");
	}

}

 

叫声行为:

package pro.pattern.strategy.example.duck.quackbehavior;

public interface QuackBehavior {

	public void quack();

}


package pro.pattern.strategy.example.duck.quackbehavior;

public class Quack implements QuackBehavior {

	@Override
	public void quack() {
		System.out.println("Quack");
	}

}


package pro.pattern.strategy.example.duck.quackbehavior;

public class MuteQuack implements QuackBehavior {

	@Override
	public void quack() {
		System.out.println("Mute Quack");
	}

}


package pro.pattern.strategy.example.duck.quackbehavior;

public class Squack implements QuackBehavior {

	@Override
	public void quack() {
		System.out.println("Squack");
	}

}

 定义完算法簇后,就可以定义一个具体的鸭子(duck)类:

package pro.pattern.strategy.example.duck;

import pro.pattern.strategy.example.duck.flybehavior.FlyNoWay;
import pro.pattern.strategy.example.duck.quackbehavior.Squack;

public class ModelDuck extends Duck {

	@Override
	public void display() {
		System.out.println("Just a strategy pattern demo!");
	}
	
	public ModelDuck () {//用组合,不用继承
		flyBehavior = new FlyNoWay();
		
		quackBehavior = new Squack();
	}

}

 

测试:

package pro.pattern.strategy.example.duck;

import pro.pattern.strategy.example.duck.flybehavior.FlyWithWings;
import pro.pattern.strategy.example.duck.quackbehavior.MuteQuack;

public class Test {
	public static void main(String[] args){
		Duck duck = new ModelDuck();
		
		duck.performFly();
		duck.performQuack();
		//运行时动态设定行为
		duck.setFlyBehavior(new FlyWithWings());
		duck.setQuackBehavior(new MuteQuack());
		
		duck.performFly();
		duck.performQuack();
	}

}

  在书中学到的设计原则:

   封装变化

   多用组合,少用继承

你可能感兴趣的:(算法,设计模式)