简体   繁体   English

我们应该在C ++中使用装饰器模式吗?

[英]Should we use decorator pattern in C++?

No doubt that decorator pattern is good and easy standard to enhance some method in class which can't (or shouldn't) inherit the one with old method, like in this example in C# (I am not using interface (I should) to make it as short as possible). 毫无疑问,装饰器模式是增强类中某些不能(或不应该)继承旧方法的方法的好方法,就像在C#中的示例(我不使用接口(我应该))使其尽可能短)。

using System;

class Human { ... }

class Father {
    public string Info() {
        return "John";
    }
}

class Son : Human { // decorating Father
    Father f = new Father();
    public string Info() {
        return "I am son of "+f.Info();
    }
}


class Program {
    public static void Main() {
        Son s = new Son();
        Console.WriteLine(s.Info()); // I am son of John
        Console.ReadKey();
    }
}

But since we have multiple inheritance in C++, we can hide Fathers' Info() instead of decorate it: 但是,由于我们在C ++中具有多个继承,因此我们可以隐藏父亲的Info()而不是修饰它:

#include <string>
#include <iostream>
using namespace std;

class Human { ... };

class Father {
public:
    string Info() {
        return "John";
    }
};

class Son : public Human, Father { // inherit both
public:
    string Info() {
        return "I am son of " + Father::Info();
    }
};

int main() {
    Son* s = new Son();
    cout << s->Info(); // I am son of John
    cin.get();
    return 0;
}

I understand that the reason for many patterns is to move the ad-hoc logic from reusable class to ad-hoc class, so the reusable class does not need to be (or can't be) messed with ad-hoc code. 我知道许多模式的原因是将即席逻辑从可重用类移到了特设类,因此可重用类不需要(也不能被)即席代码所困扰。 But this can be achieved with multiple inheritance, too. 但这也可以通过多重继承来实现。

So can you explain (or give an example) where decoration is better idea than multiple inheritance? 那么,您能解释一下(或举个例子)修饰比多重继承更好的主意吗?

With the decorator pattern you can decorate objects at run-time, while multiple inheritance is a compile-time solution. 使用装饰器模式,您可以在运行时装饰对象,而多重继承是一种编译时解决方案。 This allows you to freely combine different decorators with minimal overhead, whereas with multiple inheritance, you need a new class for each combination of decorators. 这使您可以以最小的开销自由组合不同的装饰器,而具有多重继承,则需要为每种装饰器组合使用新的类。

Also, multiple inheritance is hard to get right if you are inheriting anything but interface-classes (classes that only have pure virtual methods). 另外,如果您要继承接口类(仅具有纯虚方法的类)以外的任何东西,则很难实现多重继承。

In my opinion, this is not the decorator pattern and you seem to be using inheritance the wrong way. 在我看来,这不是装饰器模式,您似乎在以错误的方式使用继承。 Before inheriting, ask yourself, if the statement holds true: "Son is a Father" or SpecificObject is a GeneralizedObject, which is not what you ment to express. 在继承之前,请问问自己,该语句是否成立:“儿子是父亲”还是SpecificObject是GeneralizedObject,这不是您要表达的内容。

The decorator pattern works by passing the object to be decorated to the object that decorates it. 装饰器模式通过将要装饰的对象传递给装饰它的对象来工作。 A typcial example is the BorderClass in Gui's that decorates the widget you pass it with a border. 一个典型的例子是Gui's中的BorderClass,它装饰您通过边框的小部件。

 public class Border : Widget
   { 
       public Border(Widget decorateMe)
       { 
          this._toDecorate = decorateMe;
       }
       public virtual void draw(gc or whatnot){
          gc.drawRectangle(blah);
          this._toDecorate.draw(gc);
       }
   }

hope this helps 希望这可以帮助

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

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