2019年5月13日 星期一

7天學會設計模式 CHAP12 樣版模式 Template

7天學會設計模式
http://www.books.com.tw/products/0010750585

chap12.
樣版模式 Template
目的:定義一套演算法的架構,但是細節可延遲到子類別再決定。
package com.ssc24.chap12.demo01;
public abstract class Adventurer {
 protected int level;
 protected String type;
 public int getLevel() {
  return level;
 }
 public void setLevel(int level) {
  this.level = level;
 }
 public String getType() {
  return type;
 }
 public void setType(String type) {
  this.type = type;
 }
}

package com.ssc24.chap12.demo01;
public class DifficultMaze extends MazeTemplate {
 public DifficultMaze() {
  super.isDoubleMaze = true;
  super.difficulty = 50;
 }
 @Override
 void createMaze() {
  System.out.println("準備1000*1000的迷宮(包括隱藏迷宮)");
  System.out.println("安排打不完的小怪物");
  System.out.println("安排等級 50  的中BOSS,100隻");
  System.out.println("安排等級 120 的超級BOSS,放隱藏迷宮的寶物");
  System.out.println("拔草整理場地,重新油漆牆壁");
  System.out.println("擺放各種陷阱,擺放假屍體");
  System.out.println("困難迷宮準備完成");
 }
 @Override
 void start() {
  System.out.println("冒險者開始進行困難迷宮的冒險");

 }
}

package com.ssc24.chap12.demo01;
public class EazyMaze extends MazeTemplate {
 public EazyMaze() {
  super.difficulty = 1;
 }
 @Override
 void createMaze() {
  System.out.println("準備100*100的迷宮");
  System.out.println("安排10隻小怪物");
  System.out.println("安排等級10的Boss");
  System.out.println("拔草整理場地");
  System.out.println("簡易迷宮準備完成");

 }
 @Override
 void start() {
  System.out.println("冒險者開始進行簡單迷宮的冒險");
 }
}

package com.ssc24.chap12.demo01;
public class GundamJustice extends Adventurer {
 public GundamJustice() {
  super.type = "Gundam-Justice";
  super.level = 100;
 }
}

package com.ssc24.chap12.demo01;
public abstract class MazeTemplate {
 protected int difficulty;
 protected Adventurer adventurer;
 protected boolean isDoubleMaze = false;
 public Adventurer adventurer(Adventurer adventurer) {
  this.adventurer = adventurer;
  if (!this.checkLevel(adventurer.getLevel())) {
   System.out.println("冒險者等級不足,請提升等級至 " + this.difficulty + " 後開放迷宮");
  } else {
   System.out.println("---" + this.adventurer.getType() + " 開始進行困難度 " + this.difficulty + " 的迷宮");
   this.createMaze();
   this.start();
   
   if (isDoubleMaze) {
    this.hiddenMaze();
   }
   this.showReslt();
  }
  return this.adventurer;
 }
 private boolean checkLevel(int level) {
  if (level < this.difficulty) {
   return false;
  }
  return true;
 }
 abstract void createMaze();
 abstract void start();
 void hiddenMaze() {
  System.out.println("進入隱藏迷宮");
 }
 Adventurer showReslt() {
  this.adventurer.setLevel(this.adventurer.getLevel() + 50 * this.difficulty);
  System.out.println("---" + this.adventurer.getType() + " 完成困難度 " + this.difficulty + " 迷宮");
  return this.adventurer;
 }
}

package com.ssc24.chap12.demo01;
import static org.junit.Assert.*;
import org.junit.Test;
public class MazeTest {
 Adventurer sabar = new Sabar();
 Adventurer justice = new GundamJustice();
 MazeTemplate easyMaze = new EazyMaze();
 MazeTemplate hardMaze = new DifficultMaze();
 @Test
 public void test() {
  System.out.println("===樣版模式測試===");
  System.out.println("===困難迷宮===");
  sabar = hardMaze.adventurer(sabar);
  System.out.println("===簡單迷宮練功===");
  sabar = easyMaze.adventurer(sabar);
  System.out.println();
  System.out.println("===困難迷宮測試====");
  sabar = hardMaze.adventurer(sabar);
  justice = hardMaze.adventurer(justice);
 }
}

/**
===樣版模式測試===
===困難迷宮===
冒險者等級不足,請提升等級至 50 後開放迷宮
===簡單迷宮練功===
---Saber 開始進行困難度 1 的迷宮
準備100*100的迷宮
安排10隻小怪物
安排等級10的Boss
拔草整理場地
簡易迷宮準備完成
冒險者開始進行簡單迷宮的冒險
---Saber 完成困難度 1 迷宮

===困難迷宮測試====
---Saber 開始進行困難度 50 的迷宮
準備1000*1000的迷宮(包括隱藏迷宮)
安排打不完的小怪物
安排等級 50  的中BOSS,100隻
安排等級 120 的超級BOSS,放隱藏迷宮的寶物
拔草整理場地,重新油漆牆壁
擺放各種陷阱,擺放假屍體
困難迷宮準備完成
冒險者開始進行困難迷宮的冒險
進入隱藏迷宮
---Saber 完成困難度 50 迷宮
---Gundam-Justice 開始進行困難度 50 的迷宮
準備1000*1000的迷宮(包括隱藏迷宮)
安排打不完的小怪物
安排等級 50  的中BOSS,100隻
安排等級 120 的超級BOSS,放隱藏迷宮的寶物
拔草整理場地,重新油漆牆壁
擺放各種陷阱,擺放假屍體
困難迷宮準備完成
冒險者開始進行困難迷宮的冒險
進入隱藏迷宮
---Gundam-Justice 完成困難度 50 迷宮
**/