学习笔记之策略模式

策略(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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值