何行かのコードを手当たり次第に書いた.

7866 ワード


COMを見ているとついつい1行のコードを書いてしまいましたが、書き終わると工場メソッドモードでした...
余計なことは言わない
 
 
#include "stdafx.h" #include <iostream>

using namespace std; class InterfaceA { public: InterfaceA(); virtual ~InterfaceA(); virtual void Func1() = 0; virtual void Func2() = 0; }; InterfaceA::InterfaceA(){} InterfaceA::~InterfaceA(){} class ComponetB : public InterfaceA { public: ComponetB(); virtual ~ComponetB(); virtual void Func1(); virtual void Func2(); }; ComponetB::ComponetB(){cout<<" B "<<endl;} ComponetB::~ComponetB(){cout<<" B "<<endl;} void ComponetB::Func1(){cout<<"CompoentB::Func1 is called 
";} void ComponetB::Func2(){cout<<"ComponetB::Func2 is called
";} class ComponetC : public InterfaceA { public: ComponetC(); virtual ~ComponetC(); virtual void Func1(); virtual void Func2(); }; ComponetC::ComponetC(){cout<<" C "<<endl;} ComponetC::~ComponetC(){cout<<" C "<<endl;} void ComponetC::Func1(){cout<<"ComponentC::Fucn1 is called
";} void ComponetC::Func2(){cout<<"ComponetC::Func2 is called
";} class IFactory { public: IFactory(); virtual ~IFactory(); virtual InterfaceA* CreateInstance() = 0; protected: InterfaceA* m_ptrIA; }; IFactory::IFactory() : m_ptrIA(NULL){} IFactory::~IFactory(){if(NULL != m_ptrIA) delete m_ptrIA; m_ptrIA = NULL;} class FactoryB : public IFactory { public: FactoryB(); virtual ~FactoryB(); virtual InterfaceA* CreateInstance(); }; FactoryB::FactoryB() : IFactory(){} FactoryB::~FactoryB(){if(NULL != m_ptrIA) delete m_ptrIA; m_ptrIA = NULL;} InterfaceA* FactoryB::CreateInstance(){return (m_ptrIA = new ComponetB());} class FactoryC : public IFactory { public: FactoryC(); virtual ~FactoryC(); virtual InterfaceA* CreateInstance(); }; FactoryC::FactoryC() : IFactory(){} FactoryC::~FactoryC() {if(NULL != m_ptrIA) delete m_ptrIA; m_ptrIA = NULL;} InterfaceA* FactoryC::CreateInstance(){return (m_ptrIA = new ComponetC());} int _tmain(int argc, _TCHAR* argv[]) { IFactory* pFactory = new FactoryB(); InterfaceA* pInterface = pFactory->CreateInstance(); pInterface->Func1(); cout<<endl; pInterface->Func2(); cout<<endl<<endl; delete pFactory; pFactory = new FactoryC(); pInterface = pFactory->CreateInstance(); pInterface->Func1(); cout<<endl; pInterface->Func2(); delete pFactory; return 0; }