Java设计模式之策略模式代码示例



策略设计模式:



Dock超类:

package com.hycz.design.pattern.strategy;

import com.hycz.design.pattern.strategy.behavioral.FlyBehavior;
import com.hycz.design.pattern.strategy.behavioral.QuackBehavior;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 10:13 AM
 * To change this template use File | Settings | File Templates.
 */
public abstract class Duck {

    public FlyBehavior flyBehavior;
    public QuackBehavior quackBehavior;

    public void setFlyBehavior(FlyBehavior flyBehavior) {
        this.flyBehavior = flyBehavior;
    }

    public void setQuackBehavior(QuackBehavior quackBehavior) {
        this.quackBehavior = quackBehavior;
    }

    public abstract void dispaly();

    public void swim(){
        System.out.printf("All duck float, even decoys!");
    }
    public void performFly(){
        flyBehavior.fly();

    }
    public void performQuack(){
        quackBehavior.quack();
    }



}

package com.hycz.design.pattern.strategy.behavioral;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 10:16 AM
 * To change this template use File | Settings | File Templates.
 */
public interface FlyBehavior {

    public void fly();
}

package com.hycz.design.pattern.strategy.behavioral;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 10:49 AM
 * To change this template use File | Settings | File Templates.
 */
public class FlyNoWings implements FlyBehavior {
    @Override
    public void fly() {
        System.out.println("I'm can't fly !");
    }
}

package com.hycz.design.pattern.strategy.behavioral;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 10:47 AM
 * To change this template use File | Settings | File Templates.
 */
public class FlyWithWings implements FlyBehavior {
    @Override
    public void fly() {
        System.out.println("I'm flying !");
    }
}

package com.hycz.design.pattern.strategy.behavioral;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 10:16 AM
 * To change this template use File | Settings | File Templates.
 */
public interface QuackBehavior {

    public void quack();
}

package com.hycz.design.pattern.strategy.behavioral;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 10:50 AM
 * To change this template use File | Settings | File Templates.
 */
public class Quack implements QuackBehavior {
    @Override
    public void quack() {
        System.out.println("Quack !");
    }
}

package com.hycz.design.pattern.strategy;

import com.hycz.design.pattern.strategy.behavioral.FlyNoWings;
import com.hycz.design.pattern.strategy.behavioral.Quack;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 10:55 AM
 * To change this template use File | Settings | File Templates.
 */
public class ModelDuck extends Duck {

    public ModelDuck() {
        flyBehavior = new FlyNoWings();
        quackBehavior = new Quack();
    }

    @Override
    public void dispaly() {
        System.out.println("I'm a model duck !");
    }
}

package com.hycz.design.pattern.strategy;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 10:57 AM
 * To change this template use File | Settings | File Templates.
 */
public class MiniDuckSimulator {

    public static void main(String[] args) {
        Duck model = new ModelDuck();
        model.dispaly();
        model.performFly();
        model.performQuack();



    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值