设计模式之桥接与工厂模式

本文探讨了设计模式中的桥接模式和工厂模式。通过实例展示了如何在实际编程中应用log4cpp库的工厂模式进行日志处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma once
#include "iostream"

class Implementor
{
public:
    virtual void operatorImp() = 0;
};

class ImplementorA : public Implementor
{
public:
    virtual void operatorImp() {
        std::cout << __FUNCTION__ << ": A" <<std::endl; 
    }
};

class ImplementorB : public Implementor
{
public:
    virtual void operatorImp() {
        std::cout << __FUNCTION__ << " : B" << std::endl;
    }
};

class ImpFactory
{
public:
    static ImpFactory* getInstance() {
        if (imp_factory == NULL)
        {
            imp_factory = new ImpFactory();
            std::cout << __FUNCTION__ << std::endl; 
        }
        return imp_factory;
    }

    static Implementor* makeImp(int type) {
        std::cout << __FUNCTION__ << std::endl; 
        if (type == 1)
        {
            return new ImplementorA();
        }
        else if(type == 2)
        {
            return new ImplementorB();
        }
        else
        {
            return NULL;
        }
    }

    static ImpFactory* imp_factory;
protected:
    ImpFactory() { 
        std::cout << __FUNCTION__ << std::endl; 
    };
};


class Abstract
{
public:
    virtual Implementor* GetImp(int type) {
        std::cout << __FUNCTION__ << std::endl; 
        if (_imp == NULL)
        {
            _imp = ImpFactory::getInstance()->makeImp(type);
        }
        return _imp;
    }
    virtual void Draw() {
        _imp->operatorImp();
    }
protected:
    Implementor* _imp;
};

class RedfinedAbstract : Abstract
{
   void Draw() {
       std::cout << __FUNCTION__ << std::endl;
   }
};


#include "stdafx.h"
#include "Imp.h"

ImpFactory* ImpFactory::imp_factory = NULL;

调用介绍

    Abstract abs;
    abs.GetImp(2);
    abs.Draw();

结果展示:

Abstract::GetImp
ImpFactory::ImpFactory
ImpFactory::getInstance
ImpFactory::makeImp
ImplementorB::operatorImp : B


log4cpp工厂模式的使用可参考如下:

   static LayoutsFactory* layouts_factory_ = 0;\

   std::auto_ptr<Layout> create_simple_layout(const FactoryParams& params);
   std::auto_ptr<Layout> create_basic_layout(const FactoryParams& params);
   std::auto_ptr<Layout> create_pattern_layout(const FactoryParams& params);
   std::auto_ptr<Layout> create_pattern_layout(const FactoryParams& params);
   std::auto_ptr<Layout> create_pass_through_layout(const FactoryParams& params);

   LayoutsFactory& LayoutsFactory::getInstance()
   {
      if (!layouts_factory_)
      {
         std::auto_ptr<LayoutsFactory> lf(new LayoutsFactory);
         lf->registerCreator("simple", &create_simple_layout);
         lf->registerCreator("basic", &create_basic_layout);
         lf->registerCreator("pattern", &create_pattern_layout);
         lf->registerCreator("pass through", &create_pass_through_layout);
         layouts_factory_ = lf.release();
      }

      return *layouts_factory_;
   }

   void LayoutsFactory::registerCreator(const std::string& class_name, create_function_t create_function)
   {
      const_iterator i = creators_.find(class_name);
      if (i != creators_.end())
         throw std::invalid_argument("Layout creator for type name '" + class_name + "' allready registered");

      creators_[class_name] = create_function;
   }

   std::auto_ptr<Layout> LayoutsFactory::create(const std::string& class_name, const params_t& params)
   {
      const_iterator i = creators_.find(class_name);
      if (i == creators_.end())
         throw std::invalid_argument("There is no layout with type name '" + class_name + "'");

      return (*i->second)(params);
   }

   bool LayoutsFactory::registed(const std::string& class_name) const
   {
      return creators_.find(class_name) != creators_.end();
   }

这个是log4cpp采用的工厂模块,将每一个模块关联了名字和函数指针,注册的时候只需要知道某个模块的名字就可以直接注册了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值