emptysoul

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

常用链接

留言簿(18)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

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笔记本
posted on 2009-02-07 21:49 emptysoul 阅读(434) 评论(1)  编辑 收藏 引用

Feedback

# re: 设计模式-抽象工厂 2009-02-19 13:17 zhjiawei_cn
写得很好,我可以转载到我的博客上吗?  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理