java设计模式进阶_strategy
2016-08-23 10:31
387 查看
// // // 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!
相关文章推荐
- java设计模式之strategy(策略模式)和policy模式(泛化的strategy模式)
- Java设计模式-----Strategy策略模式
- Java设计模式十: 策略模式(Strategy)
- Java 进阶7 并发优化 1 并行程序的设计模式
- Java设计模式透析之 —— 策略(Strategy)
- JAVA与设计模式--之STRATEGY策略模式
- java 设计模式学习笔记十五 strategy 策略设计模式
- JAVA设计模式十:--strategy(策略者模式)
- JAVA设计模式—策略模式(Strategy)
- Java开发中的23种设计模式之十三:策略模式(strategy)
- JAVA设计模式之Strategy模式
- java设计模式之策略模式 Strategy
- java设计模式:策略模式(Strategy)
- JAVA设计模式-Strategy
- Java设计模式-----Strategy策略模式
- 设计模式---策略模式(Strategy Pattern with java)
- JAVA设计模式十--strategy(策略者模式)
- java设计模式之Strategy
- Java设计模式透析之 —— 策略(Strategy)
- Java设计模式透析之 —— 策略(Strategy)