UML Example Source 1 1 1
UML Example Source 1 1 1
void printDescription() ;
public class Client { void addChild(Component c);
void removeChild(Component c);
public static void runTest() Component getChild(int i);
{
Component theOrder = BuildOrder.getOrder() ; }
theOrder.printDescription();
public class Leaf implements Component {
}
} private String description ;
protected Double price ;
Page 3 of 11
} public class Burger extends LeafDecorator
{
private String[] options ;
public interface PriceDecorator
{
Double getPrice(); public Burger( String d )
} {
super(d) ;
}
public class CustomBurger extends Composite
{ public void setOptions( String[] options )
PriceDecorator decorator = null ; {
this.options = options ;
public CustomBurger ( String d ) for ( int i = 0; i<options.length; i++ )
{ {
super(d) ; if ( "1/3lb.".equals(options[i]) )
} this.price += 9.50 ;
if ( "2/3lb.".equals(options[i]) )
public void setDecorator( PriceDecorator p ) this.price += 11.50 ;
{ if ( "1lb.".equals(options[i]) )
this.decorator = p ; this.price += 15.50 ;
} if (
"In A Bowl".equals(options[i]) )
public void printDescription() { this.price += 1.50 ;
DecimalFormat fmt = new DecimalFormat("0.00"); }
System.out.println( description + " }
"=fmt.format(decorator.getPrice()) );
public String getDescription()
for (Component obj : components) {
{ String desc = "" ;
obj.printDescription(); for ( int i = 0; i<options.length; i++ )
} {
} if (i>0) desc += " + " + options[i] ;
} else desc = options[i] ;
}
return desc ;
public abstract class LeafDecorator extends Leaf }
implements PriceDecorator
{ }
PriceDecorator wrapped ;
public class Cheese extends LeafDecorator
public LeafDecorator( String d ) { {
super( d ) ; private String[] options ;
this.wrapped = null ;
}
public Cheese( String d )
public void wrapDecorator( PriceDecorator w ) {
{ super(d) ;
this.wrapped = w ; }
}
// 1 cheese free, extra cheese +1.00
public Double getPrice() { public void setOptions( String[] options )
if (wrapped == null ) {
{ this.options = options ;
return price ; if ( options.length > 1 )
} this.price +=
else (options.length-1) * 1.00 ;
{ }
return price + wrapped.getPrice() ;
} public String getDescription()
} {
String desc = "" ;
abstract public void setOptions( for ( int i = 0; i<options.length; i++ )
String[] options ) ; {
abstract public String getDescription() ; if (i>0) desc += " + " + options[i] ;
else desc = options[i] ;
@Override }
public void printDescription() { return desc ;
System.out.println( getDescription() ) ; }
}
}
Page 4 of 11
public class Toppings extends LeafDecorator public class Sauce extends LeafDecorator
{ {
private String[] options ; private String[] options ;
Page 5 of 11