package Create.c.AbstractFactory; //業務接口 public interface Salary { void calcSalary(); }
package Create.c.AbstractFactory; //業務接口 public interface Tax { void calcTax(); }
package Create.c.AbstractFactory; //業務實現類 public class HeBeiTax implements Tax { @Override public void calcTax() { // TODO Auto-generated method stub System.out.println("開始計算河北子公司的稅收!"); } }
package Create.c.AbstractFactory; //業務實現類 public class HeBeiSalary implements Salary { @Override public void calcSalary() { // TODO Auto-generated method stub System.out.println("開始計算河北子公司的薪資!"); } }
package Create.c.AbstractFactory; //業務實現類 public class GuangDongTax implements Tax { @Override public void calcTax() { // TODO Auto-generated method stub System.out.println("開始計算廣東子公司的稅收!"); } }
package Create.c.AbstractFactory; //業務實現類 public class GuangDongSalary implements Salary { @Override public void calcSalary() { // TODO Auto-generated method stub System.out.println("開始計算廣東子公司的薪資!"); } }
package Create.c.AbstractFactory; //工廠接口 public interface Factory { Salary createSalary(); Tax createTax(); }
package Create.c.AbstractFactory; //業務實現類工廠類 public class HeBeiFactory implements Factory { @Override public Salary createSalary() { // TODO Auto-generated method stub return new HeBeiSalary(); } @Override public Tax createTax() { // TODO Auto-generated method stub return new HeBeiTax(); } }
package Create.c.AbstractFactory; //業務實現類工廠類 public class GuangDongFactory implements Factory { @Override public Salary createSalary() { // TODO Auto-generated method stub return new GuangDongSalary(); } @Override public Tax createTax() { // TODO Auto-generated method stub return new GuangDongTax(); } }
package Create.c.AbstractFactory; //業務調用類 public class Client { public static void main(String[] args) { Factory fa = new GuangDongFactory(); Salary sa = fa.createSalary(); Tax tax = fa.createTax(); sa.calcSalary(); tax.calcTax(); fa = new HeBeiFactory(); sa = fa.createSalary(); tax =fa.createTax(); sa.calcSalary(); tax.calcTax(); } }
優勢:全部實現類設計到一個工廠類裏面,客戶端調用變得很是簡單,而且若是須要更換一個系列的產品,只須要更換一個工廠類便可java
缺點:若是涉及需求變動或添加,則須要修改抽象工廠類,並同時修改實現這個抽象工廠類的具體實現類,添加額外編碼的工做量(跟工廠方法模式相同)ide