1.简单工厂(Simple Factory)部分
*针对接口编程可以隔离掉系统以后可能发生的一大堆改变。
*用静态方法定义的工厂被成为静态工厂,这样就不用使用创建对象的方法来实例化对象,使用方便。但是这样做的缺点是无法通过继承来改变创建方法的行为。
*简单工厂不是一种设计模式,但是它比较常用。
2.工厂方法(Factory Method)模式部分
----芝加哥风味匹萨店----
- 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;}}
------------
----加州风味匹萨店----
- 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;}}
------------
*工厂方法用来处理对象的创建,并将这样的行为封装在子类中。这样,客户程序中关于超类的代码就和子类对象的创建代码解耦(Decouple)了。
工厂方法的定义:abstract Product factoryMethod(String type);
*所有工厂模式都用来封装对象的创建。
*工厂方法模式(Factory Method Pattern)通过让子类来决定该创建的对象是什么,来达到将对象的创建过程封装的目的。
*在工厂方法模式中包括创建者(Creator)类和产品(Product)类两种类型的类。
----设计谜题解答----
引用
1、与NYPizzaStore和ChicagoPizzaStore平行的是CaliforniaPizzaStore类,继承自PizzaStore类。
2、与NYStyleCheesePizza和ChicagoStyleCheesePizza平行的是CaliforniaStyleCheesePizza、CaliforniaStylePepperoniPizza、CaliforniaStyleClamPizza、CaliforniaStyleVeggiePizza类,继承自Pizza类,被CaliforniaPizzaStore类所依赖。
------------
工厂(Factory Method Pattern)方法模式:定义了一个创建对象的接口,但是由子类来决定要实例化的类是哪一个。它让类把实例化推迟到了子类。
*创建者(Creator)类实现了所有操纵产品的方法,但是不实现工厂方法。
*工厂方法模式可以和策略(Strategy)模式结合起来,在运行时动态地更换工厂类,从而创建不同的产品对象,这是简单工厂所不具有的弹性。
*工厂方法模式可以让客户在实例化对象时,只依赖接口,而不依赖具体的实现类。这符合“针对接口编程,而不是针对实现编程”的软件设计原则。
*如果类A的改变会影响到类B,那么我们说类B“依赖于”类A。
软件设计原则:要依赖抽象,不要依赖具体类。这个原则又被称为“依赖倒置原则(Dependency Inversion Principle)”。
*依赖倒置原则说明不能让高层组件依赖于底层组件,而且它们都应该依赖于抽象。
*遵循依赖倒置原则的三个指导方针:
(1)变量不可以持有具体类的引用。这可以通过使用工厂避开。
(2)不要让类派生自具体类。否则就会依赖具体类,违反了“针对接口编程,而不是针对现实编程”的软件设计原则。
(3)不要覆盖基类中已实现的方法。出现这样的情况就说明基类设计的有问题。
3.抽象工厂(Abstract Factory)模式部分
抽象工厂模式:提供一个接口,用于创建相关或者依赖对象的家族,而不需要明确指定具体类。
*抽象工厂模式是工厂方法模式的演变。工厂方法模式中,创建者只生产一种类型的产品,而抽象工厂模式中,创建者生产一组不同类型的产品。
----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)模式同时结合了工厂方法(Factory Method)模式和策略(Strategy)模式。
*工厂方法使用继承:把对象的创建委托(Delegate)给子类,让子类实现工厂方法创建对象。
*抽象工厂使用对象的组合:对象的创建被实现在工厂接口所暴露出来的方法中。
*所有工厂模式都通过减少应用程序和具体类之间的依赖来促进松耦合,即解耦(Decouple)。
4.简单工厂(Simple Factory)实例
- 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;// 发动机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();} elsereturn 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;}}
5.静态工厂实例
在上例的基础上实现:
- 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 {// 静态工厂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);// 与简单工厂的区别在这里car.prepare();car.fabricate();car.detect();System.out.println("A " + car.getName() + " car is ready.");return car;}}
6.工厂方法(Factory Method)模式实例
在上例的基础上实现:
- 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)模式实例
-
- 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;
- }
- }
// 发动机接口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 {@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.结合策略模式优化抽象工厂实例
修改CarSalesShop类如下:
- 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;}// 原有的工厂方法接口,现在实现了它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这两个类。使用方法如下:
- 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");}}
这样修改后,由于引入了策略模式,消除了两个两个子类,并且可以通过增加setPartsFactory()方法达到运行时改变零件生产工厂的目的。
--END--