装饰器添加了功能,因此您不必将所有内容都委托给原始 mario。
是的,您可以将克隆委托给原始 mario,但您的装饰器随后将使用自己更大的胡子更新 mustache 属性,然后返回更新后的克隆;
更新解释克隆:
装饰器模式的重点是隐藏被包装对象的新功能,因此被包装对象不会克隆被装饰的属性。您调用顶级装饰器的 clone 方法。装饰器会覆盖 Clone 方法,以透明的方式添加自己的功能。
public class Mario : ICloneable
{
public Mario()
{
MoustacheSize = 1;
}
private double _moustacheSize;
public virtual double MoustacheSize
{
get { return _moustacheSize; }
internal set { _moustacheSize = value; }
}
public virtual object Clone()
{
var clone = new Mario();
clone.MoustacheSize = this.MoustacheSize;
return clone;
}
}
public class HeroUpgradeDecorator : Mario
{
public HeroUpgradeDecorator(Mario mario)
{
_inner = mario;
}
private Mario _inner;
public override double MoustacheSize
{
get
{
return _inner.MoustacheSize * 1.2; // 20% increase in moustache size
}
}
public override object Clone()
{
var clone = new Mario();
clone.MoustacheSize = this.MoustacheSize;
return clone;
}
}
static void Main(string[] args)
{
var mario = new Mario();
Console.WriteLine("Mario, with moustache size: {0}", mario.MoustacheSize);
Console.WriteLine("Upgrading...");
mario = new HeroUpgradeDecorator(mario); // variable mario now points to the decorator
Console.WriteLine("Mario, with moustache size: {0}", mario.MoustacheSize);
Console.WriteLine("Upgrading again...");
mario = new HeroUpgradeDecorator(mario); // variable mario now points to the 2nd decorator
Console.WriteLine("Mario, with moustache size: {0}", mario.MoustacheSize);
Console.ReadLine();
}
此控制台应用程序的输出是:
Mario, with moustache size: 1
Upgrading...
Mario, with moustache size: 1.2
Upgrading again...
Mario, with moustache size: 1.44
如果有很多属性要克隆,装饰器也可以这样做:
public override object Clone()
{
var clone = (Mario)_inner.Clone();
clone.MoustacheSize = this.MoustacheSize;
return clone;
}
装饰器使用原始对象的克隆方法,然后更新它自己改变的属性。但它仍然是负责最终结果的装饰器。