Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Decorator pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Second example (coffee making scenario) ==== The next Java example illustrates the use of decorators using coffee making scenario. In this example, the scenario only includes cost and ingredients. {{Clear}} <syntaxhighlight lang="java"> // The interface Coffee defines the functionality of Coffee implemented by decorator public interface Coffee { public double getCost(); // Returns the cost of the coffee public String getIngredients(); // Returns the ingredients of the coffee } // Extension of a simple coffee without any extra ingredients public class SimpleCoffee implements Coffee { @Override public double getCost() { return 1; } @Override public String getIngredients() { return "Coffee"; } } </syntaxhighlight> The following classes contain the decorators for all {{mono|Coffee}} classes, including the decorator classes themselves. <syntaxhighlight lang="java"> // Abstract decorator class - note that it implements Coffee interface public abstract class CoffeeDecorator implements Coffee { private final Coffee decoratedCoffee; public CoffeeDecorator(Coffee c) { this.decoratedCoffee = c; } @Override public double getCost() { // Implementing methods of the interface return decoratedCoffee.getCost(); } @Override public String getIngredients() { return decoratedCoffee.getIngredients(); } } // Decorator WithMilk mixes milk into coffee. // Note it extends CoffeeDecorator. class WithMilk extends CoffeeDecorator { public WithMilk(Coffee c) { super(c); } @Override public double getCost() { // Overriding methods defined in the abstract superclass return super.getCost() + 0.5; } @Override public String getIngredients() { return super.getIngredients() + ", Milk"; } } // Decorator WithSprinkles mixes sprinkles onto coffee. // Note it extends CoffeeDecorator. class WithSprinkles extends CoffeeDecorator { public WithSprinkles(Coffee c) { super(c); } @Override public double getCost() { return super.getCost() + 0.2; } @Override public String getIngredients() { return super.getIngredients() + ", Sprinkles"; } } </syntaxhighlight> Here's a test program that creates a {{mono|Coffee}} instance which is fully decorated (with milk and sprinkles), and calculate cost of coffee and prints its ingredients: <syntaxhighlight lang="java"> public class Main { public static void printInfo(Coffee c) { System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients()); } public static void main(String[] args) { Coffee c = new SimpleCoffee(); printInfo(c); c = new WithMilk(c); printInfo(c); c = new WithSprinkles(c); printInfo(c); } } </syntaxhighlight> The output of this program is given below: <pre> Cost: 1.0; Ingredients: Coffee Cost: 1.5; Ingredients: Coffee, Milk Cost: 1.7; Ingredients: Coffee, Milk, Sprinkles </pre>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Decorator pattern
(section)
Add topic