打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
《Head First设计模式》阅读笔记.第四章
1.简单工厂(Simple Factory)部分

*针对接口编程可以隔离掉系统以后可能发生的一大堆改变。

*用静态方法定义的工厂被成为静态工厂,这样就不用使用创建对象的方法来实例化对象,使用方便。但是这样做的缺点是无法通过继承来改变创建方法的行为。

*简单工厂不是一种设计模式,但是它比较常用。

2.工厂方法(Factory Method)模式部分

----芝加哥风味匹萨店----
Java代码
  1. public class ChicagoPizzaStore extends PizzaStore {   
  2.     Pizza createPizza(String item) {   
  3.         if ("cheese".equals(item)) {   
  4.             return ChicagoStyleCheesePizza();   
  5.         } else if ("veggie".equals(item)) {   
  6.             return ChicagoStyleVeggiePizza();   
  7.         } else if ("clam".equals(item)) {   
  8.             return ChicagoStyleClamPizza();   
  9.         } else if ("pepperoni".equals(item)) {   
  10.             return ChicagoStylePepperoniPizza();   
  11.         } else  
  12.             return null;   
  13.     }   
  14. }  

------------

----加州风味匹萨店----
Java代码
  1. public class CaliforniaPizzaStore extends PizzaStore {   
  2.     Pizza createPizza(String item) {   
  3.         if ("cheese".equals(item)) {   
  4.             return CaliforniaStyleCheesePizza();   
  5.         } else if ("veggie".equals(item)) {   
  6.             return CaliforniaStyleVeggiePizza();   
  7.         } else if ("clam".equals(item)) {   
  8.             return CaliforniaStyleClamPizza();   
  9.         } else if ("pepperoni".equals(item)) {   
  10.             return CaliforniaStylePepperoniPizza();   
  11.         } else  
  12.             return null;   
  13.     }   
  14. }  

------------

