combinations and decorators : Decorator Pattern « Design Pattern « Java Tutorial






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.component = 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());

  }

}








34.10.Decorator Pattern
34.10.1.Coffee example using decorators
34.10.2.combinations and decorators
34.10.3.Creating and Extending Objects with the Decorator Patterns