02工厂模式+抽象工厂模式(创建型模式)

• 工厂模式:

  – 实现了创建者和调用者的分离。
  – 详细分类:
    • 简单工厂模式:用来生产同一等级结构中的任意产品。(对于增加新的产品,需要修改已有代码,虽然某种程度不符合设计原则,但实际使用最多)

    • 工厂方法模式:用来生产同一等级结构中的固定产品。(支持增加任意产品,不修改已有类的前提下,通过增加新的工厂类实现扩展)
    • 抽象工厂模式:用来生产不同产品族的全部产品。(对于增加新的产品,无能为力;支持增加产品族,不可以增加产品,可以增加产品族)

  – 应用场景
    • JDK中Calendar的getInstance方法
    • JDBC中Connection对象的获取
    • Hibernate中SessionFactory创建Session
    • spring中IOC容器创建管理bean对象
    • XML解析时的DocumentBuilderFactory创建解析器对象
    • 反射中Class对象的newInstance()

• 面向对象设计的基本原则:

  – OCP(开闭原则,Open-Closed Principle):一个软件的实体应当对扩展开放,对修改关闭。
  – DIP(依赖倒转原则,Dependence Inversion Principle):要针对接口编程,不要针对实现编程。

  – LoD(迪米特法则,Law of Demeter):只与你直接的朋友通信,而避免和陌生人通信。

• 核心本质:

  – 实例化对象,用工厂方法代替new操作。

  – 将选择实现类、创建对象统一管理和控制。从而将调用者跟我们的实现类解耦。

• 简单工厂模式:

  • 要点:
    – 简单工厂模式也叫静态工厂模式,就是工厂类一般是使用静态方法,通过接收的参数的不同来返回不同的对象实例。

    – 对于增加新产品无能为力!不修改代码的话,是无法扩展的。

 1 public interface Car {
 2    void run();
 3 }
 4 
 5 public class KaChe implements Car {
 6    @Override
 7    public void run() {
 8       System.out.println("卡车再跑!");
 9    }
10 }
11 
12 public class QiChe implements Car {
13    @Override
14    public void run() {
15       System.out.println("汽车再跑!");
16    }
17 }
18 
19 /**
20  * 第一种实现方式
21  */
22 public class CarFactory {
23    
24    public static  Car createCar(String type){
25       if("KaChe".equals(type)){
26          return new KaChe();
27       }else if("QiChe".equals(type)){
28          return new QiChe();
29       }else{
30          return null;
31       }
32    }
33 }
34 
35 /**
36  *  第二种实现方式
37  */
38 public class CarFactory2 {
39    
40    public static  Car createQiChe(){
41       return new QiChe();
42    }
43    public static  Car createC(){
44       return new KaChe();
45    }
46 }
47 
48 /**
49  * 第一种实现方式测试
50  */
51 public class Client {
52    public static void main(String[] args) {
53       Car c1 =CarFactory.createCar("KaChe");
54       Car c2 = CarFactory.createCar("QiChe");
55       c1.run();
56       c2.run();
57    }
58 }
59 
60 /**
61  * 第二种实现方式测试
62  */
63 public class Client2 {
64    public static void main(String[] args) {
65       Car c1 =CarFactory2.createKaChe();
66       Car c2 = CarFactory2.createQiChe();
67       c1.run();
68       c2.run();
69    }
70 }
View Code

• 工厂方法模式要点:

  – 为了避免简单工厂模式的缺点,不完全满足OCP。

  – 工厂方法模式和简单工厂模式最大的不同在于,简单工厂模式只有一个(对于一个项目或者一个独立模块而言)工厂类,而工厂方法模式有一组实现了相同接口的工厂类。

 1 public interface Car {
 2    void run();
 3 }
 4 
 5 public class KaChe implements Car {
 6    @Override
 7    public void run() {
 8       System.out.println("卡车再跑!");
 9    }
10 }
11 
12 public class QiChe implements Car {
13    @Override
14    public void run() {
15       System.out.println("汽车再跑!");
16    }
17 }
18 
19 public interface CarFactory {
20    Car createCar();
21 }
22 
23 public class KaCheFactory implements CarFactory {
24    @Override
25    public Car createCar() {
26       return new KaChe();
27    }
28 }
29 
30 public class QiCheFactory implements CarFactory {
31    @Override
32    public Car createCar() {
33       return new QiChe();
34    }
35 }
36 
37 public class Client {
38    public static void main(String[] args) {
39       Car c1 = new KaCheFactory().createCar();
40       Car c2 = new QiCheFactory().createCar();
41       c1.run();
42       c2.run();
43    }
44 }
View Code

