Dan*_*rez 5 c# java design-patterns decorator
我正在看这篇维基百科文章,无法理解这是怎么回事.有点沮丧只是通过查看它无法理解代码,我决定将代码移植到c#(我是.net,对不起家伙:)).只需要一些小修改(继承和扩展,基于超级等)并运行应用程序.令我惊讶的是,我得到了以下输出:
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
Run Code Online (Sandbox Code Playgroud)
只是好奇,任何java开发都可以告诉我这里有什么不同以及为什么维基百科示例有效(如果它确实有效,就像它们所说的那样).
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Coffee sampleCoffee = new SimpleCoffee();
Console.WriteLine("Cost: " + sampleCoffee.getCost() + " Ingredient: " + sampleCoffee.getIngredient());
sampleCoffee = new Milk(sampleCoffee);
Console.WriteLine("Cost: " + sampleCoffee.getCost() + " Ingredient: " + sampleCoffee.getIngredient());
sampleCoffee = new Sprinkles(sampleCoffee);
Console.WriteLine("Cost: " + sampleCoffee.getCost() + " Ingredient: " + sampleCoffee.getIngredient());
sampleCoffee = new Whip(sampleCoffee);
Console.WriteLine("Cost: " + sampleCoffee.getCost() + " Ingredient: " + sampleCoffee.getIngredient());
Console.ReadKey();
}
}
//The Coffee Interface defines the functionality of Coffee implemented by decorator
public interface Coffee
{
double getCost(); // returns the cost of coffee
String getIngredient(); //returns the ingredients mixed with coffee
}
//implementation of simple coffee without any extra ingredients
public class SimpleCoffee : Coffee
{
double cost;
String ingredient;
public SimpleCoffee()
{
cost = 1;
ingredient = "Coffee";
}
public double getCost()
{
return cost;
}
public String getIngredient()
{
return ingredient;
}
}
//abstract decorator class - note that it implements coffee interface
abstract public class CoffeeDecorator : Coffee
{
protected Coffee decoratedCoffee;
protected String ingredientSeparator;
public CoffeeDecorator(Coffee decoratedCoffee)
{
this.decoratedCoffee = decoratedCoffee;
ingredientSeparator = ", ";
}
public CoffeeDecorator()
{
}
public double getCost() //note it implements the getCost function defined in interface Coffee
{
return decoratedCoffee.getCost();
}
public String getIngredient()
{
return decoratedCoffee.getIngredient();
}
}
//Decorator Milk that mixes milk with coffee
//note it extends CoffeeDecorator
public class Milk : CoffeeDecorator
{
double cost;
String ingredient;
public Milk(Coffee decoratedCoffee) : base(decoratedCoffee)
{
cost = 0.5;
ingredient = "Milk";
}
public double getCost()
{
return base.getCost() + cost;
}
public String getIngredient()
{
return base.getIngredient() + base.ingredientSeparator + ingredient;
}
}
//Decorator Whip that mixes whip with coffee
//note it extends CoffeeDecorator
public class Whip : CoffeeDecorator
{
double cost;
String ingredient;
public Whip(Coffee decoratedCoffee)
: base(decoratedCoffee)
{
cost = 0.7;
ingredient = "Whip";
}
public double getCost()
{
return base.getCost() + cost;
}
public String getIngredient()
{
return base.getIngredient() + base.ingredientSeparator + ingredient;
}
}
//Decorator Sprinkles that mixes sprinkles with coffee
//note it extends CoffeeDecorator
public class Sprinkles : CoffeeDecorator
{
double cost;
String ingredient;
public Sprinkles(Coffee decoratedCoffee) : base(decoratedCoffee)
{
cost = 0.2;
ingredient = "Sprinkles";
}
public double getCost()
{
return base.getCost() + cost;
}
public String getIngredient()
{
return base.getIngredient() + base.ingredientSeparator + ingredient;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 10
是 - 默认情况下,方法在Java中是虚拟的,但在C#中则不是.
在编译代码时,你应该收到警告,谈论"新"修饰符.那应该给你一个线索.目前你的Milk(等)方法正在隐藏或遮蔽那些CoffeeDecorator- 它们不是多态的.
您需要CoffeeDecorator使用virtual修饰符使方法成为虚拟,然后使用修饰符在Milk(等)中显式覆盖它们override.
// In CoffeeDecorator
public virtual double getCost()
{
return decoratedCoffee.getCost();
}
// In Milk
public override double getCost()
{
return base.getCost() + cost;
}
Run Code Online (Sandbox Code Playgroud)