【问题标题】:Practical drawbacks of primitive Decorators原始装饰器的实际缺点
【发布时间】:2016-02-16 19:21:05
【问题描述】:

Decorator 模式的启发,但确信该模式的要点可以以较低的复杂性实现,我制作了两个快速的 sn-ps,分别用 C++11 和 Java 编写。

装饰器.cpp:

#include <iostream>
#include <string>

using namespace std;

class Box {

public:
    virtual string getDescription()
    {
        return "A box";
    }
};

class BoxDecorator : public Box {
    Box* wrapee;

public:
    BoxDecorator(Box* box)
    {
        wrapee = box;
    }
    string getDescription()
    {
        return wrapee->getDescription();
    }
};

class RedBox : public BoxDecorator {

public:
    RedBox(Box* box)
        : BoxDecorator(box)
    {
    }

    string getDescription()
    {
        return BoxDecorator::getDescription() + ", red-colored";
    }
};

class BigBox : public BoxDecorator {

public:
    BigBox(Box* box)
        : BoxDecorator(box)
    {
    }

    string getDescription()
    {
        return BoxDecorator::getDescription() + ", big-sized";
    }
};

class StripedBox : public BoxDecorator {

public:
    StripedBox(Box* box)
        : BoxDecorator(box)
    {
    }

    string getDescription()
    {
        return BoxDecorator::getDescription() + ", with several stripes around it";
    }
};

int main()
{
    Box* sampleBox = new StripedBox(new RedBox(new BigBox(new Box())));
    cout << sampleBox->getDescription() << endl;
}

装饰器.java:

class Box {

 public Box() {

 }

 public String getDescription() {
  return "A box";
 }

}

class BoxDecorator extends Box {

 Box boxToBeDecorated;

 public BoxDecorator(Box box) {
  boxToBeDecorated = box;
 }

 public String getDescription() {
  return boxToBeDecorated.getDescription();
 }


}

class RedBox extends BoxDecorator {

 public RedBox(Box box) {
  super(box);
 }

 public String getDescription() {
  return super.getDescription() + ", red-colored";
 }
}

class BigBox extends BoxDecorator {

 public BigBox(Box box) {
  super(box);
 }

 public String getDescription() {
  return super.getDescription() + ", big-sized";
 }
}

class StripedBox extends BoxDecorator {

 public StripedBox(Box box) {
  super(box);
 }

 public String getDescription() {
  return super.getDescription() + ", with several stripes around it";
 }
}

public class Decorator {

 public static void main(String[] args) {

  Box sampleBox = new StripedBox(new RedBox(new BigBox(new Box())));
  System.out.println(sampleBox.getDescription());

 }
}

两者都在生成有效的“一个盒子,大号,红色,周围有几条条纹”输出。因此,以 Java 为例,并不是语言的复杂性迫使我们to use interfaces or abstract classes

那么,这些被剥离的“装饰器”有哪些实际缺点?

【问题讨论】:

    标签: java c++ decorator


    【解决方案1】:

    在这里你继承了 Box 的实现。这需要内存,可能会导致问题(例如,构造函数可能有副作用)。没有继承Box的理由。此外,如果使用继承,装饰器链的可能组合数量会呈指数增长。

    【讨论】:

    • 有点好奇从 Box 继承 Decorator 对内存的影响。我想象一个 Box 非常占用内存的情况,然后 Decorator 实例在它们内部得到了所有的膨胀,这对性能来说是可怕的。这或多或少接近你的观点?
    • 是的,你是对的。超类的所有成员字段即使明显没有被使用也会占用内存。
    猜你喜欢
    • 2015-08-23
    • 2017-07-17
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2015-08-18
    • 2018-12-11
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多