策略模式定义了算法族,分别封装起来,让他们可以相互替换,此模式让算法的变化独立于使用算法的客户。
继承,相似之处用继承,假如如干个功能点需要修改,代码难以维护,
原始代码
public class Duck { //鸭子描述 public void dispaly(){ System.out.println("描述"); } //鸭子叫 public void quack(){ System.out.println("会叫"); } }
public class MallarDuck extends Duck{ public static void main(String[] args) { Duck duck=new Duck(); duck.dispaly(); duck.fly(); duck.quack(); } }
假如Duck要添加新的行为鸭子飞,那代码难以维护,比如有的鸭子会飞有的不会飞。
假如继承或者实现接口,这二种都依赖于实现,我们会被绑的死死的,没有办法更改更多行为。
接口,针对接口编程,而不是针对实现编程,需要修改干个功能点使用接口,实现方式灵活多变。
更改后代码:package com.base; import com.interfaces.FlyBehavior; import com.interfaces.QuackBehavior; /** * 将大功能点分为接口小模块,接口为了小功能点有弹性, * @author Hadoop * */ public abstract class Duck { protected FlyBehavior flyBehavior; protected QuackBehavior quackBehavior; public void setFlyBehavior(FlyBehavior flyBehavior){ this.flyBehavior=flyBehavior; } public void QuackBehavior(QuackBehavior quackBehavior){ this.quackBehavior=quackBehavior; } public abstract void dispaly();//鸭子描述 /** * 委托给行为类 */ public void performFly(){//鸭子飞 flyBehavior.fly(); } public void performQuack(){//鸭子叫 quackBehavior.quack(); } }
package com.interfaces; /** * 行为类(飞) * @author Hadoop * */ public interface FlyBehavior { void fly(); }
package com.interfaces; /** * 鸭子叫 * @author Hadoop * */ public interface QuackBehavior { void quack(); }
package com.interfaces.impl; import com.interfaces.FlyBehavior; public class FlyNoWay implements FlyBehavior { public void fly() { System.out.println("鸭子不会飞行"); } }
package com.interfaces.impl; import com.interfaces.FlyBehavior; public class FlyRocketPowered implements FlyBehavior { public void fly() { // TODO Auto-generated method stub System.out.println("助力器"); } }
package com.interfaces.impl; import com.interfaces.FlyBehavior; public class FlyWithWings implements FlyBehavior { public void fly() { System.out.println("鸭子飞行"); } }
package com.interfaces.impl; import com.interfaces.QuackBehavior; public class Quack implements QuackBehavior { public void quack() { // TODO Auto-generated method stub System.out.println("橡皮鸭子叫"); } }
package com.interfaces.impl; import com.interfaces.QuackBehavior; public class Squack implements QuackBehavior { public void quack() { // TODO Auto-generated method stub System.out.println("鸭子叫"); } }
package com; import com.base.Duck; import com.interfaces.impl.FlyNoWay; import com.interfaces.impl.FlyWithWings; import com.interfaces.impl.Quack; public class MallarDuck extends Duck{ public MallarDuck(){ quackBehavior=new Quack(); flyBehavior=new FlyNoWay(); } public void dispaly() { System.out.println("描述"); } }
import java.io.IOException; import com.MallarDuck; import com.interfaces.impl.FlyRocketPowered; public class test { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { MallarDuck mallarDuck=new MallarDuck(); mallarDuck.dispaly(); mallarDuck.setFlyBehavior(new FlyRocketPowered()); mallarDuck.performFly(); mallarDuck.performQuack(); } }
策略模式(headfirst设计模式学习笔记),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u011153869/article/details/38118921