Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 两个子类具有相同的F()实现,其他两个子类具有不同的F实现,所有四个子类都派生自同一个类_C++_Design Patterns - Fatal编程技术网

C++ 两个子类具有相同的F()实现,其他两个子类具有不同的F实现,所有四个子类都派生自同一个类

C++ 两个子类具有相同的F()实现,其他两个子类具有不同的F实现,所有四个子类都派生自同一个类,c++,design-patterns,C++,Design Patterns,我有下面的类层次结构 class Base { virtual void F(); } class Derived1 : public Base { void F() { cout<< " one implementation"<<endl; } } class Derived2 : public Base { void F() { cout<< " one implementation"<<endl

我有下面的类层次结构

class Base
{
 virtual void F();
}

class Derived1 : public Base
{
  void F() {
        cout<< " one implementation"<<endl;
  }
}
class Derived2 : public Base
{
  void F() {
        cout<< " one implementation"<<endl;
  }
}
class Derived3 : public Base
{
  void F() {
        cout<< " other implementation"<<endl;
  }
}
class Derived4 : public Base
{
  void F() {
        cout<< " other implementation"<<endl;
  }
}
类基
{
虚空F();
}
类Derived1:公共基
{
void F(){

cout我会使用合成,但这样:

#include <iostream>

// define an interface
class Base
{
 virtual void F() = 0;
};

// define an implementation framework which pulls in templated
// components to do the work
template<class HandleF>
struct Impl : Base
{
    virtual void F() override {
        f_handler(this);
    }

    HandleF f_handler;
};

struct OneImplementationOfF
{
    template<class Self>
    void operator()(Self* self) const
    {
        std::cout<< " one implementation"<< std::endl;
    }
};

struct OtherImplementationOfF
{
    template<class Self>
    void operator()(Self* self) const
    {
        std::cout<< " other implementation"<< std::endl;
    }
};

// now build our classes    
class Derived1 : public Impl<OneImplementationOfF>
{
};

class Derived2 : public Impl<OneImplementationOfF>
{
};

class Derived3 : public Impl<OtherImplementationOfF>
{
};

class Derived4 : public Impl<OtherImplementationOfF>
{
};
#包括
//定义接口
阶级基础
{
虚空F()=0;
};
//定义一个引入模板的实现框架
//组件来完成这项工作
模板
结构Impl:Base
{
虚拟void F()覆盖{
f_handler(本);
}
手持式f_搬运车;
};
结构OneImplementationOfF
{
模板
void运算符()(Self*Self)常量
{

std::Coutma可能是你描述的过于简单。我想到了装饰图案。嗯,不是真的,我正面临着确切的问题,现在我正在考虑重新设计并使其成为未来的证明。我将研究装饰图案谢谢。是的,这可以通过使用装饰图案来实现。
class Base
    {
     virtual void F();
    }

class Base1 : public Base {
    void F() {
            cout<< " one implementation"<<endl;
      }
}

class Base2 : public Base {
    void F() {
            cout<< " other implementation"<<endl;
      }
}

    class Derived1 
    {
      Base * Fer;
      Derived1()
      {
        Fer = new Base1();
      }
    }
    class Derived2
    {
      Base * Fer;
      Derived1()
      {
        Fer = new Base1();
      }
    }
    class Derived3 
    {
      Base * Fer;
      Derived1()
      {
        Fer = new Base2();
      }
    }
    class Derived4
    {
      Base * Fer;
      Derived1()
      {
        Fer = new Base2();
      }
    }
#include <iostream>

// define an interface
class Base
{
 virtual void F() = 0;
};

// define an implementation framework which pulls in templated
// components to do the work
template<class HandleF>
struct Impl : Base
{
    virtual void F() override {
        f_handler(this);
    }

    HandleF f_handler;
};

struct OneImplementationOfF
{
    template<class Self>
    void operator()(Self* self) const
    {
        std::cout<< " one implementation"<< std::endl;
    }
};

struct OtherImplementationOfF
{
    template<class Self>
    void operator()(Self* self) const
    {
        std::cout<< " other implementation"<< std::endl;
    }
};

// now build our classes    
class Derived1 : public Impl<OneImplementationOfF>
{
};

class Derived2 : public Impl<OneImplementationOfF>
{
};

class Derived3 : public Impl<OtherImplementationOfF>
{
};

class Derived4 : public Impl<OtherImplementationOfF>
{
};