AbstractFactory抽象工廠模式(創建型)
作用:
這種模式支持創建不同的對象,這些對象功能接近且一般都是在一起創建的。抽象工廠被具體化成不同的工廠來創建類似產品的不同產品。這種模式將類于使用的客戶端分離以便通過工廠來創建。這樣,各類產品可以方便的變化而不需要改變使用者的結構。
Role
This pattern supports the creation of products that exist in families and are designed to be produced together. The abstract factory can be refined to concrete factories,each of which can create different products of different types and in different combinations. The pattern isolates the product definitions and their class names from the client so that the only way to get one of them is through a factory. For this reason, product families can easily be interchanged or updated without upsetting the structure of the client.
設計:
抽象工廠接口IAbstractFactory,含有創建各類抽象產品abstract products的操作;
工廠1Factory1, 工廠2Factory2,實現了抽象工廠接口IAbstractFactory的創建產品的操作;
抽象產品接口IProductA,IProductB,含有某類產品自有的操作;
具體產品ProductA1, ProductA2, ProductB1, ProductB2,實現了抽象產品接口的具體產品類,這些具體產品類由相應的工廠生產;
舉例:
AbstractFactory???? 生產一種鋼筆
FactoryA?????????????? 英雄鋼筆廠
FactoryB?????????????? 永生鋼筆廠
AbstractProduct???? 鋼筆產品
ProductA1???????????? 英雄金筆
ProductB1???????????? 永生金筆
ProductA2???????????? 英雄軟筆
ProductB2???????????? 永生軟筆
代碼:?


using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
namespace?AbstractFactory
{
????//抽象工廠
????abstract?class?AbstractFactory
????{
????????abstract?public?AbstractWheel?CreatWheel();
????????abstract?public?AbstractOilBox?CreatOilBox();
????}
????abstract?class?AbstractWheel
????{
????????public?AbstractWheel()
????????{
????????????//Console.Write("Create?a?AbstractProduct");
????????}
????}
????abstract?class?AbstractOilBox
????{
????????public?AbstractOilBox()
????????{?}
????}
????class?BMWFactory?:?AbstractFactory
????{
????????public?override?AbstractWheel?CreatWheel()
????????{
????????????return?new?BMWWheel();
????????}
????????public?override?AbstractOilBox?CreatOilBox()
????????{
????????????return?new?BMWOilBox();
????????}
????}
????class?BMWWheel?:?AbstractWheel
????{
????????public?BMWWheel()
????????{
????????????Console.WriteLine("Create?a?BMWwheel");
????????}
????}
????class?BMWOilBox?:?AbstractOilBox
????{
????????public?BMWOilBox()
????????{
????????????Console.WriteLine("Create?a?BMWOilBox");
????????}
????}
????class?BORAWheel:AbstractWheel
?????{
??????public?BORAWheel()
??????{
???????Console.Write("Create?a?BORAWheel");
??????}
?????}
?????class?BORAOilBox:AbstractOilBox
?????{
??????public?BORAOilBox()
??????{
???????Console.Write("Create?a?BORAOilBox");
??????}
?????}
?????????
?????class?BORAFactory:AbstractFactory
?????{
??????public?override?AbstractWheel?CreatWheel()
??????{
???????return?new?BORAWheel();
??????}
??????public?override?AbstractOilBox?CreatOilBox()
??????{
???????return?new?BORAOilBox();
??????}
?????}
????
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????AbstractFactory?factory?=?null;
????????????factory?=?new?BORAFactory();
????????????factory.CreatWheel();
????????????factory.CreatOilBox();
????????????Console.ReadLine();
????????????factory?=?new?BMWFactory();
????????????factory.CreatWheel();
????????????factory.CreatOilBox();
????????????Console.ReadLine();
????????}
????}
}
?
?使用場景:
1、系統不依賴他要使用對象的創建、組成、表示
2、系統需要配置使用多組對象
3、創建對象和工廠必須是強制使用的
4、重點是要暴露接口而不是實現
Use the Abstract Factory pattern when…
?? A system should be independent of how its products are created, composed, and represented.
?? A system can be configured with one of multiple families of products.
?? The constraint requiring products from the same factory to be used together must be enforced.
?? The emphasis is on revealing interfaces, not implementations.
總結:
????? 抽象工廠模式是一種創建型模式,目的是解決實例化對象時所帶來的問題。在現實中系統總是需要變化來實例化不同的對象,因此封裝“創建操作”到抽象工廠,由具體的工廠來實例相應的對象。抽象工廠模式面向接口,將實例化對象的操作封裝起來,不同的工廠都實現實例對象的操作。
????? 在系統中,經常會遇到“一組相互依賴的對象”的創建工作;同時,由于需求的變化,往往存在更多系列對象的創建工作。面對這種問題,避免使用常規的對象創建方法,使用“封裝實例華對象”來避免系統和這種“多系列具體對象創建工作”的緊耦合。
????? 在需求穩定。系統基本無變化的情況下松耦合和緊耦合是沒有區別的,但是需求的變化,要求在某些方面將系統"解耦",抽象工廠就是將對象的實例化和系統解耦。
???? 《設計模式》:提供一個接口,讓該接口負責創建一系列“相關或者相互依賴的對象”,無需指定他們的具體類。Abstract Factory模式的幾個要點:
???? 1、如果沒有應對“多系列對象構建”的需求變化,則沒有必要使用Abstract Factory模式。
???? 2、“系列對象”指的是這項對象之間有相互依賴、或作用的關系。
???? 3、Abstract Factory模式主要在于應對“新系列”的需求變動。缺點是難以應對“新對象”的需求變動。這一點應該注意,就像前面說的,如果我們現在要在加入其他系列的類,代碼的改動會很大。
??? ?4、Abstract Factory模式經常和Factory Method模式共同組合來應對“對象創建”的需求變化。