設計模式(5)抽象工廠模式
阿新 • • 發佈:2019-03-05
如果 trac 一個 定義 pan 成員 oid c-c 數量
抽象工廠,提供一個工廠接口,用於創建依賴對象的家族,不需要明確指定具體類。可以理解是多個工廠方法的組合。
例如一個款手機是有電池、相機、處理器、屏幕組成的,它其中包含了不同的對象,我們創建時需要創建多個不同類型的對象,而工廠方法只能創建一種類型的對象,滿足不了這種需求。那我們可以把多個工廠方法組合到一起,用來創建多個產品。
首先我們先定義抽象工廠,他可以生產電池、相機、處理器、屏幕
public interface BasePhoneFactory { public BaseCamara createCamara(); public BaseBattery createBattery();BasePhoneFactorypublic BaseScreen createScreen(); public BaseCpu createCpu(); }
然後創建各個抽象的產品
public interface BaseBattery { public void name(); } public interface BaseCamara { public void name(); } public interface BaseCpu { public void產品抽象對象name(); } public interface BaseScreen { public void name(); }
然後各個產品的實現類
public class Battery_3000 implements BaseBattery { @Override public void name() { System.out.println("3000毫安鋰電池"); } } public class Battery_3800 implements BaseBattery { @Override產品實現類public void name() { System.out.println("3800毫安鋰電池"); } } public class Camara_1200 implements BaseCamara { @Override public void name() { System.out.println("1200W像素相機"); } } public class Camara_1800 implements BaseCamara { @Override public void name() { System.out.println("1800W像素相機"); } } public class Cpu_4_8 implements BaseCpu { @Override public void name() { System.out.println("4核8線程處理器"); } } public class Cpu_6_12 implements BaseCpu { @Override public void name() { System.out.println("6核12線程處理器"); } } public class Screen_1080 implements BaseScreen { @Override public void name() { System.out.println("1080P屏幕"); } } public class Screen_1440 implements BaseScreen { @Override public void name() { System.out.println("1440P屏幕"); } }
現在就是我們工廠的創建了
public class PhoneFactory_2018 implements BasePhoneFactory { @Override public BaseCamara createCamara() { return new Camara_1200(); } @Override public BaseBattery createBattery() { return new Battery_3000(); } @Override public BaseScreen createScreen() { return new Screen_1080(); } @Override public BaseCpu createCpu() { return new Cpu_4_8(); } } public class PhoneFactory_2019 implements BasePhoneFactory { @Override public BaseCamara createCamara() { return new Camara_1800(); } @Override public BaseBattery createBattery() { return new Battery_3800(); } @Override public BaseScreen createScreen() { return new Screen_1440(); } @Override public BaseCpu createCpu() { return new Cpu_6_12(); } }工廠
下面是手機基類和它的實現類
public abstract class BasePhone { /** * 電池 */ protected BaseBattery battery; /** * 相機 */ protected BaseCamara camara; /** * 處理器 */ protected BaseCpu cpu; /** * 屏幕 */ protected BaseScreen screen; public void display() { battery.name(); camara.name(); cpu.name(); screen.name(); } public abstract void getPhone(); }BasePhone
public class Phone_2018 extends BasePhone { BasePhoneFactory phoneFactory; public Phone_2018() { phoneFactory = new PhoneFactory_2018(); } @Override public void getPhone() { this.camara = this.phoneFactory.createCamara(); this.battery = this.phoneFactory.createBattery(); this.cpu = this.phoneFactory.createCpu(); this.screen = this.phoneFactory.createScreen(); } @Override public void display() { System.out.println("2018年款手機"); super.display(); } }Phone_2018
public class Phone_2019 extends BasePhone { BasePhoneFactory phoneFactory; public Phone_2019() { phoneFactory = new PhoneFactory_2019(); } @Override public void getPhone() { this.camara = this.phoneFactory.createCamara(); this.battery = this.phoneFactory.createBattery(); this.cpu = this.phoneFactory.createCpu(); this.screen = this.phoneFactory.createScreen(); } @Override public void display() { System.out.println("2019年款手機"); super.display(); } }Phone_2019
測試代碼
public class Main { public static void main(String[] args) { BasePhone phone1 = new Phone_2018(); phone1.getPhone(); phone1.display(); System.out.println("-------------------"); BasePhone phone2 = new Phone_2019(); phone2.getPhone(); phone2.display(); } }
輸出結果
抽象工廠一個具體的工廠就能創建出多個產品,和工廠方法相比,減少了具體工廠的數量。
但是,如果需要給抽象工廠中添加一個成員,就需要改動所有的工廠,擴展性就比較差了。
設計模式(5)抽象工廠模式