package raj;
/**
* @author Rajeev
*/
public class DecoratorTest {
public static void main(String[] arg) {
Computer computer = new Computer();
// put windows 7 wrapper
computer = new Windows7Computer(computer);
System.out.println(computer.getDescription());
System.out.println("Cost "+ computer.getPrice());
/* Basic computer + Winodws 7
Cost 230.75
*/
// put extra memory wrapper
computer = new ExtraMemoryComputer(computer);
System.out.println(computer.getDescription());
System.out.println("Cost "+ computer.getPrice());
/* Basic computer + Winodws 7 + 2G memory
Cost 281.5
*/
// put more extra memory
computer = new ExtraMemoryComputer(computer);
System.out.println(computer.getDescription());
System.out.println("Cost "+ computer.getPrice());
/* Basic computer + Winodws 7 + 2G memory + 2G memory
Cost 332.25
*/
}
}
class Computer {
private String description = "Basic computer";
private double price = 200.50;
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
}
abstract class ComputerDecorator extends Computer {
public abstract String getDescription();
public abstract double getPrice();
}
class Windows7Computer extends ComputerDecorator {
Computer computer;
private double price = 30.25;
private String description = "Winodws 7";
public Windows7Computer(Computer computer) {
this.computer = computer;
}
public String getDescription() {
return computer.getDescription() + " + " + description;
}
public double getPrice() {
return computer.getPrice() + price;
}
}
class ExtraMemoryComputer extends ComputerDecorator {
Computer computer;
private double price = 50.75;
private String description = "2G memory";
public ExtraMemoryComputer(Computer computer) {
this.computer = computer;
}
public String getDescription() {
return computer.getDescription() + " + " + description;
}
public double getPrice() {
return computer.getPrice() + price;
}
}
Now we can see that we can create variants of adding anti-virus, extra CD, network card etc and the wrapping works perfectly. In our design, it would have been better to put the reference to Computer in the ComputerDecorator then the concrete subclasses of Decorator but the net effect is the same. Incidentally the classes in java.io package pertaining to various streams and readers/writers use this pattern extensively. Whenever we need to add-on extra behaviour dynamically then Decorator is the answer.
No comments:
Post a Comment