मैं this विकिपीडिया लेख देख रहा था, और नहीं समझ सकता है कि कैसे नरक कि काम कर रहा था। बस इसे देखकर कोड को समझने में सक्षम नहीं होने के कारण, मैं कोड को सी # (मैं हूं .नेट, माफ करना दोस्तों :)) को पोर्ट करने के लिए समर्पित हूं। बस कुछ मामूली संशोधन की आवश्यकता थी (विरासत और विस्तार, सुपर के लिए आधार, आदि) और ऐप चलाएं। मेरे आश्चर्य करने के लिए, मैं निम्नलिखित उत्पादन मिल गया:डेकोरेटर पैटर्न प्रश्न सी #/जावा
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
Cost: 1 Ingredient: Coffee
बस उत्सुक, किसी भी जावा देव मुझे बता सकते हैं कि यहाँ क्या अलग है और यही कारण है विकिपीडिया उदाहरण काम करता है (अगर यह निश्चित रूप से काम करता है के रूप में वे कहते हैं कि यह होता है,)।
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;
}
}
}
मुझे सी # पता नहीं है। लेकिन ऐसा लगता है कि आप अपनी विधियों को ओवरराइड नहीं करते हैं। –
यहाँ http://msdn.microsoft.com/en-us/library/ms173152%28VS.80%29.aspx –