简体   繁体   English

具有多重继承的工厂模式

[英]Factory Pattern with Multiple Inheritance

In this problem I have three pure virtual classes, let's name them A, B, C . 在这个问题中,我有三个纯虚类,我们将它们命名为A,B,C Each of them provides a different set of functionality. 它们每个都提供不同的功能集。 I have another pure virtual class that I will call CommonInterface that does not introduce any new functionality but inherits A, B, and C to make them reachable through a single interface as below: 我有另一个纯虚拟类,我将其称为CommonInterface ,它没有引入任何新功能,但继承了A,B和C,以使它们可以通过单个接口访问,如下所示:

class CommonInterface : public virtual A, public virtual B, public virtual C

These interfaces are defined by a standard document and are not subject to change. 这些接口由标准文档定义,并且不会更改。 They are defined as a factory design pattern, such that, anyone can implement the functionality of CommonInterface according to their needs. 它们被定义为工厂设计模式,因此任何人都可以根据自己的需求实现CommonInterface的功能。

My role is to provide a built-in implementation to this CommonInterface, so that people can use my implementation in case the built-in implementation of the interface is good enough for them. 我的角色是为此CommonInterface提供一个内置的实现,以便人们可以使用我的实现,以防该接口的内置实现对他们足够好。 I want to design the built-in implementation well but I am not very sure how to apply the factory pattern to multiple inheritance cases. 我想很好地设计内置实现,但是我不确定如何将工厂模式应用于多个继承案例。 My current implementation implements each base interface as 我当前的实现将每个基本接口实现为

class BuiltinA : public A
class BuiltinB : public B
class BuiltinC : public C

and then implements common interface as given below: 然后实现如下所示的通用接口:

class BuiltinImplementation : public virtual CommonInterface, public BuiltinA, public BuiltinB, public BuiltinC

Is this a good or bad design? 这是好设计还是坏设计? If it is a bad design, how can I improve it? 如果设计不好,我该如何改进? Also is there any patterns I can apply to this case? 还有什么可以适用于这种情况的模式吗? Any expert opinion is welcome. 欢迎任何专家的意见。 Thanks in advance. 提前致谢。

A CommonInterface of three classes should give you access to methods, that are purely common between all three classes and hide class specific interfaces. 三个类的CommonInterface应该可以让您访问方法,这在所有三个类之间是完全通用的,并且隐藏特定于类的接口。 Your implementation of the CommonInterface gives the caller access to all class specific methods. CommonInterface的实现使调用者可以访问所有类的特定方法。 I would consider this already a bad design. 我认为这已经是一个糟糕的设计。

I would propose to change the inheritance to: 我建议将继承更改为:

class CommonInterface {
   public:
      virtual void common_method(<args>) = 0;
};

class A : public CommonInterface {
   public:
      virtual void a_specific_method(<args>) = 0;
};

class B : public CommonInterface {
   public:
      virtual void b_specific_method(<args>) = 0;
};

And your implementation of builtin is: 而您内置的实现是:

class Builtin_A : public A {
    [...]
};

class Builtin_B : public B {
    [...]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM