策略模式
为了满足不同子类具有不同的行为而设计
而且,它可以提供行为/算法的互换,使行为具有可变性
而且,支持扩展新的行为而不影响原有的代码
Sample1:
package pattern.strategy; /** * 策略模式 * 实现一组可相互切换的行为类/算法簇 */ public class StrategyExample { public static void main(String[] args) { int a = 1, b = 2; Strategy originalStrategy = new Add(); Context c = new Context(originalStrategy); int result = c.executeStrategy(a, b); System.out.println(result); //当某种条件发生时,切换策略 Strategy newStrategy = new Subtract(); c.changeStrategy(newStrategy); result = c.executeStrategy(a, b); System.out.println(result); } } class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } //对外提供改变策略的方法 public void changeStrategy(Strategy strategy) { this.strategy = strategy; } public int executeStrategy(int a, int b) { return this.strategy.execute(a, b); } } /** * 找出可能会发生变化的地方,独立进行封装 * 策略经常发生变化,所以要将其独立出来,与那些不变的代码进行隔离 */ interface Strategy { int execute(int a, int b); } class Add implements Strategy { public int execute(int a, int b) { return a + b; } } class Subtract implements Strategy { public int execute(int a, int b) { return a - b; } } class Multiply implements Strategy { public int execute(int a, int b) { return a * b; } }
Sample 2:
package pattern.strategy; public class FightDemo { public static void main(String[] args) { Character c = new Knight(); c.setWeapon(new AxeBehavior()); c.fight(); //更换武器 c.setWeapon(new SwordBehavior()); c.fight(); } } //========================================= abstract class Character { protected WeaponBehavior weapon; void setWeapon(WeaponBehavior w) { this.weapon = w; } abstract void fight(); } class Knight extends Character { @Override void fight() { System.out.println(Knight.class.getName() +" is fighting..."); this.weapon.useWeapon(); } } class King extends Character { @Override void fight() { System.out.println(King.class.getName() +" is fighting..."); this.weapon.useWeapon(); } } //========================================= /** * 行为接口 */ interface WeaponBehavior { void useWeapon(); } //行为一 class SwordBehavior implements WeaponBehavior { public void useWeapon() { System.out.println("Use sword!"); } } //行为二 class AxeBehavior implements WeaponBehavior { public void useWeapon() { System.out.println("Use axe!"); } }
相关推荐
闲了可以学学 这种东西感觉游刃有余的使用还是很不容易的设计模式 策略模式
head first 设计模式 策略模式 C++ 代码
iOS 设计模式 策略模式
策略模式实例策略模式实例策略模式实例策略模式实例策略模式实例
代码为博客的例子,详细讲解参考:http://blog.csdn.net/lmj623565791/article/details/24116745 有问题请留言
设计模式专题之(十六)策略模式---设计模式策略模式示例代码(python--c++)
设计模式中的策略模式。根据《设计模式》一书编写
设计模式--策略模式java例子
专题资料(2021-2022年)java设计模式策略模式的实现.doc
c++设计模式-行为型模式-策略模式;qt工程;c++简单源码; 策略(Strategy)模式的定义:该模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换,且算法的变化不会影响使用算法的客户。策略模式属于...
设计模式之装饰模式Java实现和UML类设计图
短小版的thinkphp3.2 缓存类 策略模式理解 希望可以帮助到你
走进设计模式之 策略模式(Strategy) 代码
包含策略模式,单件模式,迭代器与组合模式,工厂模式,观察者模式,命令模式,适配器与外观模式,装饰者模式的问题描述,类图,源码,运行截图。
这是策略模式中的一个经典实例,通过鸭子问题,能让学习者更好的了解设计模式,这也是headfirst 设计模式中用的经典实例
参考header first里的代码编写的小demo方便自己理解
设计模式 策略模式。金典模式之策略模式代码。
设计模式 ——策略模式 实验报告 详细代码 设计概要都有
├── 第25章_策略模式.ppt ├── 第26章_模板方法模式.ppt ├── 第27章_访问者模式.ppt ├── 第2章_面向对象设计原则.ppt ├── 第3章_设计模式概述.ppt ├── 第4章_简单工厂模式.ppt ├── 第5章_工厂...