2019年5月13日 星期一

7天學會設計模式 CHAP13 合成模式 Composite

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

chap13.
合成模式 Composite
目的:處理樹狀結構資料
package com.ssc24.chap13.demo01;
public abstract class AbstractAssociation {
 protected String name;
 public AbstractAssociation(String name) {
  this.name = name;
 }
 public abstract void add(AbstractAssociation a);
 public abstract void remove(AbstractAssociation a);
 public abstract void display(int depth);
 public abstract void lineOfDute();
}

package com.ssc24.chap13.demo01;
import java.util.ArrayList;
import java.util.List;
public class Association extends AbstractAssociation {
 private List<AbstractAssociation> branchs = new ArrayList<AbstractAssociation>();
 public Association(String name) {
  super(name);
 }
 @Override
 public void add(AbstractAssociation a) {
  branchs.add(a);
 }
 @Override
 public void remove(AbstractAssociation a) {
  branchs.remove(a);
 }
 @Override
 public void display(int depth) {
  for (int i =0 ; i < depth ; i++) {
   System.out.print("-");
  }
  System.out.println(name);
  for (AbstractAssociation a : branchs) {
   a.display(depth + 1);
  }
  //System.out.println("|");
 }
 @Override
 public void lineOfDute() {
  for (AbstractAssociation a : branchs) {
   a.lineOfDute();
  }
 }
}

package com.ssc24.chap13.demo01;
public abstract class Department extends AbstractAssociation {
 public Department(String name) {
  super(name);
 }
 @Override
 public void add(AbstractAssociation a) {
  System.out.println("Leaf無法增加子節點");
 }
 @Override
 public void remove(AbstractAssociation a) {
  System.out.println("Leaf無子節點可以移除");
 }
 @Override
 public void display(int depth) {
  for (int i =0 ; i < depth ; i++) {
   System.out.print("-");
  }
  //System.out.println("*");
  System.out.println(name);
 }
 @Override
 public abstract void lineOfDute();
}

package com.ssc24.chap13.demo01;
public class HumanResouce extends Department {
 public HumanResouce(String name) {
  super(name);
 }
 @Override
 public void lineOfDute() {
  System.out.println(name + " :想辦法拐騙冒險者來完成任務");
 }
}

package com.ssc24.chap13.demo01;
public class ServiceDepartment extends Department {
 public ServiceDepartment(String name) {
  super(name);
 }
 @Override
 public void lineOfDute() {
  System.out.println(name + " :處理客訴,告訴客戶,這肯定是冒險者的錯,不是協會的錯");
 }
}

package com.ssc24.chap13.demo01;
import static org.junit.Assert.*;
import org.junit.Test;
public class BranchOrganizationTest {
 @Test
 public void test() {
  System.out.println("===合成模式測試===");
  AbstractAssociation root = new Association("冒險者總會");
  root.add(new HumanResouce("總會-人力資源單位"));
  root.add(new ServiceDepartment("總會-客服單位"));
  AbstractAssociation mars = new Association("火星分會");
  mars.add(new HumanResouce("火星分會-人力資源單位"));
  mars.add(new ServiceDepartment("火星分會-客服單位"));
  root.add(mars);
  AbstractAssociation saturn = new Association("土星分會");
  saturn.add(new HumanResouce("土星分會-人力資源單位"));
  saturn.add(new ServiceDepartment("土星分會-客服單位"));
  root.add(saturn);
  AbstractAssociation m1 = new Association("土衛1號辦事處");
  m1.add(new HumanResouce("土衛1號辦事處-人力資源單位"));
  m1.add(new ServiceDepartment("土衛1號辦事處-客服單位"));
  saturn.add(m1);
  AbstractAssociation m2 = new Association("土衛2號辦事處");
  m2.add(new HumanResouce("土衛2號辦事處-人力資源單位"));
  saturn.add(m2);
  System.out.println("結構圖:");
  root.display(1);
  System.out.println();
  System.out.println("職責表");
  root.lineOfDute();
  System.out.println();
  AbstractAssociation root2 = new HumanResouce("測試1");
  root2.remove(m2);
 }
}

/**
===合成模式測試===
結構圖:
-冒險者總會
--總會-人力資源單位
--總會-客服單位
--火星分會
---火星分會-人力資源單位
---火星分會-客服單位
--土星分會
---土星分會-人力資源單位
---土星分會-客服單位
---土衛1號辦事處
----土衛1號辦事處-人力資源單位
----土衛1號辦事處-客服單位
---土衛2號辦事處
----土衛2號辦事處-人力資源單位

職責表
總會-人力資源單位 :想辦法拐騙冒險者來完成任務
總會-客服單位 :處理客訴,告訴客戶,這肯定是冒險者的錯,不是協會的錯
火星分會-人力資源單位 :想辦法拐騙冒險者來完成任務
火星分會-客服單位 :處理客訴,告訴客戶,這肯定是冒險者的錯,不是協會的錯
土星分會-人力資源單位 :想辦法拐騙冒險者來完成任務
土星分會-客服單位 :處理客訴,告訴客戶,這肯定是冒險者的錯,不是協會的錯
土衛1號辦事處-人力資源單位 :想辦法拐騙冒險者來完成任務
土衛1號辦事處-客服單位 :處理客訴,告訴客戶,這肯定是冒險者的錯,不是協會的錯
土衛2號辦事處-人力資源單位 :想辦法拐騙冒險者來完成任務

Leaf無子節點可以移除
**/