策略模式定义了算法簇,分别封装起来,让他们可以互相替换,此模式让算法的变化独立于使用算法的使用者。

系统描述:各种不同的鸭子,有的可以飞,有点不可以飞,不同的鸭子叫声也不一定相同。

定义一个鸭子的超类Duck,其他具体的鸭子继承该超类,将鸭子的飞的行为和叫的行为分别封装到接口中

鸭子的超类:

package cn.lan.duck;

import cn.lan.duck.behavior.FlyBehavior;
import cn.lan.duck.behavior.QuackBehavior;
/**
 * 鸭子的超类
 * @author wy
 *
 */
public abstract class Duck {

	//飞行行为的接口
	FlyBehavior flyBehavior;
	//叫声行为的接口
	QuackBehavior quackBehavior;

	public Duck() {

	}
	//执行飞行为的方法
	public void performFly() {
		flyBehavior.fly();
	}
	//执行叫行为的方法
	public void performQuack() {
		quackBehavior.quack();
	}
	//执行游泳的方法
	public void swim() {
		System.out.println("all ducks can swim....");
	}
	
	public void setFlyBehavior(FlyBehavior flyBehavior) {
		this.flyBehavior = flyBehavior;
	}

	public void setQuackBehavior(QuackBehavior quackBehavior) {
		this.quackBehavior = quackBehavior;
	}
}
飞行行为的接口

package cn.lan.duck.behavior;
/**
 * 飞行行为的接口
 * @author wy
 *
 */
public interface FlyBehavior {

	public void fly();
}
叫声的接口

package cn.lan.duck.behavior;
/**
 * 叫行为的接口
 * @author wy
 *
 */
public interface QuackBehavior {

	public void quack();
}
具体的实现类

package cn.lan.duck.behavior.impl;

import cn.lan.duck.behavior.FlyBehavior;
/**
 * 飞行行为的实现类:不能飞
 * @author wy
 *
 */
public class FlyNoWays implements FlyBehavior {

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("can not fly...");
	}

}

package cn.lan.duck.behavior.impl;

import cn.lan.duck.behavior.FlyBehavior;
/**
 * 飞行行为的实现类:能飞
 * @author wy
 *
 */
public class FlyWithWins implements FlyBehavior {

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("flying .....");
	}

}

package cn.lan.duck.behavior.impl;

import cn.lan.duck.behavior.QuackBehavior;
/**
 * 叫行为的实现类:mute
 * @author wy
 *
 */
public class MuteQuack implements QuackBehavior {

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println("no sound...");
	}

}

package cn.lan.duck.behavior.impl;

import cn.lan.duck.behavior.QuackBehavior;
/**
 * 叫行为的实现类:quack
 * @author wy
 *
 */
public class Quack implements QuackBehavior {

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println("quack...");
	}

}
package cn.lan.duck.behavior.impl;

import cn.lan.duck.behavior.QuackBehavior;
/**
 * 叫行为的实现类:squeak
 * @author wy
 *
 */
public class SqueakQuack implements QuackBehavior {

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println("squeak...");
	}

}
2种飞行实现类和3种叫实现类,再写3种不同的鸭子,每个鸭子的飞和叫的行为都不同,在他们各自的构造方法中绑定好了


package cn.lan.duck;

import cn.lan.duck.behavior.impl.FlyWithWins;
import cn.lan.duck.behavior.impl.Quack;

public class FirstDuck extends Duck {
	public FirstDuck() {
		quackBehavior = new Quack();
		flyBehavior = new FlyWithWins();
	}
}
package cn.lan.duck;

import cn.lan.duck.behavior.impl.FlyNoWays;
import cn.lan.duck.behavior.impl.MuteQuack;

public class SecondDuck extends Duck {
	public SecondDuck() {
		quackBehavior = new MuteQuack();
		flyBehavior = new FlyNoWays();
	}
}
package cn.lan.duck;

import cn.lan.duck.behavior.impl.FlyWithWins;
import cn.lan.duck.behavior.impl.SqueakQuack;

public class ThirdDuck extends Duck {
	public ThirdDuck() {
		quackBehavior = new SqueakQuack();
		flyBehavior = new FlyWithWins();
	}
}
再写个测试类:
package test;

import cn.lan.duck.Duck;
import cn.lan.duck.FirstDuck;
import cn.lan.duck.SecondDuck;
import cn.lan.duck.ThirdDuck;
import cn.lan.duck.behavior.impl.FlyNoWays;
import cn.lan.duck.behavior.impl.FlyWithWins;

public class DuckTest {

	public static void main(String[] args) {
		System.out.println("first duck:");

		Duck firstDuck = new FirstDuck();
		firstDuck.performFly();
		firstDuck.performQuack();
		firstDuck.swim();

		System.out.println();
		System.out.println("second duck:");

		Duck secondDuck = new SecondDuck();
		secondDuck.performFly();
		secondDuck.performQuack();
		secondDuck.swim();

		System.out.println();
		System.out.println("third duck:");

		Duck thirdDuck = new ThirdDuck();
		thirdDuck.performFly();
		thirdDuck.performQuack();
		thirdDuck.swim();
		System.out.println("change third duck......");
		thirdDuck.setFlyBehavior(new FlyNoWays());
		thirdDuck.performFly();
	}
}

这样就把各种行为和Duck类分开了,即使再添加一个新的鸭子类,只需要在这个新的鸭子类中的构造方法中添加属于它的行为即可;

即使在新增一个鸭子行为,也只需要添加一个新的行为接口,然后在需要添加该行为的鸭子类中加上该接口即可;不会因为新增一个行为,

导致大面积更改代码。


输出结果为:

first duck:
flying .....
quack...
all ducks can swim....

second duck:
can not fly...
no sound...
all ducks can swim....

third duck:
flying .....
squeak...
all ducks can swim....
change third duck......
can not fly...






posted on 2013-12-18 15:48  wyang0126  阅读(149)  评论(0编辑  收藏  举报