全部博文(155)
分类: 项目管理
2007-09-17 14:17:39
/*----------------
No.: one
Filename: AbstractFactory.cpp
Desc: 主程序
----------------*/
#include
#include
#include "Factory.h"
using namespace Factory;
void main()
{
DrvFactory *pResFactory;
cout<<"Please input resolution,format likes \"800*600\" or \"1024*768\""<
cin>>strConfig;
if(strConfig == "800*600")
pResFactory = new LowResolutionFactory();
else if(strConfig == "1024*768")
pResFactory = new HighResolutionFactory();
else
{
cout<<"The format of resolution is error!"<
}
//使用相应分辨率的驱动
//这时你可以忘记具体的分辨率了(被封装起来了),需要用到Driver时至pResFactory"拿来"即可(拿来主义)
DisplayDriver *pDisplayDriver = pResFactory->GetDispDrv();
pDisplayDriver->Display();
delete pDisplayDriver; //用完将其删除,防止memory leak
PrintDriver *pPrintDriver = pResFactory->GetPrintDrv();
pPrintDriver->Print();
delete pPrintDriver;
}
/*------------------------------------------------------
No.: two
Filename: DvrFactory.h
Desc: DrvFactory,LowResolutionFactory,HighResolutionFactory三个类的头文件
-------------------------------------------------------*/
#include "Headers.h"
using namespace Driver;
namespace Factory
{
class DrvFactory
{
public:
virtual ~DrvFactory(){};
virtual DisplayDriver *GetDispDrv()=0;
virtual PrintDriver *GetPrintDrv()=0;
};
class LowResolutionFactory:public DrvFactory
{
public:
DisplayDriver *GetDispDrv();
PrintDriver *GetPrintDrv();
};
class HighResolutionFactory:public DrvFactory
{
public:
DisplayDriver *GetDispDrv();
PrintDriver *GetPrintDrv();
};
}
/*--------------------------
No.: three
Filename: DrvFactory.cpp
Desc: DrvFactory(抽象类,无实现文件),
LowResolutionFactory,HighResolutionFactory二个类的实现文件
---------------------------*/
#include "Factory.h"
#include "headers.h"
using namespace Factory;
using namespace Driver;
//生成低分辨率Driver对象
DisplayDriver* LowResolutionFactory::GetDispDrv()
{
return new LowResolutionDisplayDriver();
}
PrintDriver* LowResolutionFactory::GetPrintDrv()
{
return new LowResolutionPrintDriver();
}
//生成高分辨率Driver对象
DisplayDriver* HighResolutionFactory::GetDispDrv()
{
return new HighResolutionDisplayDriver();
}
PrintDriver* HighResolutionFactory::GetPrintDrv()
{
return new HighResolutionPrintDriver();
}
/*--------------------------------------
No.: four
Filename: DisplayDriver.h
Desc: DisplayDriver,
LowResolutionDisplayDriver,HighResolutionDisplayDriver三个(显示)类头文件
----------------------------------------*/
#include
using namespace std;
namespace Driver
{
class DisplayDriver
{
public:
virtual ~DisplayDriver(){};
virtual void Display()=0;
};
class LowResolutionDisplayDriver:public DisplayDriver
{
public:
void Display();
};
class HighResolutionDisplayDriver:public DisplayDriver
{
public:
void Display();
};
}
/*--------------------
No.: five
Filename: DisplayDriver.cpp
Desc: DisplayDriver(抽象类)
LowResolutionDisplayDriver,HighResolutionDisplayDriver二个(显示)类的实现文件
---------------------*/
#include "DisplayDriver.h"
using namespace Driver;
void LowResolutionDisplayDriver::Display()
{
cout<<"Display picture in Low Resolution(800*600)"<
void HighResolutionDisplayDriver::Display()
{
cout<<"Display picture in High Resolution(1024*768)"<
/*------------
No.: six
Filename: PrintDriver.h
Desc: PrintDriver,
LowResolutionPrintDriver,HighResolutionPrintDriver三个(打印)类的头文件
------------*/
#include
using namespace std;
namespace Driver
{
class PrintDriver
{
public:
virtual ~PrintDriver(){};
virtual void Print()=0;
};
class LowResolutionPrintDriver: public PrintDriver
{
public:
void Print();
};
class HighResolutionPrintDriver: public PrintDriver
{
public:
void Print();
};
}
/*-------------------------------
No.: seven
Filename: PrintDriver.cpp
Desc: PrintDriver(抽象类)
LowResolutionPrintDriver,HighResolutionPrintDriver二个(打印)类实现文件
--------------------------------*/
#include "PrintDriver.h"
using namespace Driver;
void LowResolutionPrintDriver::Print()
{
cout<<"Print the Photo in Low Resolution Printer..."<
void HighResolutionPrintDriver::Print()
{
cout<<"Print the Photo in High Resolution Printer..."<
/*-----------------------------------
No.: eight
Filename:headers.h
Desc: 为了防止重复包含而定义的头文件
------------------------------------*/
#ifndef PRINTDRIVER_H
#define PRINTDRIVER_H
#include "PrintDriver.h"
#endif
#ifndef DISPLAYDRIVER_H
#define DISPLAYDRIVER_H
#include "DisplayDriver.h"
#endif