AbstractFactory模式解決的問題是創建一組相關或者相互依賴的對象。
我們以一個電腦產品的例子來說明。
我們現在要生產電腦產品,假設電腦產品現在只有臺式機及筆記本兩種,我們需要建一個工廠用來生產電腦產品,而工廠中可以生產不同品牌的電腦,對于每個品牌,我們分別建立相應的品牌工廠,負責生產各自的品牌產品,假設現在有DELL及IBM兩個品牌工廠,那么現在每個工廠都可以生產各自的臺式機及筆記本了。
其類圖如下:

以下是實現代碼:
//DesktopProduct.h
class DesktopProduct
{
public:
virtual ~DesktopProduct();
protected:
DesktopProduct();
};
//DesktopProduct.cpp
#include "stdafx.h"
#include "DesktopProduct.h"
DesktopProduct::DesktopProduct()
{
}
DesktopProduct::~DesktopProduct()
{
}
//DELLDesktopProduct.h
#include "DesktopProduct.h"
class DELLDesktopProduct : public DesktopProduct
{
public:
DELLDesktopProduct();
virtual ~DELLDesktopProduct();
};
//DELLDesktopProduct.cpp
#include "stdafx.h"
#include "DELLDesktopProduct.h"
#include <iostream>
using namespace std;
DELLDesktopProduct::DELLDesktopProduct()
{
cout << "創建DELL臺式機" << endl;
}
DELLDesktopProduct::~DELLDesktopProduct()
{
}
//IBMDesktopProduct.h
#include "DesktopProduct.h"
class IBMDesktopProduct : public DesktopProduct
{
public:
IBMDesktopProduct();
virtual ~IBMDesktopProduct();
};
//IBMDesktopProduct.cpp
#include "stdafx.h"
#include "IBMDesktopProduct.h"
#include <iostream>
using namespace std;
IBMDesktopProduct::IBMDesktopProduct()
{
cout << "創建IBM臺式機" << endl;
}
IBMDesktopProduct::~IBMDesktopProduct()
{
}
//NotebookProduct.h
class NotebookProduct
{
public:
virtual ~NotebookProduct();
protected:
NotebookProduct();
};
//NotebookProduct.cpp
#include "stdafx.h"
#include "NotebookProduct.h"
NotebookProduct::NotebookProduct()
{
}
NotebookProduct::~NotebookProduct()
{
}
//DELLNotebookProduct.h
#include "NotebookProduct.h"
class DELLNotebookProduct : public NotebookProduct
{
public:
DELLNotebookProduct();
virtual ~DELLNotebookProduct();
};
//DELLNotebookProduct.cpp
#include "stdafx.h"
#include "DELLNotebookProduct.h"
#include <iostream>
using namespace std;
DELLNotebookProduct::DELLNotebookProduct()
{
cout << "創建DELL筆記本" << endl;
}
DELLNotebookProduct::~DELLNotebookProduct()
{
}
//IBMNotebookProduct.h
#include "NotebookProduct.h"
class IBMNotebookProduct : public NotebookProduct
{
public:
IBMNotebookProduct();
virtual ~IBMNotebookProduct();
};
//IBMNotebookProduct.cpp
using namespace std;
IBMNotebookProduct::IBMNotebookProduct()
{
cout << "創建IBM筆記本" << endl;
}
IBMNotebookProduct::~IBMNotebookProduct()
{
}
//AbstractFactory.h
class DesktopProduct;
class NotebookProduct;
class AbstractFactory
{
public:
virtual ~AbstractFactory();
virtual DesktopProduct* CreateDesktopProduct() = 0;
virtual NotebookProduct* CreateNotebookProduct() = 0;
protected:
AbstractFactory();
};
//AbstractFactory.cpp
#include "stdafx.h"
#include "AbstractFactory.h"
AbstractFactory::AbstractFactory()
{
}
AbstractFactory::~AbstractFactory()
{
}
//DELLFactory.h
#include "AbstractFactory.h"
class DELLFactory : public AbstractFactory
{
public:
DELLFactory();
virtual ~DELLFactory();
DesktopProduct* CreateDesktopProduct();
NotebookProduct* CreateNotebookProduct();
};
//DELLFactory.cpp
#include "stdafx.h"
#include "DELLFactory.h"
#include "DELLDesktopProduct.h"
#include "DELLNotebookProduct.h"
DELLFactory::DELLFactory()
{
}
DELLFactory::~DELLFactory()
{
}
DesktopProduct* DELLFactory::CreateDesktopProduct()
{
return new DELLDesktopProduct;
}
NotebookProduct* DELLFactory::CreateNotebookProduct()
{
return new DELLNotebookProduct;
}
//IBMFactory.h
#include "AbstractFactory.h"
class IBMFactory : public AbstractFactory
{
public:
IBMFactory();
virtual ~IBMFactory();
DesktopProduct* CreateDesktopProduct();
NotebookProduct* CreateNotebookProduct();
};
//IBMFactory.cpp
#include "stdafx.h"
#include "IBMFactory.h"
#include "IBMDesktopProduct.h"
#include "IBMNotebookProduct.h"
IBMFactory::IBMFactory()
{
}
IBMFactory::~IBMFactory()
{
}
DesktopProduct* IBMFactory::CreateDesktopProduct()
{
return new IBMDesktopProduct;
}
NotebookProduct* IBMFactory::CreateNotebookProduct()
{
return new IBMNotebookProduct;
}
//Main.cpp
#include "stdafx.h"
#include "AbstractFactory.h"
#include "DELLFactory.h"
#include "IBMFactory.h"
#include "DesktopProduct.h"
#include "DELLDeskProduct.h"
#include "IBMDesktopProduct.h"
#include "NotebookProduct.h"
#include "DELLNotebookProduct.h"
#include "IBMNotebookProduct.h"
int main(int argc, char* argv[])
{
AbstractFactory* fac = new DELLFactory();
fac->CreateDesktopProduct();
fac->CreateNotebookProduct();
delete fac;
fac = new IBMFactory();
fac->CreateDesktopProduct();
fac->CreateNotebookProduct();
delete fac;
return 0;
}
最后輸出為:
創建DELL臺式機
創建DELL筆記本
創建IBM臺式機
創建IBM筆記本