概念 抽象工廠模式是所有形態的工廠模式中最為抽象和最具一般性的一種形態。抽象工廠模式是指當有多個抽象角色時,使用的一種工廠模式。抽象工廠模式可以向客戶端提供一個介面,使客戶端在不必指定產品的具體的情況下,創建多個產品族中的產品對象。根據里氏替換原則,任何接受父類型的地方,都應當能夠接受子類型。因此, ...
- 概念
抽象工廠模式是所有形態的工廠模式中最為抽象和最具一般性的一種形態。抽象工廠模式是指當有多個抽象角色時,使用的一種工廠模式。抽象工廠模式可以向客戶端提供一個介面,使客戶端在不必指定產品的具體的情況下,創建多個產品族中的產品對象。根據里氏替換原則,任何接受父類型的地方,都應當能夠接受子類型。因此,實際上系統所需要的,僅僅是類型與這些抽象產品角色相同的一些實例,而不是這些抽象產品的實例。換言之,也就是這些抽象產品的具體子類的實例。工廠類負責創建抽象產品的具體子類的實例。
- 優缺點
優點:
1、抽象工廠模式隔離了具體類的生產,客戶不需要知道什麼被創建。
2、當一個產品族中的多個對象被設計成一起工作時,它能保證客戶端始終只使用同一個產品族中的對象,不會造成混淆和混亂。
3、增加新的具體工廠和具體產品的時候會十分方便,不需要修改現有的代碼,符合“開閉原則”。
缺點:
增加新的產品等級結構很複雜,需要修改抽象工廠和所有的具體工廠類,對“開閉原則”的支持呈現傾斜性。
- 實現
下麵我們通過一個例子來說明抽象工廠模式。
說明:
很多游戲裡面都有符文系統,像我們經常玩的英雄聯盟裡面,就是紅色,藍色和黃色的三種符文,然後每種符文又有一級、二級、三級三個等級。我們現在就針對這個來寫抽象工廠模式的例子的代碼。這裡我們只對應紅色符文和藍色符文兩種。首先是兩個介面類:IRedMark,IBlueMark。然後是具體實現類,實現一級二級三級符文各自的屬性,分別為FirstLevelRed,SecondLevelRed,ThirdLevelRed,FirstLevelBlue,SecondLevelBlue,ThirdLevelBlue。然後我們建立一個抽象工廠的介面類IMarkFactory,再然後就是具體的抽象工廠類,FirstLevelFactory,SecondLevelFactory,ThirdLevelFactory。
類圖:
實現代碼:
// 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; }
下麵是運行的結果: