首页 > 其他 > 详细

设计模式之策略模式

时间:2014-05-22 23:19:57      阅读:467      评论:0      收藏:0      [点我收藏+]

策略模式定义:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。

简单定义:封装可以互换的行为,并使用委托来决定要使用哪一个。

OO原则:

封装变化

多用组合,少用继续。

针对接口编程,不针对实现编程。

以书中的Duck为例:

Duck.java(抽象类,performFly()与performQuack()函数的实现委托给行为类,行为由子类设置,也可以在运行时动态设置。display()为抽象函数,由子类实现。)

package headfirst.strategy;

public abstract class Duck {
	FlyBehavior flyBehavior;
	QuackBehavior quackBehavior;
 
	public Duck() {
	}
 
	public void setFlyBehavior (FlyBehavior fb) {
		flyBehavior = fb;
	}
 
	public void setQuackBehavior(QuackBehavior qb) {
		quackBehavior = qb;
	}
 
	abstract void display();
 
	public void performFly() {
		flyBehavior.fly();
	}
 
	public void performQuack() {
		quackBehavior.quack();
	}
 
	public void swim() {
		System.out.println("All ducks float, even decoys!");
	}
}

MallardDuck.java(具体Duck类,实例化行为类和display)

package headfirst.strategy;

public class MallardDuck extends Duck {
 
	public MallardDuck() {
		quackBehavior = new Quack();
                flyBehavior = new FlyWithWings();
	}
 
	public void display() {
		System.out.println("I'm a real Mallard duck");
	}
}

ModelDuck.java

package headfirst.strategy;

public class ModelDuck extends Duck {
	public ModelDuck() {
		flyBehavior = new FlyNoWay();
		quackBehavior = new Quack();
	}

	public void display() {
		System.out.println("I'm a model duck");
	}
}


FlyBehavior.java(Fly行为抽象类)

package headfirst.strategy;

public interface FlyBehavior {
	public void fly();
}

FlyWithWings.java(Fly行为具体类)

package headfirst.strategy;

public class FlyWithWings implements FlyBehavior {
	public void fly() {
		System.out.println("I'm flying!!");
	}
}

FlyNoWay.java

package headfirst.strategy;

public class FlyNoWay implements FlyBehavior {
	public void fly() {
		System.out.println("I can't fly");
	}
}


FlyRocketPowered.java

package headfirst.strategy;

public class FlyRocketPowered implements FlyBehavior {
	public void fly() {
		System.out.println("I'm flying with a rocket");
	}
}

QuackBehavior.java(叫声行为抽象类)

package headfirst.strategy;

public interface QuackBehavior {
	public void quack();
}


Quack.java(叫声行为具体类)

package headfirst.strategy;

public class Quack implements QuackBehavior {
	public void quack() {
		System.out.println("Quack");
	}
}

MuteQuack.java

package headfirst.strategy;

public class MuteQuack implements QuackBehavior {
	public void quack() {
		System.out.println("<< Silence >>");
	}
}

Squeak.java

package headfirst.strategy;

public class Squeak implements QuackBehavior {
	public void quack() {
		System.out.println("Squeak");
	}
}

测试类:

MiniDuckSimulator.java

package headfirst.strategy;

public class MiniDuckSimulator {
 
	public static void main(String[] args) {
 
		Duck mallard = new MallardDuck();
		mallard.performQuack();
		mallard.performFly();
   
		Duck model = new ModelDuck();
		model.performFly();
		model.setFlyBehavior(new FlyRocketPowered());
		model.performFly();

	}
}

输出:

Quack
I'm flying!!
I can't fly
I'm flying with a rocket







设计模式之策略模式,布布扣,bubuko.com

设计模式之策略模式

原文:http://blog.csdn.net/viviwen123/article/details/26473799

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!