作用: 定义一个用于创建对象的的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
(1)Product
View Code
1 // Product.h: interface for the CProduct class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #if !defined(AFX_PRODUCT_H__C6FF5C69_D54B_4447_AFBE_36B336CF7E92__INCLUDED_) 6 #define AFX_PRODUCT_H__C6FF5C69_D54B_4447_AFBE_36B336CF7E92__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 class CProduct 13 { 14 public: 15 CProduct(); 16 virtual ~CProduct(); 17 18 }; 19 20 #endif // !defined(AFX_PRODUCT_H__C6FF5C69_D54B_4447_AFBE_36B336CF7E92__INCLUDED_)
View Code
1 // Product.cpp: implementation of the CProduct class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #include "stdafx.h" 6 #include "Product.h" 7 8 ////////////////////////////////////////////////////////////////////// 9 // Construction/Destruction 10 ////////////////////////////////////////////////////////////////////// 11 12 CProduct::CProduct() 13 { 14 15 } 16 17 CProduct::~CProduct() 18 { 19 20 }
(2)ConcreateProduct
View Code
1 // ConcreateProduct.h: interface for the CConcreateProduct class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #if !defined(AFX_CONCREATEPRODUCT_H__1EA01570_5934_4482_8F64_FB28887E8C8C__INCLUDED_) 6 #define AFX_CONCREATEPRODUCT_H__1EA01570_5934_4482_8F64_FB28887E8C8C__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 #include "Product.h" 13 14 class CConcreateProduct : public CProduct 15 { 16 public: 17 CConcreateProduct(); 18 virtual ~CConcreateProduct(); 19 20 }; 21 22 #endif // !defined(AFX_CONCREATEPRODUCT_H__1EA01570_5934_4482_8F64_FB28887E8C8C__INCLUDED_)
View Code
1 // ConcreateProduct.cpp: implementation of the CConcreateProduct class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #include "stdafx.h" 6 #include "ConcreateProduct.h" 7 8 ////////////////////////////////////////////////////////////////////// 9 // Construction/Destruction 10 ////////////////////////////////////////////////////////////////////// 11 12 CConcreateProduct::CConcreateProduct() 13 { 14 cout<<"CConcreateProduct::CConcreateProduct()"<<endl; 15 } 16 17 CConcreateProduct::~CConcreateProduct() 18 { 19 cout<<"CConcreateProduct::~CConcreateProduct()"<<endl; 20 }
(3)Creator
View Code
1 // Creator.h: interface for the CCreator class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #if !defined(AFX_CREATOR_H__DD7DDCD0_5A15_48F3_A119_A6849228A7F3__INCLUDED_) 6 #define AFX_CREATOR_H__DD7DDCD0_5A15_48F3_A119_A6849228A7F3__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 #include "Product.h" 13 14 class CCreator 15 { 16 public: 17 CCreator(); 18 virtual ~CCreator(); 19 20 void AnOperation(); 21 22 protected: 23 virtual CProduct * FactoryMethod() = 0; 24 25 }; 26 27 #endif // !defined(AFX_CREATOR_H__DD7DDCD0_5A15_48F3_A119_A6849228A7F3__INCLUDED_)
View Code
1 // Creator.cpp: implementation of the CCreator class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #include "stdafx.h" 6 #include "Creator.h" 7 8 ////////////////////////////////////////////////////////////////////// 9 // Construction/Destruction 10 ////////////////////////////////////////////////////////////////////// 11 12 CCreator::CCreator() 13 { 14 15 } 16 17 CCreator::~CCreator() 18 { 19 20 } 21 22 void CCreator::AnOperation() 23 { 24 CProduct *p = FactoryMethod(); 25 cout<<"an operation of pruduct"<<endl; 26 }
(4)ConCreateCreator
View Code
1 // ConcreateCreator.h: interface for the CConcreateCreator class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #if !defined(AFX_CONCREATECREATOR_H__C07EA831_1FCE_479B_B729_66CFB626AB30__INCLUDED_) 6 #define AFX_CONCREATECREATOR_H__C07EA831_1FCE_479B_B729_66CFB626AB30__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 #include "Creator.h" 13 14 class CConcreateCreator : public CCreator 15 { 16 public: 17 CConcreateCreator(); 18 virtual ~CConcreateCreator(); 19 20 void AnOperation(); 21 22 protected: 23 virtual CProduct * FactoryMethod(); 24 25 }; 26 27 #endif // !defined(AFX_CONCREATECREATOR_H__C07EA831_1FCE_479B_B729_66CFB626AB30__INCLUDED_)
View Code
1 // ConcreateCreator.cpp: implementation of the CConcreateCreator class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #include "stdafx.h" 6 #include "ConcreateCreator.h" 7 #include "ConcreateProduct.h" 8 9 ////////////////////////////////////////////////////////////////////// 10 // Construction/Destruction 11 ////////////////////////////////////////////////////////////////////// 12 13 CConcreateCreator::CConcreateCreator() 14 { 15 cout<<"CConcreateCreator::CConcreateCreator()"<<endl; 16 } 17 18 CConcreateCreator::~CConcreateCreator() 19 { 20 cout<<"CConcreateCreator::~CConcreateCreator()"<<endl; 21 } 22 23 CProduct * CConcreateCreator::FactoryMethod() 24 { 25 return new CConcreateProduct(); 26 }
(5)Factory
View Code
1 // Factory.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include "ConcreateCreator.h" 6 #include "ConcreateProduct.h" 7 8 int main(int argc, char* argv[]) 9 { 10 CCreator *pTemp = new CConcreateCreator(); 11 12 pTemp->AnOperation(); 13 14 delete pTemp; 15 16 return 0; 17 }
我喜欢一无所有,这样就只能一步一步的创造世界...


浙公网安备 33010602011771号