设计模式之代理模式(c++)

代理模式

#include 
using namespace std;

//抽象工厂
class IFactory
{
    public:
        virtual ~IFactory(){}
        virtual void createProduct()=0;
};

//苹果AirPods耳机工厂
class AppleAirpodsFactory:public IFactory
{
    public:
        void createProduct()
        {
            cout<<"生产苹果AirPods耳机"<createProduct();
        }
    private:
        IFactory* m_factory;
};

//客户端
int main()
{
    IFactory* factory = new AppleAirpodsFactory;
    ILXCompanyProxy* proxy = new ILXCompanyProxy(factory);
    
    proxy->createProduct();

    if(factory)
    {
        delete factory;
        factory = nullptr;
    }

    if(proxy)
    {
        delete proxy;
        proxy = nullptr;
    }

    return 0;
}

 

你可能感兴趣的:(C++,设计模式)