Is it an example of decorator pattern?
Posted
by Supereme
on Stack Overflow
See other posts from Stack Overflow
or by Supereme
Published on 2010-04-07T15:10:26Z
Indexed on
2010/04/07
15:13 UTC
Read the original article
Hit count: 197
decorator
Hi, I've an example please tell me whether it is Decorator pattern or not?
public abstract class ComputerComponent
{
String description ="Unknown Type";
public String getDescription()
{
return description;
}
public abstract double getCost();
}
public abstract class AccessoryDecorator
{
ComputerComponent comp;
public abstract String getDescription();
}
public class PIIIConcreteComp extends ComputerComponent
{
public PIIIConcreteComp()
{
description=“Pentium III”;
}
public double getCost()
{
return 19950.00;
}
public class floppyConcreteDeco extends AccessoryDecorator
{
public floppyConcreteDeco(ComputerComponent comp)
this.comp=comp;
}
public String getDescription()
{
return comp.getDescription() +”, floppy 1.44 mb”;
}
public double getCost()
{
return 250+comp.getCost();
}
}
public class ComponentAssembly
{
public static void createComponent()
{
ComputerComponent comp = new PIIConcreteComp();
// create a PIII computer object
ComputerComponent deco1= new floppyConcreteDeco(comp);
// decorate it with a floppy
//ComputerComponent deco2= newCDRomConcreteDeco(deco1);
ComputerComponent deco2= new floppyConcreteDeco(deco1);
// decorate with a CDRom or with one more floppy
System.out.println( deco2.getdescription() +” “+ deco2.getCost();
}
}
Thank you.
© Stack Overflow or respective owner