策略模式-三十六计
策略模式属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有共同接口的独立类中。从而使它们可以互相替换。策略模式使得算法可以在不影响客户端的情况下发生变化。
策略模式用法1
在一场战争中,一个伟大的将领需要根据敌人的数量,巧妙的使用各种计谋,来获取战争的胜利。将不同的计谋封装到单独的Stratagem类中,每个计谋有他们可以战胜的敌人个数。战争Warfare开始之前,将领需要根据自己对战场形势的判断,传入不同的计谋。
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 12:55 * @notify * @version 1.0 */ public interface Stratagem { void killEnemy(); }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 12:57 * @notify * @version 1.0 */ public class OneStratagem implements Stratagem{ @Override public void killEnemy() { System.out.println("杀敌100"); } }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 12:57 * @notify * @version 1.0 */ public class TwoStratagem implements Stratagem{ @Override public void killEnemy() { System.out.println("杀敌200"); } }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 12:57 * @notify * @version 1.0 */ public class ThreeStratagem implements Stratagem { @Override public void killEnemy() { System.out.println("杀敌300"); } }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 13:00 * @notify * @version 1.0 */ public class Warfare { private Stratagem stratagem; public Warfare(Stratagem stratagem) { this.stratagem = stratagem; } public void start() { stratagem.killEnemy(); } }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 13:01 * @notify * @version 1.0 */ public class Main { public static void main(String[] args) { Warfare warfare = new Warfare(new OneStratagem()); warfare.start(); } }
策略模式用法2
兵书中的计谋太多了,如果对阵的是100人,将领选择杀敌300人的计谋固然可以完全取胜,但是造成的兵马消耗也是巨大的。于是兵书的编纂者,给每一个计谋扩展了一个方法,计算当前计谋能否战胜敌人。如果可以战胜,自动的选择该计谋。
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 12:55 * @notify * @version 1.0 */ public interface Stratagem { void killEnemy(); boolean compare(int kill); }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 12:57 * @notify * @version 1.0 */ public class OneStratagem implements Stratagem { @Override public void killEnemy() { System.out.println("杀敌100"); } @Override public boolean compare(int kill) { if (kill <= 100) { return true; } return false; } }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 12:57 * @notify * @version 1.0 */ public class TwoStratagem implements Stratagem{ @Override public void killEnemy() { System.out.println("杀敌200"); } @Override public boolean compare(int kill) { if (kill <= 200) { return true; } return false; } }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 12:57 * @notify * @version 1.0 */ public class ThreeStratagem implements Stratagem { @Override public void killEnemy() { System.out.println("杀敌300"); } @Override public boolean compare(int kill) { if (kill <= 300) { return true; } return false; } }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 13:00 * @notify * @version 1.0 */ import java.util.ArrayList; import java.util.List; public class Warfare { private List<Stratagem> stratagemList = new ArrayList<>(); public Warfare(List<Stratagem> stratagemList) { this.stratagemList = stratagemList; } public void start() { Stratagem stratagem = null; for (Stratagem s : stratagemList) { boolean compare = s.compare(200); if (compare) { stratagem = s; break; } } stratagem.killEnemy(); } }
package strategy_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-18 13:01 * @notify * @version 1.0 */ import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Stratagem> stratagems = new ArrayList<>(); stratagems.add(new OneStratagem()); stratagems.add(new TwoStratagem()); stratagems.add(new ThreeStratagem()); Warfare warfare = new Warfare(stratagems); warfare.start(); } }