抽象工廠模式是全部形態的工廠模式中最爲抽象和最具通常性的一種形態。抽象工廠模式是指當有多個抽象角色時,使用的一種工廠模式。抽象工廠模式能夠向客戶端提供一個接口,使客戶端在沒必要指定產品的具體的狀況下,建立多個產品族中的產品對象。根據里氏替換原則,任何接受父類型的地方,都應當可以接受子類型。所以,實際上系統所須要的,僅僅是類型與這些抽象產品角色相同的一些實例,而不是這些抽象產品的實例。換言之,也就是這些抽象產品的具體子類的實例。工廠類負責建立抽象產品的具體子類的實例。spa
優勢:
一、抽象工廠模式隔離了具體類的生產,客戶不須要知道什麼被建立。
二、當一個產品族中的多個對象被設計成一塊兒工做時,它能保證客戶端始終只使用同一個產品族中的對象,不會形成混淆和混亂。
三、增長新的具體工廠和具體產品的時候會十分方便,不須要修改現有的代碼,符合「開閉原則」。
缺點:
增長新的產品等級結構很複雜,須要修改抽象工廠和全部的具體工廠類,對「開閉原則」的支持呈現傾斜性。設計
下面咱們經過一個例子來講明抽象工廠模式。code
說明:對象
不少遊戲裏面都有符文系統,像咱們常常玩的英雄聯盟裏面,就是紅色,藍色和黃色的三種符文,而後每種符文又有一級、二級、三級三個等級。咱們如今就針對這個來寫抽象工廠模式的例子的代碼。這裏咱們只對應紅色符文和藍色符文兩種。首先是兩個接口類:IRedMark,IBlueMark。而後是具體實現類,實現一級二級三級符文各自的屬性,分別爲FirstLevelRed,SecondLevelRed,ThirdLevelRed,FirstLevelBlue,SecondLevelBlue,ThirdLevelBlue。而後咱們創建一個抽象工廠的接口類IMarkFactory,再而後就是具體的抽象工廠類,FirstLevelFactory,SecondLevelFactory,ThirdLevelFactory。blog
類圖:接口
實現代碼:遊戲
// IRedMark.h #pragma once class IRedMark { public: IRedMark(void); virtual ~IRedMark(void); virtual void AddPhysicalDamage() = 0; }; // IRedMark.cpp #include "StdAfx.h" #include "IRedMark.h" IRedMark::IRedMark(void) { } IRedMark::~IRedMark(void) { } // FirstLevelRed.h #pragma once #include "IRedMark.h" class CFirstLevelRed : public IRedMark { public: CFirstLevelRed(void); virtual ~CFirstLevelRed(void); virtual void AddPhysicalDamage(); }; // FirstLevelRed.cpp #include "StdAfx.h" #include "FirstLevelRed.h" CFirstLevelRed::CFirstLevelRed(void) { } CFirstLevelRed::~CFirstLevelRed(void) { } void CFirstLevelRed::AddPhysicalDamage() { printf("Physical Damage +10\n"); } // SecondLevelRed.h #pragma once #include "IRedMark.h" class CSecondLevelRed : public IRedMark { public: CSecondLevelRed(void); virtual ~CSecondLevelRed(void); virtual void AddPhysicalDamage(); }; // SecondLevelRed.cpp #include "StdAfx.h" #include "SecondLevelRed.h" CSecondLevelRed::CSecondLevelRed(void) { } CSecondLevelRed::~CSecondLevelRed(void) { } void CSecondLevelRed::AddPhysicalDamage() { printf("Physical Damage +20\n"); } // ThirdLevelRed.h #pragma once #include "IRedMark.h" class CThirdLevelRed : public IRedMark { public: CThirdLevelRed(void); virtual ~CThirdLevelRed(void); virtual void AddPhysicalDamage(); }; // ThirdLevelRed.cpp #include "StdAfx.h" #include "ThirdLevelRed.h" CThirdLevelRed::CThirdLevelRed(void) { } CThirdLevelRed::~CThirdLevelRed(void) { } void CThirdLevelRed::AddPhysicalDamage() { printf("Physical Damage +30\n"); } // IBlueMark.h #pragma once class IBlueMark { public: IBlueMark(void); virtual ~IBlueMark(void); virtual void AddSpellDamage() = 0; }; // IBlueMark.cpp #include "StdAfx.h" #include "IBlueMark.h" IBlueMark::IBlueMark(void) { } IBlueMark::~IBlueMark(void) { } // FirstLevelBlue.h #pragma once #include "IBlueMark.h" class CFirstLevelBlue : public IBlueMark { public: CFirstLevelBlue(void); virtual ~CFirstLevelBlue(void); virtual void AddSpellDamage(); }; // FirstLevelBlue.cpp #include "StdAfx.h" #include "FirstLevelBlue.h" CFirstLevelBlue::CFirstLevelBlue(void) { } CFirstLevelBlue::~CFirstLevelBlue(void) { } void CFirstLevelBlue::AddSpellDamage() { printf("Spell Damage +10\n"); } // SecondLevelBlue.h #pragma once #include "IBlueMark.h" class CSecondLevelBlue : public IBlueMark { public: CSecondLevelBlue(void); virtual ~CSecondLevelBlue(void); virtual void AddSpellDamage(); }; // SecondLevelBlue.cpp #include "StdAfx.h" #include "SecondLevelBlue.h" CSecondLevelBlue::CSecondLevelBlue(void) { } CSecondLevelBlue::~CSecondLevelBlue(void) { } void CSecondLevelBlue::AddSpellDamage() { printf("Spell Damage +20\n"); } // ThirdLevelBlue.h #pragma once #include "IBlueMark.h" class CThirdLevelBlue : public IBlueMark { public: CThirdLevelBlue(void); virtual ~CThirdLevelBlue(void); virtual void AddSpellDamage(); }; // ThirdLevelBlue.cpp #include "StdAfx.h" #include "ThirdLevelBlue.h" CThirdLevelBlue::CThirdLevelBlue(void) { } CThirdLevelBlue::~CThirdLevelBlue(void) { } void CThirdLevelBlue::AddSpellDamage() { printf("Spell Damage +30\n"); } // IMarkFactory.h #pragma once #include "IRedMark.h" #include "IBlueMark.h" class IMarkFactory { public: IMarkFactory(void); virtual ~IMarkFactory(void); virtual IRedMark* CreateRedMark() = 0; virtual IBlueMark* CreateBlueMark() = 0; }; // IMarkFactory.cpp #include "StdAfx.h" #include "IMarkFactory.h" IMarkFactory::IMarkFactory(void) { } IMarkFactory::~IMarkFactory(void) { } // FirstLevelFactory.h #pragma once #include "IMarkFactory.h" class CFirstLevelFactory : public IMarkFactory { public: CFirstLevelFactory(void); virtual ~CFirstLevelFactory(void); virtual IRedMark* CreateRedMark(); virtual IBlueMark* CreateBlueMark(); }; // FirstLevelFactory.cpp #include "StdAfx.h" #include "FirstLevelFactory.h" #include "FirstLevelRed.h" #include "FirstLevelBlue.h" CFirstLevelFactory::CFirstLevelFactory(void) { } CFirstLevelFactory::~CFirstLevelFactory(void) { } IRedMark* CFirstLevelFactory::CreateRedMark() { return new CFirstLevelRed(); } IBlueMark* CFirstLevelFactory::CreateBlueMark() { return new CFirstLevelBlue(); } // SecondLevelFactory.h #pragma once #include "IMarkFactory.h" class CSecondLevelFactory : public IMarkFactory { public: CSecondLevelFactory(void); ~CSecondLevelFactory(void); virtual IRedMark* CreateRedMark(); virtual IBlueMark* CreateBlueMark(); }; // SecondLevelFactory.cpp #include "StdAfx.h" #include "SecondLevelFactory.h" #include "SecondLevelRed.h" #include "SecondLevelBlue.h" CSecondLevelFactory::CSecondLevelFactory(void) { } CSecondLevelFactory::~CSecondLevelFactory(void) { } IRedMark* CSecondLevelFactory::CreateRedMark() { return new CSecondLevelRed(); } IBlueMark* CSecondLevelFactory::CreateBlueMark() { return new CSecondLevelBlue(); } // ThirdLevelFactory.h #pragma once #include "IMarkFactory.h" class CThirdLevelFactory : public IMarkFactory { public: CThirdLevelFactory(void); virtual ~CThirdLevelFactory(void); virtual IRedMark* CreateRedMark(); virtual IBlueMark* CreateBlueMark(); }; // ThirdLevelFactory.cpp #include "StdAfx.h" #include "ThirdLevelFactory.h" #include "ThirdLevelRed.h" #include "ThirdLevelBlue.h" CThirdLevelFactory::CThirdLevelFactory(void) { } CThirdLevelFactory::~CThirdLevelFactory(void) { } IRedMark* CThirdLevelFactory::CreateRedMark() { return new CThirdLevelRed(); } IBlueMark* CThirdLevelFactory::CreateBlueMark() { return new CThirdLevelBlue(); } // main #include "stdafx.h" #include "stdlib.h" #include "IMarkFactory.h" #include "IRedMark.h" #include "IBlueMark.h" #include "FirstLevelFactory.h" #include "FirstLevelRed.h" #include "FirstLevelBlue.h" #include "SecondLevelFactory.h" #include "SecondLevelRed.h" #include "SecondLevelBlue.h" #include "ThirdLevelFactory.h" #include "ThirdLevelRed.h" #include "ThirdLevelBlue.h" void TestFirstLevel() { IMarkFactory *markFactory = new CFirstLevelFactory(); IRedMark *redMark = markFactory->CreateRedMark(); IBlueMark *blueMark = markFactory->CreateBlueMark(); redMark->AddPhysicalDamage(); blueMark->AddSpellDamage(); delete blueMark; delete redMark; delete markFactory; } void TestSecondLevel() { IMarkFactory *markFactory = new CSecondLevelFactory(); IRedMark *redMark = markFactory->CreateRedMark(); IBlueMark *blueMark = markFactory->CreateBlueMark(); redMark->AddPhysicalDamage(); blueMark->AddSpellDamage(); delete blueMark; delete redMark; delete markFactory; } void TestThirdLevel() { IMarkFactory *markFactory = new CThirdLevelFactory(); IRedMark *redMark = markFactory->CreateRedMark(); IBlueMark *blueMark = markFactory->CreateBlueMark(); redMark->AddPhysicalDamage(); blueMark->AddSpellDamage(); delete blueMark; delete redMark; delete markFactory; } int _tmain(int argc, _TCHAR* argv[]) { printf("----------------\n"); TestFirstLevel(); printf("----------------\n"); TestSecondLevel(); printf("----------------\n"); TestThirdLevel(); printf("----------------\n"); system("pause"); return 0; }
下面是運行的結果:產品