• 随手写了几行代码


    看COM的时候随手写了一行代码,写完发现是工厂方法模式。。。

    废话不多说了

    #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;
    }
  • 相关阅读:
    排序算法(2)-冒泡排序及优化
    [转]Java泛型详解
    [转]Java泛型详解
    02-机器学习_(knn分类算法与应用)
    01-机器学习_(python数据类型详解)
    流量运营项目说明
    数据仓库星型模型vs雪花模型
    数据仓库命名规范
    数据仓库建模
    数据仓库
  • 原文地址:https://www.cnblogs.com/cuish/p/3854448.html
Copyright © 2020-2023  润新知