@Override publicvoidexecute(){ //你投下了瓦解的魔咒,龙在一堆尘土中蒸发了! LOGGER.info("You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"); }
@Override publicvoidexecute(){ //您用神奇的弓箭射击了龙,它掉在了地上! LOGGER.info("You shoot the dragon with the magical crossbow and it falls dead on the ground!"); } }
/** * * <p>The Strategy pattern (also known as the policy pattern) is a software design pattern that * enables an algorithm's behavior to be selected at runtime.</p> * * <p>Before Java 8 the Strategies needed to be separate classes forcing the developer * to write lots of boilerplate code. With modern Java it is easy to pass behavior * with method references and lambdas making the code shorter and more readable.</p> * * <p>In this example ({@link DragonSlayingStrategy}) encapsulates an algorithm. The containing * object ({@link DragonSlayer}) can alter its behavior by changing its strategy.</p> <p>策略模式(也称为策略模式)是一种软件设计模式,可以在运行时选择算法的行为。</ p> <p>在Java 8之前,策略必须是单独的类,从而迫使开发人员编写大量样板代码。 使用现代Java,可以轻松地通过方法引用和lambda传递行为,从而使代码更短,更易读。</ p> <p>在此示例({@link DragonSlayingStrategy})中封装了一种算法。 包含对象({@link DragonSlayer})可以通过更改其策略来更改其行为。</ p> * */ publicclassApp{
privatestaticfinal Logger LOGGER = LoggerFactory.getLogger(App.class); /** * Program entry point. * * @param args command line args */ publicstaticvoidmain(String[] args){ // GoF Strategy pattern LOGGER.info("Green dragon spotted ahead!"); var dragonSlayer = new DragonSlayer(new MeleeStrategy()); dragonSlayer.goToBattle(); LOGGER.info("Red dragon emerges."); dragonSlayer.changeStrategy(new ProjectileStrategy()); dragonSlayer.goToBattle(); LOGGER.info("Black dragon lands before you."); dragonSlayer.changeStrategy(new SpellStrategy()); dragonSlayer.goToBattle();
// Java 8 Strategy pattern LOGGER.info("Green dragon spotted ahead!"); dragonSlayer = new DragonSlayer( () -> LOGGER.info("With your Excalibur you severe the dragon's head!")); dragonSlayer.goToBattle(); LOGGER.info("Red dragon emerges."); dragonSlayer.changeStrategy(() -> LOGGER.info( "You shoot the dragon with the magical crossbow and it falls dead on the ground!")); dragonSlayer.goToBattle(); LOGGER.info("Black dragon lands before you."); dragonSlayer.changeStrategy(() -> LOGGER.info( "You cast the spell of disintegration and the dragon vaporizes in a pile of dust!")); dragonSlayer.goToBattle(); } }
应用场景
Java Comparator 中的策略模式
java.util.Comparator 接口是比较器接口,可以通过 Collections.sort(List,Comparator) 和 Arrays.sort(Object[],Comparator) 对集合和数据进行排序,下面为示例程序 一个学生类,有两个属性 id 和 name
publicclassTest1{ publicstaticvoidmain(String[] args){ Student[] students = { new Student(3, "张三"), new Student(1, "李四"), new Student(4, "王五"), new Student(2, "赵六") }; toString(students, "排序前");
Arrays.sort(students, new AscSortor()); toString(students, "升序后");
Arrays.sort(students, new DescSortor()); toString(students, "降序后"); }
publicstaticvoidtoString(Student[] students, String desc){ for (int i = 0; i < students.length; i++) { System.out.print(desc + ": " +students[i].toString() + ", "); } System.out.println(); } }
publicclassTest2{ publicstaticvoidmain(String[] args){ List<Student> students = Arrays.asList( new Student(3, "张三"), new Student(1, "李四"), new Student(4, "王五"), new Student(2, "赵六") ); toString(students, "排序前");
Collections.sort(students, new AscSortor()); toString(students, "升序后");
Collections.sort(students, new DescSortor()); toString(students, "降序后"); }