*工厂方法用来处理对象的创建,并将这样的行为封装在子类中。这样,客户程序中关于超类的代码就和子类对象的创建代码解耦(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代码----
Java代码
  1. public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {   
  2.     public Dough createDough() {   
  3.         return new ThickCrustDough();   
  4.     }   
  5.        
  6.     public Sause createSause() {   
  7.         return new  FlumTomatoSause();   
  8.     }   
  9.        
  10.     public Cheese createCheese() {   
  11.         return new Mozzarella();   
  12.     }   
  13.        
  14.     public Veggies[] createVeggies() {   
  15.         return new Beggies[] {new BlackOlives(), new Spinach(), new EggPlant()};   
  16.     }   
  17.        
  18.     public Pepperoni createPepperoni() {   
  19.         return new SlicedPepperoni();   
  20.     }   
  21.        
  22.     public Clam createClam() {   
  23.         return new FrozenClams();   
  24.     }   
  25. }  

------------

*抽象工厂(Abstract Factory)模式同时结合了工厂方法(Factory Method)模式和策略(Strategy)模式。

*工厂方法使用继承:把对象的创建委托(Delegate)给子类,让子类实现工厂方法创建对象。

*抽象工厂使用对象的组合:对象的创建被实现在工厂接口所暴露出来的方法中。

*所有工厂模式都通过减少应用程序和具体类之间的依赖来促进松耦合,即解耦(Decouple)。

4.简单工厂(Simple Factory)实例
Java代码
  1. public abstract class Car {   
  2.     protected String name;// 名称   
  3.   
  4.     protected String engines;// 发动机   
  5.   
  6.     protected String wheels;// 车轮   
  7.   
  8.     protected String lamps;// 车灯   
  9.   
  10.     public void prepare() {   
  11.         System.out.println("Preparing " + name);   
  12.         System.out.println("Get engines ready...");   
  13.         System.out.println("Get wheels ready...");   
  14.         System.out.println("Get lamps ready...");   
  15.     }   
  16.   
  17.     // 组装   
  18.     public void fabricate() {   
  19.         System.out.println("Adding engines.");   
  20.         System.out.println("Adding wheels.");   
  21.         System.out.println("Adding lamps.");   
  22.     }   
  23.   
  24.     // 检测   
  25.     public void detect() {   
  26.         System.out.println("Detect the car.");   
  27.     }   
  28.   
  29.     public String getName() {   
  30.         return name;   
  31.     }   
  32. }   
  33.   
  34. public class BmwX3Car extends Car {   
  35.     public BmwX3Car() {   
  36.         name = "BMW X3";   
  37.         engines = "One Engine";   
  38.         wheels = "Four Wheels";   
  39.         lamps = "Two Lamps";   
  40.     }   
  41. }   
  42.   
  43. public class BmwX5Car extends Car {   
  44.     public BmwX5Car() {   
  45.         name = "BMW X5";   
  46.         engines = "Two Engines";   
  47.         wheels = "Four Wheels";   
  48.         lamps = "Four Lamps";   
  49.     }   
  50. }   
  51.   
  52. public class BmwX7Car extends Car {   
  53.     public BmwX7Car() {   
  54.         name = "BMW X7";   
  55.         engines = "Four Engine";   
  56.         wheels = "Four Wheels and One Spare Tyre";   
  57.         lamps = "Six Lamps";   
  58.     }   
  59. }   
  60.   
  61. public class SimpleCarFactory {   
  62.     // 简单工厂   
  63.     public Car createCar(String type) {   
  64.         if ("bmwx3".equals(type)) {   
  65.             return new BmwX3Car();   
  66.         } else if ("bmwx5".equals(type)) {   
  67.             return new BmwX5Car();   
  68.         } else if ("bmwx7".equals(type)) {   
  69.             return new BmwX7Car();   
  70.         } else  
  71.             return null;   
  72.     }   
  73. }   
  74.   
  75. // 汽车销售店   
  76. public class CarSalesShop {   
  77.     public Car orderCar(String type) {   
  78.         SimpleCarFactory factory = new SimpleCarFactory();   
  79.         Car car = factory.createCar(type);   
  80.         car.prepare();   
  81.         car.fabricate();   
  82.         car.detect();   
  83.   
  84.         System.out.println("A " + car.getName() + " car is ready.");   
  85.         return car;   
  86.     }   
  87. }  


5.静态工厂实例

在上例的基础上实现:
Java代码
  1. public class StaticCarFactory {   
  2.     // 静态工厂   
  3.     public static Car createCar(String type) {   
  4.         if ("bmwx3".equals(type)) {   
  5.             return new BmwX3Car();   
  6.         } else if ("bmwx5".equals(type)) {   
  7.             return new BmwX5Car();   
  8.         } else if ("bmwx7".equals(type)) {   
  9.             return new BmwX7Car();   
  10.         } else  
  11.             return null;   
  12.     }   
  13. }  


修改CarSalesShop的代码:
Java代码
  1. public class CarSalesShop {   
  2.     public Car orderCar(String type) {   
  3.         Car car = StaticCarFactory.createCar(type);// 与简单工厂的区别在这里   
  4.         car.prepare();   
  5.         car.fabricate();   
  6.         car.detect();   
  7.   
  8.         System.out.println("A " + car.getName() + " car is ready.");   
  9.         return car;   
  10.     }   
  11. }  


6.工厂方法(Factory Method)模式实例

在上例的基础上实现:
Java代码
  1. public class BeijingBmwX3Car extends Car {   
  2.     public BeijingBmwX3Car() {   
  3.         name = "Beijing BMW X3";   
  4.         engines = "One Engine";   
  5.         wheels = "Four Wheels";   
  6.         lamps = "Two Lamps";   
  7.     }   
  8. }   
  9.   
  10. public class BeijingBmwX5Car extends Car {   
  11.     public BeijingBmwX5Car() {   
  12.         name = "Beijing BMW X5";   
  13.         engines = "Two Engines";   
  14.         wheels = "Four Wheels";   
  15.         lamps = "Four Lamps";   
  16.     }   
  17. }   
  18.   
  19. public class BeijingBmwX7Car extends Car {   
  20.     public BeijingBmwX7Car() {   
  21.         name = "Beijing BMW X7";   
  22.         engines = "Four Engine";   
  23.         wheels = "Four Wheels and One Spare Tyre";   
  24.         lamps = "Six Lamps";   
  25.     }   
  26. }   
  27.   
  28. public class ShanghaiBmwX3Car extends Car {   
  29.     public ShanghaiBmwX3Car() {   
  30.         name = "Shanghai BMW X3";   
  31.         engines = "One Engine";   
  32.         wheels = "Four Wheels";   
  33.         lamps = "Two Lamps";   
  34.     }   
  35. }   
  36.   
  37. public class ShanghaiBmwX5Car extends Car {   
  38.     public ShanghaiBmwX5Car() {   
  39.         name = "Shanghai BMW X5";   
  40.         engines = "Two Engines";   
  41.         wheels = "Four Wheels";   
  42.         lamps = "Four Lamps";   
  43.     }   
  44. }   
  45.   
  46. public class ShanghaiBmwX7Car extends Car {   
  47.     public ShanghaiBmwX7Car() {   
  48.         name = "Shanghai BMW X7";   
  49.         engines = "Four Engine";   
  50.         wheels = "Four Wheels and One Spare Tyre";   
  51.         lamps = "Six Lamps";   
  52.     }   
  53. }   
  54.   
  55. public abstract class CarSalesShop {   
  56.     public Car orderCar(String type) {   
  57.         Car car = createCar(type);   
  58.         car.prepare();   
  59.         car.fabricate();   
  60.         car.detect();   
  61.   
  62.         System.out.println("A " + car.getName() + " car is ready.");   
  63.         return car;   
  64.     }   
  65.   
  66.     // 工厂方法接口   
  67.     public abstract Car createCar(String type);   
  68. }   
  69.   
  70. public class BeijingCarSalesShop extends CarSalesShop {   
  71.     @Override  
  72.     public Car createCar(String type) {   
  73.         if ("bmwx3".equals(type)) {   
  74.             return new BeijingBmwX3Car();   
  75.         } else if ("bmwx5".equals(type)) {   
  76.             return new BeijingBmwX5Car();   
  77.         } else if ("bmwx7".equals(type)) {   
  78.             return new BeijingBmwX7Car();   
  79.         } else  
  80.             return null;   
  81.     }   
  82. }   
  83.   
  84. public class ShanghaiCarSalesShop extends CarSalesShop {   
  85.     @Override  
  86.     public Car createCar(String type) {   
  87.         if ("bmwx3".equals(type)) {   
  88.             return new ShanghaiBmwX3Car();   
  89.         } else if ("bmwx5".equals(type)) {   
  90.             return new ShanghaiBmwX5Car();   
  91.         } else if ("bmwx7".equals(type)) {   
  92.             return new ShanghaiBmwX7Car();   
  93.         } else  
  94.             return null;   
  95.     }   
  96. }  


7.抽象工厂(Abstract Factory)模式实例
Java代码
  1. // 发动机接口   
  2. public interface Engine {   
  3.     public int getHorsepower();   
  4. }   
  5.   
  6. public class BeijingEngine implements Engine {   
  7.     public int getHorsepower() {   
  8.         return 200;   
  9.     }   
  10. }   
  11.   
  12. public class ShanghaiEngine implements Engine {   
  13.     public int getHorsepower() {   
  14.         return 230;   
  15.     }   
  16. }   
  17.   
  18. // 车轮接口   
  19. public interface Wheel {   
  20.     public double getDiameter();   
  21. }   
  22.   
  23. public class BeijingWheel implements Wheel {   
  24.     public double getDiameter() {   
  25.         return 80;   
  26.     }   
  27. }   
  28.   
  29. public class ShanghaiWheel implements Wheel {   
  30.     public double getDiameter() {   
  31.         return 95;   
  32.     }   
  33. }   
  34.   
  35. // 车灯接口   
  36. public interface Lamp {   
  37.     public int getPower();   
  38. }   
  39.   
  40. public class BeijingLamp implements Lamp {   
  41.     public int getPower() {   
  42.         return 60;   
  43.     }   
  44. }   
  45.   
  46. public class ShanghaiLamp implements Lamp {   
  47.     public int getPower() {   
  48.         return 80;   
  49.     }   
  50. }   
  51.   
  52. // 汽车零件制造工厂   
  53. public interface PartsFactory {   
  54.     public Engine createEngine();   
  55.   
  56.     public Wheel createWheel();   
  57.   
  58.     public Lamp createLamp();   
  59. }   
  60.   
  61. public class BeijingPartsFactory implements PartsFactory {   
  62.     public Engine createEngine() {   
  63.         return new BeijingEngine();   
  64.     }   
  65.   
  66.     public Lamp createLamp() {   
  67.         return new BeijingLamp();   
  68.     }   
  69.   
  70.     public Wheel createWheel() {   
  71.         return new BeijingWheel();   
  72.     }   
  73. }   
  74.   
  75. public class ShanghaiPartsFactory implements PartsFactory {   
  76.     public Engine createEngine() {   
  77.         return new ShanghaiEngine();   
  78.     }   
  79.   
  80.     public Lamp createLamp() {   
  81.         return new ShanghaiLamp();   
  82.     }   
  83.   
  84.     public Wheel createWheel() {   
  85.         return new ShanghaiWheel();   
  86.     }   
  87. }   
  88.   
  89. public abstract class Car {   
  90.     protected String name;// 名称   
  91.   
  92.     protected Engine[] engines;// 发动机   
  93.   
  94.     protected Wheel[] wheels;// 车轮   
  95.   
  96.     protected Lamp[] lamps;// 车灯   
  97.   
  98.     public void prepare() {   
  99.         System.out.println("Preparing " + name);   
  100.         System.out.println("Get engines ready...");   
  101.         System.out.println("Get wheels ready...");   
  102.         System.out.println("Get lamps ready...");   
  103.     }   
  104.   
  105.     // 组装   
  106.     public void fabricate() {   
  107.         System.out.println("Adding engines.");   
  108.         System.out.println("Adding wheels.");   
  109.         System.out.println("Adding lamps.");   
  110.     }   
  111.   
  112.     // 检测   
  113.     public void detect() {   
  114.         System.out.println("Detect the car.");   
  115.     }   
  116.   
  117.     public String getName() {   
  118.         return name;   
  119.     }   
  120. }   
  121.   
  122. public class BmwX3Car extends Car {   
  123.     private PartsFactory factory;   
  124.   
  125.     public BmwX3Car(PartsFactory factory) {   
  126.         this.factory = factory;   
  127.   
  128.         name = "BMW X3";   
  129.         // 一个发动机   
  130.         engines = new Engine[] { this.factory.createEngine() };   
  131.         // 四个车轮   
  132.         wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),   
  133.                 this.factory.createWheel() };   
  134.         // 两个车灯   
  135.         lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp() };   
  136.     }   
  137. }   
  138.   
  139. public class BmwX5Car extends Car {   
  140.     private PartsFactory factory;   
  141.   
  142.     public BmwX5Car(PartsFactory factory) {   
  143.         this.factory = factory;   
  144.   
  145.         name = "BMW X5";   
  146.         // 两个发动机   
  147.         engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine() };   
  148.         // 四个车轮   
  149.         wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),   
  150.                 this.factory.createWheel() };   
  151.         // 四个车灯   
  152.         lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),   
  153.                 this.factory.createLamp() };   
  154.     }   
  155. }   
  156.   
  157. public class BmwX7Car extends Car {   
  158.     private PartsFactory factory;   
  159.   
  160.     public BmwX7Car(PartsFactory factory) {   
  161.         this.factory = factory;   
  162.   
  163.         name = "BMW X7";   
  164.         // 四个发动机   
  165.         engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine(), this.factory.createEngine(),   
  166.                 this.factory.createEngine() };   
  167.         // 四个车轮和一个备用胎   
  168.         wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),   
  169.                 this.factory.createWheel(), this.factory.createWheel() };   
  170.         // 六个车灯   
  171.         lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),   
  172.                 this.factory.createLamp() };   
  173.     }   
  174. }   
  175.   
  176. public abstract class CarSalesShop {   
  177.     public Car orderCar(String type) {   
  178.         Car car = createCar(type);   
  179.         car.prepare();   
  180.         car.fabricate();   
  181.         car.detect();   
  182.   
  183.         System.out.println("A " + car.getName() + " car is ready.");   
  184.         return car;   
  185.     }   
  186.   
  187.     // 工厂方法接口   
  188.     public abstract Car createCar(String type);   
  189. }   
  190.   
  191. public class BeijingCarSalesShop extends CarSalesShop {   
  192.     @Override  
  193.     public Car createCar(String type) {   
  194.         PartsFactory factory = new BeijingPartsFactory();   
  195.         if ("bmwx3".equals(type)) {   
  196.             return new BmwX3Car(factory);   
  197.         } else if ("bmwx5".equals(type)) {   
  198.             return new BmwX5Car(factory);   
  199.         } else if ("bmwx7".equals(type)) {   
  200.             return new BmwX7Car(factory);   
  201.         } else  
  202.             return null;   
  203.     }   
  204. }   
  205.   
  206. public class ShanghaiCarSalesShop extends CarSalesShop {   
  207.     @Override  
  208.     public Car createCar(String type) {   
  209.         PartsFactory factory = new ShanghaiPartsFactory();   
  210.         if ("bmwx3".equals(type)) {   
  211.             return new BmwX3Car(factory);   
  212.         } else if ("bmwx5".equals(type)) {   
  213.             return new BmwX5Car(factory);   
  214.         } else if ("bmwx7".equals(type)) {   
  215.             return new BmwX7Car(factory);   
  216.         } else  
  217.             return null;   
  218.     }   
  219. }  


