我正在学习C#中的装饰器类模式。我注意到装饰器类似乎是重复的,重复了原始类的所有方法。有什么办法可以避免这种情况,或者是标准做法?无论如何要在Visual Studio 2017 IDE中自动生成Decorator代码?
下面的示例如何更好地写在下面?
简单的例子: https://www.dotnettricks.com/learn/designpatterns/decorator-design-pattern-dotnet
原始课程:
public interface Vehicle
{
string Make { get; }
string Model { get; }
double Price { get; }
}
public class HondaCity : Vehicle
{
public string Make
{
get { return "HondaCity"; }
}
public string Model
{
get { return "CNG"; }
}
public double Price
{
get { return 1000000; }
}
}
装饰器: 似乎是一种重复的编码方式
public abstract class VehicleDecorator : Vehicle
{
private Vehicle _vehicle;
public VehicleDecorator(Vehicle vehicle)
{
_vehicle = vehicle;
}
public string Make
{
get { return _vehicle.Make; }
}
public string Model
{
get { return _vehicle.Model; }
}
public double Price
{
get { return _vehicle.Price; }
}
}