简单工厂模式和工厂方法模式PK:

  – 结构复杂度:从这个角度比较,显然简单工厂模式要占优。简单工厂模式只需一个工厂类,而工厂方法模式的工厂类随着产品类个数增加而增加,这无疑会使类的个数越来越多,从而增加了结构的复杂程度。
  – 代码复杂度:代码复杂度和结构复杂度是一对矛盾,既然简单工厂模式在结构方面相对简洁,那么它在代码方面肯定是比工厂方法模式复杂的了。简单工厂模式的工厂类随着产品类的增加需要增加很多方法(或代码),而工厂方法模式每个具体工厂类只完成单一任务,代码简洁。
  – 客户端编程难度:工厂方法模式虽然在工厂类结构中引入了接口从而满足了OCP,但是在客户端编码中需要对工厂类进行实例化。而简单工厂模式的工厂类是个静态类,在客户端无需实例化,这无疑是个吸引人的优点。
  – 管理上的难度(这是个关键的问题):我们先谈扩展。众所周知,工厂方法模式完全满足OCP,即它有非常良好的扩展性。那是否就说明了简单工厂模式就没有扩展性呢?答案是否定的。简单工厂模式同样具备良好的扩展性——扩展的时候仅需要修改少量的代码(修改工厂类的代码)就可以满足扩展性的要求了。尽管这没有完全满足OCP,但我们不需要太拘泥于设计理论,要知道,sun提供的java官方工具包中也有想到多没有满足OCP的例子啊。然后我们从维护性的角度分析下。假如某个具体产品类需要进行一定的修改,很可能需要修改对应的工厂类。当同时需要修改多个产品类的时候,对工厂类的修改会变得相当麻烦(对号入座已经是个问题了)。反而简单工厂没有这些麻烦,当多个产品类需要修改是,简单工厂模式仍然仅仅需要修改唯一的工厂类(无论怎样都能改到满足要求吧?大不了把这个类重写)。
  – 根据设计理论建议:工厂方法模式。但实际上,我们一般都用简单工厂模式。

• 抽象工厂模式

  – 用来生产不同产品族的全部产品。(对于增加新的产品,无能为力;支持增加产品族)

  – 抽象工厂模式是工厂方法模式的升级版本,在有多个业务品种、业务分类时,通过抽象工厂模式产生需要的对象是一种非常好的解决方式

  1 public interface Tyre {
  2    void revolve();
  3 }
  4 
  5 public class LuxuryTyre implements Tyre {
  6    @Override
  7    public void revolve() {
  8       System.out.println("旋转不磨损!");
  9    }
 10 }
 11 
 12 public class LowTyre implements Tyre {
 13    @Override
 14    public void revolve() {
 15       System.out.println("旋转磨损快!");
 16    }
 17 }
 18 
 19 public interface Seat {
 20    void massage();
 21 }
 22 
 23 public class LuxurySeat implements Seat {
 24    @Override
 25    public void massage() {
 26       System.out.println("可以自动按摩!");
 27    }
 28 }
 29 
 30 public class LowSeat implements Seat {
 31    @Override
 32    public void massage() {
 33       System.out.println("不能按摩!");
 34    }
 35 }
 36 
 37 public interface Engine {
 38    void run();
 39    void start();
 40 }
 41 
 42 public class LuxuryEngine implements Engine{
 43    @Override
 44    public void run() {
 45       System.out.println("转的快!");
 46    }
 47 
 48    @Override
 49    public void start() {
 50       System.out.println("启动快!可以自动启停!");
 51    }
 52 }
 53 
 54 public class LowEngine implements Engine{
 55    @Override
 56    public void run() {
 57       System.out.println("转的慢!");
 58    }
 59    
 60    @Override
 61    public void start() {
 62       System.out.println("启动慢!");
 63    }
 64 }
 65 
 66 public interface CarFactory {
 67    Engine createEngine();
 68    Seat createSeat();
 69    Tyre createTyre();
 70 }
 71 
 72 public class LowCarFactory implements CarFactory {
 73    @Override
 74    public Engine createEngine() {
 75       return new LowEngine();
 76    }
 77 
 78    @Override
 79    public Seat createSeat() {
 80       return new LowSeat();
 81    }
 82 
 83    @Override
 84    public Tyre createTyre() {
 85       return new LowTyre();
 86    }
 87 }
 88 
 89 public class LuxuryCarFactory implements CarFactory {
 90    @Override
 91    public Engine createEngine() {
 92       return new LuxuryEngine();
 93    }
 94 
 95    @Override
 96    public Seat createSeat() {
 97       return new LuxurySeat();
 98    }
 99 
100    @Override
101    public Tyre createTyre() {
102       return new LuxuryTyre();
103    }
104 }
105 
106 public class Client {
107    public static void main(String[] args) {
108       CarFactory  factory = new LuxuryCarFactory();
109       Engine e = factory.createEngine();
110       e.run();
111       e.start();
112    }
113 }
View Code

 

posted @ 2018-05-27 07:15  jqbai  阅读(103)  评论(0)    收藏  举报