http://www.books.com.tw/products/0010750585
chap06.
策略模式 Strategy
目的:將各種可以互換的演算法(策略)包裝成一個類別。
package com.ssc24.chap06.demo01; public class Adventurer { FightStrategy fightStrategy; public Adventurer() { System.out.println("冒險者"); } public void attack() { if (fightStrategy == null) { fightStrategy = new NormalAttack(); } fightStrategy.execute(); } public void choiceStrategy(FightStrategy fightStrategy) { this.fightStrategy = fightStrategy; } }
package com.ssc24.chap06.demo01; public interface FightStrategy { void execute(); }
package com.ssc24.chap06.demo01; public class NormalAttack implements FightStrategy { @Override public void execute() { System.out.println("使用一般攻擊"); } }
package com.ssc24.chap06.demo01; public class UseItem implements FightStrategy { @Override public void execute() { System.out.println("使用道具,丟火把"); } }
package com.ssc24.chap06.demo01; public class UseSkill implements FightStrategy { @Override public void execute() { System.out.println("使用超級痛的攻擊技能"); } }
package com.ssc24.chap06.demo01; import static org.junit.Assert.*; import org.junit.Test; public class FightStrategyTest { @Test public void test() { System.out.println("===策略模式測試==="); Adventurer adventurer = new Adventurer(); System.out.println("出現史萊姆"); adventurer.attack(); System.out.println("出現非常巨大的史萊姆"); adventurer.choiceStrategy(new UseSkill()); adventurer.attack(); System.out.println("出現不怕刀槍的殭屍"); adventurer.choiceStrategy(new UseItem()); adventurer.attack(); } }
/** ===策略模式測試=== 冒險者 出現史萊姆 使用一般攻擊 出現非常巨大的史萊姆 使用超級痛的攻擊技能 出現不怕刀槍的殭屍 使用道具,丟火把 **/
package com.ssc24.chap06.demo02; import java.util.Comparator; public class SortVillageByArea implements Comparator<Village> { @Override public int compare(Village o1, Village o2) { if (o1.area > o2.area){ return 1; } if (o1.area < o2.area){ return -1; } return 0; } }
package com.ssc24.chap06.demo02; import java.util.Comparator; public class SortVillageById implements Comparator<Village> { @Override public int compare(Village o1, Village o2) { if (o1.id > o2.id){ return 1; } if (o1.id < o2.id){ return -1; } return 0; } }
package com.ssc24.chap06.demo02; import java.util.Comparator; public class SortVillageByName implements Comparator<Village> { @Override public int compare(Village o1, Village o2) { if (o1.name.charAt(0) > o2.name.charAt(0)){ return 1; } if (o1.name.charAt(0) < o2.name.charAt(0)){ return -1; } return 0; } }
package com.ssc24.chap06.demo02; import java.util.Comparator; public class SortVillageByPopulation implements Comparator<Village> { @Override public int compare(Village o1, Village o2) { if (o1.population > o2.population){ return 1; } if (o1.population < o2.population){ return -1; } return 0; } }
package com.ssc24.chap06.demo02; public class Village { public int id; public String name; public int population; public double area; public Village ( int id,String name,int population,double area){ this.id = id; this.name = name; this.population = population; this.area = area; } public String toString(){ return id + "." + name + "(人口:" + population + " 面積: " + area + ")"; } }
package com.ssc24.chap06.demo02; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class StrategyExample { public static void main(String[] args) { System.out.println("===裝飾者模式 測試==="); Village appleFarm = new Village(3, "apple farm", 32, 5.1); Village barnField = new Village(1, "barn field", 22, 1.7); Village capeValley = new Village(2, "cape valley", 10, 10.2); List<Village> villages = new ArrayList<Village>(); villages.add(appleFarm); villages.add(barnField); villages.add(capeValley); System.out.println("沒排序過"); showList(villages); System.out.println("根據ID排序"); Collections.sort(villages,new SortVillageById()); showList(villages); System.out.println("根據名字排序"); Collections.sort(villages,new SortVillageByName()); showList(villages); System.out.println("根據人口排序"); Collections.sort(villages,new SortVillageByPopulation()); showList(villages); System.out.println("根據面積排序"); Collections.sort(villages,new SortVillageByArea()); showList(villages); } public static void showList(List<Village> list) { for (Village v : list) { System.out.println(v); } } }
/** ===裝飾者模式 測試=== 沒排序過 3.apple farm(人口:32 面積: 5.1) 1.barn field(人口:22 面積: 1.7) 2.cape valley(人口:10 面積: 10.2) 根據ID排序 1.barn field(人口:22 面積: 1.7) 2.cape valley(人口:10 面積: 10.2) 3.apple farm(人口:32 面積: 5.1) 根據名字排序 3.apple farm(人口:32 面積: 5.1) 1.barn field(人口:22 面積: 1.7) 2.cape valley(人口:10 面積: 10.2) 根據人口排序 2.cape valley(人口:10 面積: 10.2) 1.barn field(人口:22 面積: 1.7) 3.apple farm(人口:32 面積: 5.1) 根據面積排序 1.barn field(人口:22 面積: 1.7) 3.apple farm(人口:32 面積: 5.1) 2.cape valley(人口:10 面積: 10.2) **/