策略(Strategy)模式:定义了算法族,分别封装起来,让它们之间可以互相替换, 此模式让算法的变化独立于使用算法的客户。属于设计模式中对象行为型模式。
下面看代码:
[b]抽象类Duck.java[/b]
[code]
package org.kangsg219.abst;
import org.kangsg219.intf.FlyBehavior;
import org.kangsg219.intf.QuackBehavior;
public abstract class Duck {
public FlyBehavior flybehavior;
public QuackBehavior quackbehavior;
public Duck(){
}
public abstract void display();
public void performFly(){
flybehavior.fly();
}
public void setFlybehavior(FlyBehavior flybehavior) {
this.flybehavior = flybehavior;
}
public void setQuackbehavior(QuackBehavior quackbehavior) {
this.quackbehavior = quackbehavior;
}
public void performQuack(){
quackbehavior.quack();
}
public void swim(){
System.out.println("All ducks float,even decoys!");
}
}
[/code]
[b]duck具备两个行为:飞和叫;先分别定义接口再实现[/b]
[code]
package org.kangsg219.intf;
public interface FlyBehavior {
public void fly();
}
[/code]
[code]
package org.kangsg219.intf;
public interface QuackBehavior {
public void quack();
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.FlyBehavior;
public class FlyNoWay implements FlyBehavior {
public void fly() {
System.out.println("I can't fly!");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.FlyBehavior;
public class FlyWithRocket implements FlyBehavior {
public void fly() {
System.out.println("I'm flying whit rocket!");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.FlyBehavior;
public class FlyWithWings implements FlyBehavior {
public void fly() {
System.out.println("I'm flying with wings!");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.QuackBehavior;
public class MuteQuack implements QuackBehavior {
public void quack() {
System.out.println("<silence>");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.QuackBehavior;
public class Quack implements QuackBehavior {
public void quack() {
System.out.println("Quack......");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.QuackBehavior;
public class Squeak implements QuackBehavior {
public void quack() {
System.out.println("Squeak......");
}
}
[/code]
[b]看看效果[/b]
[code]
package org.kangsg219.test;
import org.kangsg219.abst.Duck;
import org.kangsg219.impl.FlyWithWings;
import org.kangsg219.impl.Quack;
public class MallardDuck extends Duck {
public MallardDuck(){
quackbehavior=new Quack();
flybehavior=new FlyWithWings();
}
@Override
public void display() {
System.out.println("I'm a real mallard duck!");
}
}
[/code]
[code]
package org.kangsg219.test;
import org.kangsg219.abst.Duck;
import org.kangsg219.impl.FlyWithRocket;
import org.kangsg219.impl.Quack;
public class MiniDuckSimulator {
public static void main(String[] args) {
System.out.println("mallard duck********************");
Duck mallard=new MallardDuck();
mallard.performFly();
mallard.performQuack();
mallard.swim();
mallard.display();
System.out.println("modle duck********************");
Duck modle=new ModleDuck();
modle.setFlybehavior(new FlyWithRocket());
modle.setQuackbehavior(new Quack());
modle.performFly();
modle.performQuack();
modle.swim();
modle.display();
}
}
[/code]
[code]
package org.kangsg219.test;
import org.kangsg219.abst.Duck;
public class ModleDuck extends Duck {
@Override
public void display() {
System.out.println("I'm a modle duck!");
}
}
[/code]
下面看代码:
[b]抽象类Duck.java[/b]
[code]
package org.kangsg219.abst;
import org.kangsg219.intf.FlyBehavior;
import org.kangsg219.intf.QuackBehavior;
public abstract class Duck {
public FlyBehavior flybehavior;
public QuackBehavior quackbehavior;
public Duck(){
}
public abstract void display();
public void performFly(){
flybehavior.fly();
}
public void setFlybehavior(FlyBehavior flybehavior) {
this.flybehavior = flybehavior;
}
public void setQuackbehavior(QuackBehavior quackbehavior) {
this.quackbehavior = quackbehavior;
}
public void performQuack(){
quackbehavior.quack();
}
public void swim(){
System.out.println("All ducks float,even decoys!");
}
}
[/code]
[b]duck具备两个行为:飞和叫;先分别定义接口再实现[/b]
[code]
package org.kangsg219.intf;
public interface FlyBehavior {
public void fly();
}
[/code]
[code]
package org.kangsg219.intf;
public interface QuackBehavior {
public void quack();
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.FlyBehavior;
public class FlyNoWay implements FlyBehavior {
public void fly() {
System.out.println("I can't fly!");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.FlyBehavior;
public class FlyWithRocket implements FlyBehavior {
public void fly() {
System.out.println("I'm flying whit rocket!");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.FlyBehavior;
public class FlyWithWings implements FlyBehavior {
public void fly() {
System.out.println("I'm flying with wings!");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.QuackBehavior;
public class MuteQuack implements QuackBehavior {
public void quack() {
System.out.println("<silence>");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.QuackBehavior;
public class Quack implements QuackBehavior {
public void quack() {
System.out.println("Quack......");
}
}
[/code]
[code]
package org.kangsg219.impl;
import org.kangsg219.intf.QuackBehavior;
public class Squeak implements QuackBehavior {
public void quack() {
System.out.println("Squeak......");
}
}
[/code]
[b]看看效果[/b]
[code]
package org.kangsg219.test;
import org.kangsg219.abst.Duck;
import org.kangsg219.impl.FlyWithWings;
import org.kangsg219.impl.Quack;
public class MallardDuck extends Duck {
public MallardDuck(){
quackbehavior=new Quack();
flybehavior=new FlyWithWings();
}
@Override
public void display() {
System.out.println("I'm a real mallard duck!");
}
}
[/code]
[code]
package org.kangsg219.test;
import org.kangsg219.abst.Duck;
import org.kangsg219.impl.FlyWithRocket;
import org.kangsg219.impl.Quack;
public class MiniDuckSimulator {
public static void main(String[] args) {
System.out.println("mallard duck********************");
Duck mallard=new MallardDuck();
mallard.performFly();
mallard.performQuack();
mallard.swim();
mallard.display();
System.out.println("modle duck********************");
Duck modle=new ModleDuck();
modle.setFlybehavior(new FlyWithRocket());
modle.setQuackbehavior(new Quack());
modle.performFly();
modle.performQuack();
modle.swim();
modle.display();
}
}
[/code]
[code]
package org.kangsg219.test;
import org.kangsg219.abst.Duck;
public class ModleDuck extends Duck {
@Override
public void display() {
System.out.println("I'm a modle duck!");
}
}
[/code]