分类: C/C++
2009-01-17 08:47:40

#define _PRODUCT_H_
class Product
...{
public:
virtual void Output()=0;
};
class Pen:public Product
...{
public:
void Output();
};
class Pencil:public Product
...{
public:
void Output();
};
#endif
#ifndef _FACTORY_H_
#define _FACTORY_H_
#include "Product.h"
#include <iostream>
#include <string>
using namespace std;
class Product;
class Pen;
class Pencil;
class Factory
...{
public:
virtual Product* Produce()=0;
};
class PenFactory:public Factory
...{
public:
Product *Produce();
};
class PencilFactory:public Factory
...{
public:
Product *Produce();
};
#endif
#include "Product.h"
void Pen::Output()
...{
cout<<"The pen is produced ";
}
void Pencil::Output()
...{
cout<<"The Pencil is produced ";
}
#include "Factory.h"
Product * PenFactory::Produce()
...{
return new Pen();
}
Product * PencilFactory::Produce()
...{
return new Pencil();
}
#include "Factory.h"
#include "Product.h"
void main()
...{
Factory *factory=new PenFactory();
Product *product=factory->Produce();
product->Output();
factory=new PencilFactory();
product=factory->Produce();
product->Output();
}