Posts 【创建型】抽象工厂模式详解
Post
Cancel

【创建型】抽象工厂模式详解

【创建型】抽象工厂模式

定义

1
2
3
4
5
6
7
8
9
10
定义:多个抽象产品类,派生出多个具体产品类;一个抽象工厂类,派生出多个具体工厂类;每个具体工厂类可创建多个具体产品类的实例。

工厂方法模式是简单工厂模式的升级版

抽象工厂模式有四个要素:

    1.一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有形态的工厂模式都是重要的。
    2.这个系统有多于一个的产品族,而系统只消费其中某一产品族。
    3.同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。
    4.系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。

UML图


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
          _ _ _ _ _ _
         |           |
         |   Client  |
         |_ _ _ _ _ _|
               |
      ┌-----------------┐
 _ _ _↓_ _ _       _ _ _↓_ _ _
|           |     |           |
| IFactory  |     | IProduct  |
|_ _ _ _ _ _|     |_ _ _ _ _ _|
      ↑                 ↑
      |                 |
 _ _ _|_ _ _       _ _ _|_ _ _
|           |     |           |
|  Factory  |----→|  Product  |
|_ _ _ _ _ _|     |_ _ _ _ _ _|

代码

1
2
3
4
5
public interface CPU{

    public void createCPU();

}

1
2
3
4
public interface Monitor{

    public void createMonitor();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Intel implements CPU{

    public void createCPU(){

        System.out.println( "Intel company produce CPU of intel.");
    }
} ---

public class AMD implements CPU{

    public void createCPU(){

        System.out.println("AMD company produce CPU of AMD.");
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Dell implements Monitor{

    public void createMonitor(){

        System.out.println("Creating... Dell monitor!");
    }
} ---

public class Samsung implements Monitor{

    public void createMonitor(){

        System.out.println("Creating... Samsung monitor!");
    }
}

1
2
3
4
5
6
7
abstract class FactoryX{

    abstract CPU needIntelCPU();

    abstract Monitor needSamsungMonitor();

}

1
2
3
4
5
6
7
abstract class FactoryY{

    abstract CPU needAMDCPU();

    abstract Monitor needDellMonitor();

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Lenovo extends FactoryX{

    public CPU needIntelCPU(){

        return new Intel();
    }


    public Monitor needSamsungMonitor(){

        return new Samsung();
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
public class Apple extends FactoryY{

    public CPU needAMDCPU(){

        return new AMD();
    }

    public Monitor needDellMonitor(){

        return new Dell();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Client{

    public static void main(String[] args){


        Lenovo  lenovoComputer = new Lenovo();

        lenovoComputer.needIntelCPU().createCPU();

        lenovoComputer.needSamsungMonitor().createMonitor();



        Apple  macComputer = new Apple();

        macComputer.needAMDCPU().createCPU();

        macComputer.needDellMonitor().createMonitor();


    }

}

运行结果:

Intel company produce CPU of intel.

Creating... Samsung monitor!


AMD company produce CPU of AMD.

Creating... Dell monitor!
------------------------------------

应用


JDK中抽象工厂模式的的应用:

• java.util.Calendar#getInstance()

• java.util.Arrays#asList()

• java.util.ResourceBundle#getBundle()

• java.net.URL#openConnection()

• java.sql.DriverManager#getConnection()

• java.sql.Connection#createStatement()

• java.sql.Statement#executeQuery()

• java.text.NumberFormat#getInstance()

• java.lang.management.ManagementFactory (所有getXXX()方法)

• java.nio.charset.Charset#forName()

• javax.xml.parsers.DocumentBuilderFactory#newInstance()

• javax.xml.transform.TransformerFactory#newInstance()

• javax.xml.xpath.XPathFactory#newInstance()

Arrays#asList()

1
2
3
4
5
6
7
8
9
10
11
public class Arrays{

    .....

    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }

    ......

}
This post is licensed under CC BY 4.0 by the author.

【创建型】工厂方法模式详解

【创建型】建造者模式详解