Java Tutorial/Design Pattern/Decorator Pattern
Coffee example using decorators
<source lang="java">
interface Countable {
String getName(); float getCount();
} class Book implements Countable {
public String getName() { return "Book"; } public float getCount() { return 0; }
} abstract class Number implements Countable {
protected Countable component; Number(Countable component) { this.ruponent = component; } public float getCount() { return component.getCount(); } public abstract String getName();
} class Integer extends Number {
private float value = 0.75f; private String description = " integer"; public Integer(Countable component) { super(component); } public float getCount() { return component.getCount() + value; } public String getName() { return component.getName() + description; }
} class Float extends Number {
private String description = " float"; public Float(Countable component) { super(component); } public String getName() { return component.getName() + description; }
} class Double extends Number {
private float value = 0.25f; private String description = " double"; public Double(Countable component) { super(component); } public float getCount() { return component.getCount() + value; } public String getName() { return component.getName() + description; }
} class Decimal extends Number {
private float value = 0.25f; private String description = " decimal"; public Decimal(Countable component) { super(component); } public float getCount() { return component.getCount() + value; } public String getName() { return component.getName() + description; }
} class Positive extends Number {
private float cost = 0.25f; private String description = " positive"; public Positive(Countable component) { super(component); } public float getCount() { return component.getCount() + cost; } public String getName() { return component.getName() + description; }
} class Negative extends Number {
private float value = -0.25f; private String description = " negative"; public Negative(Countable component) { super(component); } public float getCount() { return component.getCount() + value; } public String getName() { return component.getName() + description; }
} public class NumberCountableDemo {
public static void main(String[] args) { Countable cappuccino = new Integer(new Double(new Book())); System.out.println(cappuccino.getName().trim() + ": $" + cappuccino.getCount()); Countable cafeMocha = new Integer(new Decimal(new Negative(new Positive(new Float( new Book()))))); System.out.println(cafeMocha.getName().trim() + ": $" + cafeMocha.getCount()); }
}</source>
combinations and decorators
<source lang="java">
interface Countable {
float getValue(); String getName();
} class Book implements Countable {
private String description = "books"; private float value = 0.75f; public float getValue() { return value; } public String getName() { return description; }
} class Dog implements Countable {
private String description = "dogs"; private float cost = 1; public float getValue() { return cost; } public String getName() { return description; }
} class Cat implements Countable {
private float cost = 1; private String description = "Cat"; public float getValue() { return cost; } public String getName() { return description; }
} class Child implements Countable {
private float cost = 1; private String description = "child"; public float getValue() { return cost; } public String getName() { return description; }
} class Cow implements Countable {
private float cost = 1.25f; private String description = "Cow"; public float getValue() { return cost; } public String getName() { return description; }
} abstract class Number implements Countable {
protected Countable component; public Number(Countable component) { this.ruponent = component; } public float getValue() { return component.getValue(); } public String getName() { return component.getName(); }
} class MyBook extends Number {
private float cost = 0.75f; public MyBook(Countable component) { super(component); } public String getName() { return component.getName() + " my books"; } public float getValue() { return cost + component.getValue(); }
} class Integer extends Number {
private float cost = 0.50f; public Integer(Countable component) { super(component); } public float getValue() { return cost + component.getValue(); } public String getName() { return component.getName() + " integer"; }
} class Float extends Number {
public Float(Countable component) { super(component); } public String getName() { return component.getName() + " float"; }
} class Double extends Number {
public Double(Countable component) { super(component); } public String getName() { return component.getName() + " double"; }
} class Decimal extends Number {
public Decimal(Countable component) { super(component); } public String getName() { return component.getName() + " decimal"; }
} public class CountableDemo {
public static void main(String[] args) { Countable cappuccino = new Cat(); System.out.println(cappuccino.getName() + ": $" + cappuccino.getValue()); Countable cafeMocha = new Integer(new Float(new Cow())); System.out.println(cafeMocha.getName() + ": $" + cafeMocha.getValue()); }
}</source>
Creating and Extending Objects with the Decorator Patterns
<source lang="java">
public class MainClass {
public static void main(String args[]) { Computer computer = new Computer(); computer = new Disk(computer); computer = new Monitor(computer); computer = new CD(computer); computer = new CD(computer); System.out.println("You"re getting a " + computer.description() + "."); }
} class Disk extends ComponentDecorator {
Computer computer; public Disk(Computer c) { computer = c; } public String description() { return computer.description() + " and a disk"; }
} class CD extends ComponentDecorator {
Computer computer; public CD(Computer c) { computer = c; } public String description() { return computer.description() + " and a CD"; }
} class Computer {
public Computer() { } public String description() { return "computer"; }
} abstract class ComponentDecorator extends Computer {
public abstract String description();
} class Monitor extends ComponentDecorator {
Computer computer; public Monitor(Computer c) { computer = c; } public String description() { return computer.description() + " and a monitor"; }
}</source>