1.簡(jiǎn)單工廠(Simple Factory)部分
*針對(duì)接口編程可以隔離掉系統(tǒng)以后可能發(fā)生的一大堆改變。
*用靜態(tài)方法定義的工廠被成為靜態(tài)工廠,這樣就不用使用創(chuàng)建對(duì)象的方法來(lái)實(shí)例化對(duì)象,使用方便。但是這樣做的缺點(diǎn)是無(wú)法通過(guò)繼承來(lái)改變創(chuàng)建方法的行為。
*簡(jiǎn)單工廠不是一種設(shè)計(jì)模式,但是它比較常用。
2.工廠方法(Factory Method)模式部分
----芝加哥風(fēng)味匹薩店----
- public class ChicagoPizzaStore extends PizzaStore {
- Pizza createPizza(String item) {
- if ("cheese".equals(item)) {
- return ChicagoStyleCheesePizza();
- } else if ("veggie".equals(item)) {
- return ChicagoStyleVeggiePizza();
- } else if ("clam".equals(item)) {
- return ChicagoStyleClamPizza();
- } else if ("pepperoni".equals(item)) {
- return ChicagoStylePepperoniPizza();
- } else
- return null;
- }
- }
public class ChicagoPizzaStore extends PizzaStore {Pizza createPizza(String item) {if ("cheese".equals(item)) {return ChicagoStyleCheesePizza();} else if ("veggie".equals(item)) {return ChicagoStyleVeggiePizza();} else if ("clam".equals(item)) {return ChicagoStyleClamPizza();} else if ("pepperoni".equals(item)) {return ChicagoStylePepperoniPizza();} elsereturn null;}}
------------
----加州風(fēng)味匹薩店----
- public class CaliforniaPizzaStore extends PizzaStore {
- Pizza createPizza(String item) {
- if ("cheese".equals(item)) {
- return CaliforniaStyleCheesePizza();
- } else if ("veggie".equals(item)) {
- return CaliforniaStyleVeggiePizza();
- } else if ("clam".equals(item)) {
- return CaliforniaStyleClamPizza();
- } else if ("pepperoni".equals(item)) {
- return CaliforniaStylePepperoniPizza();
- } else
- return null;
- }
- }
public class CaliforniaPizzaStore extends PizzaStore {Pizza createPizza(String item) {if ("cheese".equals(item)) {return CaliforniaStyleCheesePizza();} else if ("veggie".equals(item)) {return CaliforniaStyleVeggiePizza();} else if ("clam".equals(item)) {return CaliforniaStyleClamPizza();} else if ("pepperoni".equals(item)) {return CaliforniaStylePepperoniPizza();} elsereturn null;}}
------------
*工廠方法用來(lái)處理對(duì)象的創(chuàng)建,并將這樣的行為封裝在子類(lèi)中。這樣,客戶程序中關(guān)于超類(lèi)的代碼就和子類(lèi)對(duì)象的創(chuàng)建代碼解耦(Decouple)了。
工廠方法的定義:abstract Product factoryMethod(String type);
*所有工廠模式都用來(lái)封裝對(duì)象的創(chuàng)建。
*工廠方法模式(Factory Method Pattern)通過(guò)讓子類(lèi)來(lái)決定該創(chuàng)建的對(duì)象是什么,來(lái)達(dá)到將對(duì)象的創(chuàng)建過(guò)程封裝的目的。
*在工廠方法模式中包括創(chuàng)建者(Creator)類(lèi)和產(chǎn)品(Product)類(lèi)兩種類(lèi)型的類(lèi)。
----設(shè)計(jì)謎題解答----
引用
1、與NYPizzaStore和ChicagoPizzaStore平行的是CaliforniaPizzaStore類(lèi),繼承自PizzaStore類(lèi)。
2、與NYStyleCheesePizza和ChicagoStyleCheesePizza平行的是CaliforniaStyleCheesePizza、CaliforniaStylePepperoniPizza、CaliforniaStyleClamPizza、CaliforniaStyleVeggiePizza類(lèi),繼承自Pizza類(lèi),被CaliforniaPizzaStore類(lèi)所依賴。
------------
工廠(Factory Method Pattern)方法模式:定義了一個(gè)創(chuàng)建對(duì)象的接口,但是由子類(lèi)來(lái)決定要實(shí)例化的類(lèi)是哪一個(gè)。它讓類(lèi)把實(shí)例化推遲到了子類(lèi)。
*創(chuàng)建者(Creator)類(lèi)實(shí)現(xiàn)了所有操縱產(chǎn)品的方法,但是不實(shí)現(xiàn)工廠方法。
*工廠方法模式可以和策略(Strategy)模式結(jié)合起來(lái),在運(yùn)行時(shí)動(dòng)態(tài)地更換工廠類(lèi),從而創(chuàng)建不同的產(chǎn)品對(duì)象,這是簡(jiǎn)單工廠所不具有的彈性。
*工廠方法模式可以讓客戶在實(shí)例化對(duì)象時(shí),只依賴接口,而不依賴具體的實(shí)現(xiàn)類(lèi)。這符合“針對(duì)接口編程,而不是針對(duì)實(shí)現(xiàn)編程”的軟件設(shè)計(jì)原則。
*如果類(lèi)A的改變會(huì)影響到類(lèi)B,那么我們說(shuō)類(lèi)B“依賴于”類(lèi)A。
軟件設(shè)計(jì)原則:要依賴抽象,不要依賴具體類(lèi)。這個(gè)原則又被稱為“依賴倒置原則(Dependency Inversion Principle)”。
*依賴倒置原則說(shuō)明不能讓高層組件依賴于底層組件,而且它們都應(yīng)該依賴于抽象。
*遵循依賴倒置原則的三個(gè)指導(dǎo)方針:
(1)變量不可以持有具體類(lèi)的引用。這可以通過(guò)使用工廠避開(kāi)。
(2)不要讓類(lèi)派生自具體類(lèi)。否則就會(huì)依賴具體類(lèi),違反了“針對(duì)接口編程,而不是針對(duì)現(xiàn)實(shí)編程”的軟件設(shè)計(jì)原則。
(3)不要覆蓋基類(lèi)中已實(shí)現(xiàn)的方法。出現(xiàn)這樣的情況就說(shuō)明基類(lèi)設(shè)計(jì)的有問(wèn)題。
3.抽象工廠(Abstract Factory)模式部分
抽象工廠模式:提供一個(gè)接口,用于創(chuàng)建相關(guān)或者依賴對(duì)象的家族,而不需要明確指定具體類(lèi)。
*抽象工廠模式是工廠方法模式的演變。工廠方法模式中,創(chuàng)建者只生產(chǎn)一種類(lèi)型的產(chǎn)品,而抽象工廠模式中,創(chuàng)建者生產(chǎn)一組不同類(lèi)型的產(chǎn)品。
----ChicagoPizzaIngredientFactory代碼----
- public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
- public Dough createDough() {
- return new ThickCrustDough();
- }
-
- public Sause createSause() {
- return new FlumTomatoSause();
- }
-
- public Cheese createCheese() {
- return new Mozzarella();
- }
-
- public Veggies[] createVeggies() {
- return new Beggies[] {new BlackOlives(), new Spinach(), new EggPlant()};
- }
-
- public Pepperoni createPepperoni() {
- return new SlicedPepperoni();
- }
-
- public Clam createClam() {
- return new FrozenClams();
- }
- }
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {public Dough createDough() {return new ThickCrustDough();}public Sause createSause() {return new FlumTomatoSause();}public Cheese createCheese() {return new Mozzarella();}public Veggies[] createVeggies() {return new Beggies[] {new BlackOlives(), new Spinach(), new EggPlant()};}public Pepperoni createPepperoni() {return new SlicedPepperoni();}public Clam createClam() {return new FrozenClams();}}
------------
*抽象工廠(Abstract Factory)模式同時(shí)結(jié)合了工廠方法(Factory Method)模式和策略(Strategy)模式。
*工廠方法使用繼承:把對(duì)象的創(chuàng)建委托(Delegate)給子類(lèi),讓子類(lèi)實(shí)現(xiàn)工廠方法創(chuàng)建對(duì)象。
*抽象工廠使用對(duì)象的組合:對(duì)象的創(chuàng)建被實(shí)現(xiàn)在工廠接口所暴露出來(lái)的方法中。
*所有工廠模式都通過(guò)減少應(yīng)用程序和具體類(lèi)之間的依賴來(lái)促進(jìn)松耦合,即解耦(Decouple)。
4.簡(jiǎn)單工廠(Simple Factory)實(shí)例
- public abstract class Car {
- protected String name;
-
- protected String engines;
-
- protected String wheels;
-
- protected String lamps;
-
- public void prepare() {
- System.out.println("Preparing " + name);
- System.out.println("Get engines ready...");
- System.out.println("Get wheels ready...");
- System.out.println("Get lamps ready...");
- }
-
-
- public void fabricate() {
- System.out.println("Adding engines.");
- System.out.println("Adding wheels.");
- System.out.println("Adding lamps.");
- }
-
-
- public void detect() {
- System.out.println("Detect the car.");
- }
-
- public String getName() {
- return name;
- }
- }
-
- public class BmwX3Car extends Car {
- public BmwX3Car() {
- name = "BMW X3";
- engines = "One Engine";
- wheels = "Four Wheels";
- lamps = "Two Lamps";
- }
- }
-
- public class BmwX5Car extends Car {
- public BmwX5Car() {
- name = "BMW X5";
- engines = "Two Engines";
- wheels = "Four Wheels";
- lamps = "Four Lamps";
- }
- }
-
- public class BmwX7Car extends Car {
- public BmwX7Car() {
- name = "BMW X7";
- engines = "Four Engine";
- wheels = "Four Wheels and One Spare Tyre";
- lamps = "Six Lamps";
- }
- }
-
- public class SimpleCarFactory {
-
- public Car createCar(String type) {
- if ("bmwx3".equals(type)) {
- return new BmwX3Car();
- } else if ("bmwx5".equals(type)) {
- return new BmwX5Car();
- } else if ("bmwx7".equals(type)) {
- return new BmwX7Car();
- } else
- return null;
- }
- }
-
-
- public class CarSalesShop {
- public Car orderCar(String type) {
- SimpleCarFactory factory = new SimpleCarFactory();
- Car car = factory.createCar(type);
- car.prepare();
- car.fabricate();
- car.detect();
-
- System.out.println("A " + car.getName() + " car is ready.");
- return car;
- }
- }
public abstract class Car {protected String name;// 名稱protected String engines;// 發(fā)動(dòng)機(jī)protected String wheels;// 車(chē)輪protected String lamps;// 車(chē)燈public void prepare() {System.out.println("Preparing " + name);System.out.println("Get engines ready...");System.out.println("Get wheels ready...");System.out.println("Get lamps ready...");}// 組裝public void fabricate() {System.out.println("Adding engines.");System.out.println("Adding wheels.");System.out.println("Adding lamps.");}// 檢測(cè)public void detect() {System.out.println("Detect the car.");}public String getName() {return name;}}public class BmwX3Car extends Car {public BmwX3Car() {name = "BMW X3";engines = "One Engine";wheels = "Four Wheels";lamps = "Two Lamps";}}public class BmwX5Car extends Car {public BmwX5Car() {name = "BMW X5";engines = "Two Engines";wheels = "Four Wheels";lamps = "Four Lamps";}}public class BmwX7Car extends Car {public BmwX7Car() {name = "BMW X7";engines = "Four Engine";wheels = "Four Wheels and One Spare Tyre";lamps = "Six Lamps";}}public class SimpleCarFactory {// 簡(jiǎn)單工廠public Car createCar(String type) {if ("bmwx3".equals(type)) {return new BmwX3Car();} else if ("bmwx5".equals(type)) {return new BmwX5Car();} else if ("bmwx7".equals(type)) {return new BmwX7Car();} elsereturn null;}}// 汽車(chē)銷(xiāo)售店public class CarSalesShop {public Car orderCar(String type) {SimpleCarFactory factory = new SimpleCarFactory();Car car = factory.createCar(type);car.prepare();car.fabricate();car.detect();System.out.println("A " + car.getName() + " car is ready.");return car;}}
5.靜態(tài)工廠實(shí)例
在上例的基礎(chǔ)上實(shí)現(xiàn):
- public class StaticCarFactory {
-
- public static Car createCar(String type) {
- if ("bmwx3".equals(type)) {
- return new BmwX3Car();
- } else if ("bmwx5".equals(type)) {
- return new BmwX5Car();
- } else if ("bmwx7".equals(type)) {
- return new BmwX7Car();
- } else
- return null;
- }
- }
public class StaticCarFactory {// 靜態(tài)工廠public static Car createCar(String type) {if ("bmwx3".equals(type)) {return new BmwX3Car();} else if ("bmwx5".equals(type)) {return new BmwX5Car();} else if ("bmwx7".equals(type)) {return new BmwX7Car();} elsereturn null;}}
修改CarSalesShop的代碼:
- public class CarSalesShop {
- public Car orderCar(String type) {
- Car car = StaticCarFactory.createCar(type);
- car.prepare();
- car.fabricate();
- car.detect();
-
- System.out.println("A " + car.getName() + " car is ready.");
- return car;
- }
- }
public class CarSalesShop {public Car orderCar(String type) {Car car = StaticCarFactory.createCar(type);// 與簡(jiǎn)單工廠的區(qū)別在這里car.prepare();car.fabricate();car.detect();System.out.println("A " + car.getName() + " car is ready.");return car;}}
6.工廠方法(Factory Method)模式實(shí)例
在上例的基礎(chǔ)上實(shí)現(xiàn):
- public class BeijingBmwX3Car extends Car {
- public BeijingBmwX3Car() {
- name = "Beijing BMW X3";
- engines = "One Engine";
- wheels = "Four Wheels";
- lamps = "Two Lamps";
- }
- }
-
- public class BeijingBmwX5Car extends Car {
- public BeijingBmwX5Car() {
- name = "Beijing BMW X5";
- engines = "Two Engines";
- wheels = "Four Wheels";
- lamps = "Four Lamps";
- }
- }
-
- public class BeijingBmwX7Car extends Car {
- public BeijingBmwX7Car() {
- name = "Beijing BMW X7";
- engines = "Four Engine";
- wheels = "Four Wheels and One Spare Tyre";
- lamps = "Six Lamps";
- }
- }
-
- public class ShanghaiBmwX3Car extends Car {
- public ShanghaiBmwX3Car() {
- name = "Shanghai BMW X3";
- engines = "One Engine";
- wheels = "Four Wheels";
- lamps = "Two Lamps";
- }
- }
-
- public class ShanghaiBmwX5Car extends Car {
- public ShanghaiBmwX5Car() {
- name = "Shanghai BMW X5";
- engines = "Two Engines";
- wheels = "Four Wheels";
- lamps = "Four Lamps";
- }
- }
-
- public class ShanghaiBmwX7Car extends Car {
- public ShanghaiBmwX7Car() {
- name = "Shanghai BMW X7";
- engines = "Four Engine";
- wheels = "Four Wheels and One Spare Tyre";
- lamps = "Six Lamps";
- }
- }
-
- public abstract class CarSalesShop {
- public Car orderCar(String type) {
- Car car = createCar(type);
- car.prepare();
- car.fabricate();
- car.detect();
-
- System.out.println("A " + car.getName() + " car is ready.");
- return car;
- }
-
-
- public abstract Car createCar(String type);
- }
-
- public class BeijingCarSalesShop extends CarSalesShop {
- @Override
- public Car createCar(String type) {
- if ("bmwx3".equals(type)) {
- return new BeijingBmwX3Car();
- } else if ("bmwx5".equals(type)) {
- return new BeijingBmwX5Car();
- } else if ("bmwx7".equals(type)) {
- return new BeijingBmwX7Car();
- } else
- return null;
- }
- }
-
- public class ShanghaiCarSalesShop extends CarSalesShop {
- @Override
- public Car createCar(String type) {
- if ("bmwx3".equals(type)) {
- return new ShanghaiBmwX3Car();
- } else if ("bmwx5".equals(type)) {
- return new ShanghaiBmwX5Car();
- } else if ("bmwx7".equals(type)) {
- return new ShanghaiBmwX7Car();
- } else
- return null;
- }
- }
public class BeijingBmwX3Car extends Car {public BeijingBmwX3Car() {name = "Beijing BMW X3";engines = "One Engine";wheels = "Four Wheels";lamps = "Two Lamps";}}public class BeijingBmwX5Car extends Car {public BeijingBmwX5Car() {name = "Beijing BMW X5";engines = "Two Engines";wheels = "Four Wheels";lamps = "Four Lamps";}}public class BeijingBmwX7Car extends Car {public BeijingBmwX7Car() {name = "Beijing BMW X7";engines = "Four Engine";wheels = "Four Wheels and One Spare Tyre";lamps = "Six Lamps";}}public class ShanghaiBmwX3Car extends Car {public ShanghaiBmwX3Car() {name = "Shanghai BMW X3";engines = "One Engine";wheels = "Four Wheels";lamps = "Two Lamps";}}public class ShanghaiBmwX5Car extends Car {public ShanghaiBmwX5Car() {name = "Shanghai BMW X5";engines = "Two Engines";wheels = "Four Wheels";lamps = "Four Lamps";}}public class ShanghaiBmwX7Car extends Car {public ShanghaiBmwX7Car() {name = "Shanghai BMW X7";engines = "Four Engine";wheels = "Four Wheels and One Spare Tyre";lamps = "Six Lamps";}}public abstract class CarSalesShop {public Car orderCar(String type) {Car car = createCar(type);car.prepare();car.fabricate();car.detect();System.out.println("A " + car.getName() + " car is ready.");return car;}// 工廠方法接口public abstract Car createCar(String type);}public class BeijingCarSalesShop extends CarSalesShop {@Overridepublic Car createCar(String type) {if ("bmwx3".equals(type)) {return new BeijingBmwX3Car();} else if ("bmwx5".equals(type)) {return new BeijingBmwX5Car();} else if ("bmwx7".equals(type)) {return new BeijingBmwX7Car();} elsereturn null;}}public class ShanghaiCarSalesShop extends CarSalesShop {@Overridepublic Car createCar(String type) {if ("bmwx3".equals(type)) {return new ShanghaiBmwX3Car();} else if ("bmwx5".equals(type)) {return new ShanghaiBmwX5Car();} else if ("bmwx7".equals(type)) {return new ShanghaiBmwX7Car();} elsereturn null;}}
7.抽象工廠(Abstract Factory)模式實(shí)例
-
- public interface Engine {
- public int getHorsepower();
- }
-
- public class BeijingEngine implements Engine {
- public int getHorsepower() {
- return 200;
- }
- }
-
- public class ShanghaiEngine implements Engine {
- public int getHorsepower() {
- return 230;
- }
- }
-
-
- public interface Wheel {
- public double getDiameter();
- }
-
- public class BeijingWheel implements Wheel {
- public double getDiameter() {
- return 80;
- }
- }
-
- public class ShanghaiWheel implements Wheel {
- public double getDiameter() {
- return 95;
- }
- }
-
-
- public interface Lamp {
- public int getPower();
- }
-
- public class BeijingLamp implements Lamp {
- public int getPower() {
- return 60;
- }
- }
-
- public class ShanghaiLamp implements Lamp {
- public int getPower() {
- return 80;
- }
- }
-
-
- public interface PartsFactory {
- public Engine createEngine();
-
- public Wheel createWheel();
-
- public Lamp createLamp();
- }
-
- public class BeijingPartsFactory implements PartsFactory {
- public Engine createEngine() {
- return new BeijingEngine();
- }
-
- public Lamp createLamp() {
- return new BeijingLamp();
- }
-
- public Wheel createWheel() {
- return new BeijingWheel();
- }
- }
-
- public class ShanghaiPartsFactory implements PartsFactory {
- public Engine createEngine() {
- return new ShanghaiEngine();
- }
-
- public Lamp createLamp() {
- return new ShanghaiLamp();
- }
-
- public Wheel createWheel() {
- return new ShanghaiWheel();
- }
- }
-
- public abstract class Car {
- protected String name;
-
- protected Engine[] engines;
-
- protected Wheel[] wheels;
-
- protected Lamp[] lamps;
-
- public void prepare() {
- System.out.println("Preparing " + name);
- System.out.println("Get engines ready...");
- System.out.println("Get wheels ready...");
- System.out.println("Get lamps ready...");
- }
-
-
- public void fabricate() {
- System.out.println("Adding engines.");
- System.out.println("Adding wheels.");
- System.out.println("Adding lamps.");
- }
-
-
- public void detect() {
- System.out.println("Detect the car.");
- }
-
- public String getName() {
- return name;
- }
- }
-
- public class BmwX3Car extends Car {
- private PartsFactory factory;
-
- public BmwX3Car(PartsFactory factory) {
- this.factory = factory;
-
- name = "BMW X3";
-
- engines = new Engine[] { this.factory.createEngine() };
-
- wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),
- this.factory.createWheel() };
-
- lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp() };
- }
- }
-
- public class BmwX5Car extends Car {
- private PartsFactory factory;
-
- public BmwX5Car(PartsFactory factory) {
- this.factory = factory;
-
- name = "BMW X5";
-
- engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine() };
-
- wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),
- this.factory.createWheel() };
-
- lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),
- this.factory.createLamp() };
- }
- }
-
- public class BmwX7Car extends Car {
- private PartsFactory factory;
-
- public BmwX7Car(PartsFactory factory) {
- this.factory = factory;
-
- name = "BMW X7";
-
- engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine(), this.factory.createEngine(),
- this.factory.createEngine() };
-
- wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),
- this.factory.createWheel(), this.factory.createWheel() };
-
- lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),
- this.factory.createLamp() };
- }
- }
-
- public abstract class CarSalesShop {
- public Car orderCar(String type) {
- Car car = createCar(type);
- car.prepare();
- car.fabricate();
- car.detect();
-
- System.out.println("A " + car.getName() + " car is ready.");
- return car;
- }
-
-
- public abstract Car createCar(String type);
- }
-
- public class BeijingCarSalesShop extends CarSalesShop {
- @Override
- public Car createCar(String type) {
- PartsFactory factory = new BeijingPartsFactory();
- if ("bmwx3".equals(type)) {
- return new BmwX3Car(factory);
- } else if ("bmwx5".equals(type)) {
- return new BmwX5Car(factory);
- } else if ("bmwx7".equals(type)) {
- return new BmwX7Car(factory);
- } else
- return null;
- }
- }
-
- public class ShanghaiCarSalesShop extends CarSalesShop {
- @Override
- public Car createCar(String type) {
- PartsFactory factory = new ShanghaiPartsFactory();
- if ("bmwx3".equals(type)) {
- return new BmwX3Car(factory);
- } else if ("bmwx5".equals(type)) {
- return new BmwX5Car(factory);
- } else if ("bmwx7".equals(type)) {
- return new BmwX7Car(factory);
- } else
- return null;
- }
- }
// 發(fā)動(dòng)機(jī)接口public interface Engine {public int getHorsepower();}public class BeijingEngine implements Engine {public int getHorsepower() {return 200;}}public class ShanghaiEngine implements Engine {public int getHorsepower() {return 230;}}// 車(chē)輪接口public interface Wheel {public double getDiameter();}public class BeijingWheel implements Wheel {public double getDiameter() {return 80;}}public class ShanghaiWheel implements Wheel {public double getDiameter() {return 95;}}// 車(chē)燈接口public interface Lamp {public int getPower();}public class BeijingLamp implements Lamp {public int getPower() {return 60;}}public class ShanghaiLamp implements Lamp {public int getPower() {return 80;}}// 汽車(chē)零件制造工廠public interface PartsFactory {public Engine createEngine();public Wheel createWheel();public Lamp createLamp();}public class BeijingPartsFactory implements PartsFactory {public Engine createEngine() {return new BeijingEngine();}public Lamp createLamp() {return new BeijingLamp();}public Wheel createWheel() {return new BeijingWheel();}}public class ShanghaiPartsFactory implements PartsFactory {public Engine createEngine() {return new ShanghaiEngine();}public Lamp createLamp() {return new ShanghaiLamp();}public Wheel createWheel() {return new ShanghaiWheel();}}public abstract class Car {protected String name;// 名稱protected Engine[] engines;// 發(fā)動(dòng)機(jī)protected Wheel[] wheels;// 車(chē)輪protected Lamp[] lamps;// 車(chē)燈public void prepare() {System.out.println("Preparing " + name);System.out.println("Get engines ready...");System.out.println("Get wheels ready...");System.out.println("Get lamps ready...");}// 組裝public void fabricate() {System.out.println("Adding engines.");System.out.println("Adding wheels.");System.out.println("Adding lamps.");}// 檢測(cè)public void detect() {System.out.println("Detect the car.");}public String getName() {return name;}}public class BmwX3Car extends Car {private PartsFactory factory;public BmwX3Car(PartsFactory factory) {this.factory = factory;name = "BMW X3";// 一個(gè)發(fā)動(dòng)機(jī)engines = new Engine[] { this.factory.createEngine() };// 四個(gè)車(chē)輪wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),this.factory.createWheel() };// 兩個(gè)車(chē)燈lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp() };}}public class BmwX5Car extends Car {private PartsFactory factory;public BmwX5Car(PartsFactory factory) {this.factory = factory;name = "BMW X5";// 兩個(gè)發(fā)動(dòng)機(jī)engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine() };// 四個(gè)車(chē)輪wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),this.factory.createWheel() };// 四個(gè)車(chē)燈lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),this.factory.createLamp() };}}public class BmwX7Car extends Car {private PartsFactory factory;public BmwX7Car(PartsFactory factory) {this.factory = factory;name = "BMW X7";// 四個(gè)發(fā)動(dòng)機(jī)engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine(), this.factory.createEngine(),this.factory.createEngine() };// 四個(gè)車(chē)輪和一個(gè)備用胎wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),this.factory.createWheel(), this.factory.createWheel() };// 六個(gè)車(chē)燈lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),this.factory.createLamp() };}}public abstract class CarSalesShop {public Car orderCar(String type) {Car car = createCar(type);car.prepare();car.fabricate();car.detect();System.out.println("A " + car.getName() + " car is ready.");return car;}// 工廠方法接口public abstract Car createCar(String type);}public class BeijingCarSalesShop extends CarSalesShop {@Overridepublic Car createCar(String type) {PartsFactory factory = new BeijingPartsFactory();if ("bmwx3".equals(type)) {return new BmwX3Car(factory);} else if ("bmwx5".equals(type)) {return new BmwX5Car(factory);} else if ("bmwx7".equals(type)) {return new BmwX7Car(factory);} elsereturn null;}}public class ShanghaiCarSalesShop extends CarSalesShop {@Overridepublic Car createCar(String type) {PartsFactory factory = new ShanghaiPartsFactory();if ("bmwx3".equals(type)) {return new BmwX3Car(factory);} else if ("bmwx5".equals(type)) {return new BmwX5Car(factory);} else if ("bmwx7".equals(type)) {return new BmwX7Car(factory);} elsereturn null;}}
8.結(jié)合策略模式優(yōu)化抽象工廠實(shí)例
修改CarSalesShop類(lèi)如下:
- public class CarSalesShop {
- private PartsFactory factory;
-
- public CarSalesShop(PartsFactory factory) {
- this.factory = factory;
- }
-
- public Car orderCar(String type) {
- Car car = createCar(type);
- car.prepare();
- car.fabricate();
- car.detect();
-
- System.out.println("A " + car.getName() + " car is ready.");
- return car;
- }
-
-
- public Car createCar(String type) {
- if ("bmwx3".equals(type)) {
- return new BmwX3Car(factory);
- } else if ("bmwx5".equals(type)) {
- return new BmwX5Car(factory);
- } else if ("bmwx7".equals(type)) {
- return new BmwX7Car(factory);
- } else
- return null;
- }
- }
public class CarSalesShop {private PartsFactory factory;public CarSalesShop(PartsFactory factory) {this.factory = factory;}public Car orderCar(String type) {Car car = createCar(type);car.prepare();car.fabricate();car.detect();System.out.println("A " + car.getName() + " car is ready.");return car;}// 原有的工廠方法接口,現(xiàn)在實(shí)現(xiàn)了它public Car createCar(String type) {if ("bmwx3".equals(type)) {return new BmwX3Car(factory);} else if ("bmwx5".equals(type)) {return new BmwX5Car(factory);} else if ("bmwx7".equals(type)) {return new BmwX7Car(factory);} elsereturn null;}}
這樣修改后,就不再需要BeijingCarSalesShop和ShanghaiCarSalesShop這兩個(gè)類(lèi)。使用方法如下:
- public class Test {
- public static void main(String[] args) {
- PartsFactory factory = new BeijingPartsFactory();
- CarSalesShop shop = new CarSalesShop(factory);
- shop.orderCar("bmwx7");
- }
- }
public class Test {public static void main(String[] args) {PartsFactory factory = new BeijingPartsFactory();CarSalesShop shop = new CarSalesShop(factory);shop.orderCar("bmwx7");}}
這樣修改后,由于引入了策略模式,消除了兩個(gè)兩個(gè)子類(lèi),并且可以通過(guò)增加setPartsFactory()方法達(dá)到運(yùn)行時(shí)改變零件生產(chǎn)工廠的目的。
--END--