8.结合策略模式优化抽象工厂实例

修改CarSalesShop类如下:
Java代码
  1. public class CarSalesShop {   
  2.     private PartsFactory factory;   
  3.   
  4.     public CarSalesShop(PartsFactory factory) {   
  5.         this.factory = factory;   
  6.     }   
  7.   
  8.     public Car orderCar(String type) {   
  9.         Car car = createCar(type);   
  10.         car.prepare();   
  11.         car.fabricate();   
  12.         car.detect();   
  13.   
  14.         System.out.println("A " + car.getName() + " car is ready.");   
  15.         return car;   
  16.     }   
  17.   
  18.     // 原有的工厂方法接口,现在实现了它   
  19.     public Car createCar(String type) {   
  20.         if ("bmwx3".equals(type)) {   
  21.             return new BmwX3Car(factory);   
  22.         } else if ("bmwx5".equals(type)) {   
  23.             return new BmwX5Car(factory);   
  24.         } else if ("bmwx7".equals(type)) {   
  25.             return new BmwX7Car(factory);   
  26.         } else  
  27.             return null;   
  28.     }   
  29. }  


这样修改后,就不再需要BeijingCarSalesShop和ShanghaiCarSalesShop这两个类。使用方法如下:
Java代码
  1. public class Test {   
  2.     public static void main(String[] args) {   
  3.         PartsFactory factory = new BeijingPartsFactory();   
  4.         CarSalesShop shop = new CarSalesShop(factory);   
  5.         shop.orderCar("bmwx7");   
  6.     }   
  7. }  


这样修改后,由于引入了策略模式,消除了两个两个子类,并且可以通过增加setPartsFactory()方法达到运行时改变零件生产工厂的目的。

--END--
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java中的简单工厂模式
工厂模式讲解, 引入Spring IOC
终于,月薪过5万了!
设计模式----------工厂(Factory)模式
面向对象(继承中的面试题)
spring的ioc实现和例子
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服