java设计模式进阶_strategy

这里写图片描述

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : DragonSlayingStrategy.java
//  @ Date : 2016/8/23
//  @ Author : 
//
//

public interface DragonSlayingStrategy {
    public void execute();
}
//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : MeleeStrategy.java
//  @ Date : 2016/8/23
//  @ Author : 
//
//




public class MeleeStrategy implements DragonSlayingStrategy {

    public void execute()
    {
        System.out.println("With your Excalibur you severe the dragon's head!");
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : SpellStrategy.java
//  @ Date : 2016/8/23
//  @ Author : 
//
//




public class SpellStrategy implements DragonSlayingStrategy {

    public void execute()
    {
        System.out.println("You cast the spell of disintegration and the dragon vaporizes in a pile of dust!");
    }
}
//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ProjectileStrategy.java
//  @ Date : 2016/8/23
//  @ Author : 
//
//




public class ProjectileStrategy implements DragonSlayingStrategy {

    public void execute()
    {
        System.out.println("You shoot the dragon with the magical crossbow and it falls dead on the ground!");
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : DragonSlayer.java
//  @ Date : 2016/8/23
//  @ Author : 
//
//

/*
 * DragonSlayer 使用不同的策略去杀龙.
 */
public class DragonSlayer {

    private DragonSlayingStrategy strategy;

    public DragonSlayer(DragonSlayingStrategy strategy)
    {
        this.strategy = strategy;
    }

    public void changeStrategy(DragonSlayingStrategy dss) {
        this.strategy = dss;
    }

    public void gotoBattle() {
        strategy.execute();
    }
}

/*
 * 策略(DragonSlayingStrategy) 封装一个算法.包含对象(DragonSlayer)能修改它的行为通过
 * changeStrategy方法改变它的策略.
 */
public class App {

    public static void main(String[] args) {
        System.out.println("Green  dragon spotted ahead!");
        DragonSlayer dragonSlayer = new DragonSlayer(new MeleeStrategy());
        dragonSlayer.gotoBattle();

        System.out.println("Red dragon emerges.");
        dragonSlayer.changeStrategy(new ProjectileStrategy());
        dragonSlayer.gotoBattle();

        System.out.println("Black dragon lands before you.");
        dragonSlayer.changeStrategy(new SpellStrategy());
        dragonSlayer.gotoBattle();
    }

}


//Green  dragon spotted ahead!
//With your Excalibur you severe the dragon's head!
//Red dragon emerges.
//You shoot the dragon with the magical crossbow and it falls dead on the ground!
//Black dragon lands before you.
//You cast the spell of disintegration and the dragon vaporizes in a pile of dust!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值