main.cpp
#include "Factory.h" #include "Product.h" #include <iostream> using namespace std; int main(int argc, char *argv[]) { Factory *fac = new ConcreateFactory(); Product *p = fac->CreateProduct(); return 0; }
Product.h
#ifndef _PRODUCT_H #define _PRODUCT_H class Product { public : virtual ~Product() = 0; protected : Product(); }; class ConcreateProduct :public Product { public : ~ConcreateProduct(); ConcreateProduct(); }; #endif
Product.cpp
#include "Product.h" #include <iostream> using namespace std; Product::Product() {} Product::~Product() {} ConcreateProduct::ConcreateProduct() { cout << "ConcreateProduct..." << endl; } ConcreateProduct::~ConcreateProduct() {}
Factory.h
#ifndef _FACTORY_H #define _FACTORY_H class Product; class Factory { public : virtual ~Factory() = 0; virtual Product *CreateProduct() = 0; protected: Factory(); }; class ConcreateFactory :public Factory { public : ~ConcreateFactory(); ConcreateFactory(); Product *CreateProduct(); }; #endif
Factory.cpp
#include "Factory.h" #include "Product.h" #include <iostream> using namespace std; Factory::Factory() {} Factory::~Factory() {} ConcreateFactory::ConcreateFactory() {} ConcreateFactory::~ConcreateFactory() {} Product *ConcreateFactory::CreateProduct() { return new ConcreateProduct(); }
时间: 2024-10-24 08:43:31