我需要向对象添加额外的数据——但我不需要添加功能。因此,以下是经典 GOF 装饰器模式的示例吗?
class ClassIWantToDecorate
{
public string GetData() { return "data"; }
}
class Decorator
{
private ClassIWantToDecorate _classIWantToDecInstance = null;
private string _additionalData;
public Decorator(ClassIWantToDecorate classIWantToDec, string additionalData)
{
_classIWantToDecInstance = classIWantToDec;
_additionalData = additionalData;
}
public string DataINeed()
{
return _classIWantToDecInstance.GetData();
}
public string AdditionalDataINeed()
{
return _additionalData;
}
}