[创建型模式] Factory
Factory.h
//
// Factory.h
// Factory
//
// Created by Cheney Shen on 11-2-20.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#ifndef _FACTORY_H_
#define _FACTORY_H_
class Product;
class Factory
{
public:
virtual ~Factory();
virtual Product* CreateProduct()=0;
protected:
Factory();
private:
};
class ConcreteFactory:public Factory
{
public:
~ConcreteFactory();
ConcreteFactory();
Product* CreateProduct();
protected:
private:
};
#endif //~_FACTORY_H_
Factory.cpp
//
// Factory.cpp
// Factory
//
// Created by Cheney Shen on 11-2-20.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#include "Factory.h"
#include "Product.h"
#include <iostream>
using namespace std;
Factory::Factory()
{
}
Factory::~Factory()
{
}
ConcreteFactory::ConcreteFactory()
{
cout<<"ConcreteFacotry..."<<endl;
}
ConcreteFactory::~ConcreteFactory()
{
}
Product* ConcreteFactory::CreateProduct()
{
return new ConcreteProduct();
}
Product.h
//
// Product.h
// Factory
//
// Created by Cheney Shen on 11-2-20.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#ifndef _PRODUCT_H_
#define _PRODUCT_H_
class Product
{
public:
virtual ~Product()=0;
protected:
Product();
private:
};
class ConcreteProduct : public Product
{
public:
~ConcreteProduct();
ConcreteProduct();
protected:
private:
};
#endif // ~_PRODUCT_H_
Product.cpp
//
// Product.cpp
// Factory
//
// Created by Cheney Shen on 11-2-20.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#include "Product.h"
#include <iostream>
using namespace std;
Product::Product()
{
}
Product::~Product()
{
}
ConcreteProduct::ConcreteProduct()
{
cout<<"ConcreteProduct..."<<endl;
}
ConcreteProduct::~ConcreteProduct()
{
}
main.cpp
//
// main.cpp
// Factory
//
// Created by Cheney Shen on 11-2-20.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#include "Factory.h"
#include "Product.h"
#include <iostream>
using namespace std;
int main (int argc, const char * argv[]) {
Factory* fac = new ConcreteFactory();
Product* p = fac->CreateProduct();
return 0;
}
posted on 2011-03-10 02:48 Cheney Shen 阅读(218) 评论(0) 收藏 举报
浙公网安备 33010602011771号