0% found this document useful (0 votes)
72 views3 pages

UML Example Source 1 1 1

1) The document defines an interface and classes for a component-based menu ordering system. 2) It includes classes for menu items like burgers, cheese, toppings, sauces that can be decorated and customized. 3) A BuildOrder class constructs a sample order by adding customized menu item components to a composite order object.

Uploaded by

Mahesh Palagani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views3 pages

UML Example Source 1 1 1

1) The document defines an interface and classes for a component-based menu ordering system. 2) It includes classes for menu items like burgers, cheese, toppings, sauces that can be decorated and customized. 3) A BuildOrder class constructs a sample order by adding customized menu item components to a composite order object.

Uploaded by

Mahesh Palagani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SOURCE

CODE public interface Component {

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 ;

public class BuildOrder { public Leaf ( String d, Double p )


{
public static Component getOrder() description = d ;
{ price = p ;
Component order = new Composite( "Order" ) ; }
order.addChild(
new Leaf("Crispy Onion Strings", 5.50 )); public Leaf ( String d )
order.addChild( {
new Leaf("The Purist", 8.00 )); description = d ;
CustomBurger customBurger = new price = 0.0 ;
CustomBurger( "Build Your Own Burger" ) ; }

// base price for 1/3 lb public void printDescription() {


Burger b = new Burger( "Burger Options" ) ; DecimalFormat fmt =
String[] bo = new DecimalFormat("0.00");
{ "Beef", "1/3lb.", "On A Bun" } ; System.out.println(
b.setOptions( bo ) ; description + " " + fmt.format(price) ) ;
}
// 1 cheese free, extra cheese +1.00
Cheese c = new Cheese( "Cheese Options" ) ; public void addChild(Component c) {
String[] co = // no implementation
{ "Danish Blue Cheese", }
"Horseradish Cheddar" } ;
c.setOptions( co ) ; public void removeChild(Component c) {
c.wrapDecorator( b ) ; // no implementation
}
// 4 toppings free, extra +.75
Toppings t = new Toppings( public Component getChild(int i) {
"Toppings Options" ) ; // no implementation
String[] to = return null ;
{ "Bermuda Red Onion", }
"Black Olives", "Carrot Strings", }
"Coleslaw" } ;
t.setOptions( to ) ; public class Composite implements Component {
t.wrapDecorator( c ) ;
protected ArrayList<Component>
// premium topping +1.50 components = new ArrayList<Component>() ;
Premium p = protected String description ;
new Premium( "Premium Options" ) ;
String[] po = { "Applewood Smoked Bacon" } ; public Composite ( String d )
p.setOptions( po ) ; {
p.wrapDecorator( t ) ; description = d ;
}
// 1 sauce free, extra +.75
Sauce s = new Sauce( "Sauce Options" ) ; public void printDescription() {
String[] so = { "Appricot Sauce" } ; System.out.println( description );
s.setOptions( so ) ; for (Component obj : components)
s.wrapDecorator( p ) ; {
obj.printDescription();
// Setup Custom Burger Ingredients }
customBurger.setDecorator( s ) ; }
customBurger.addChild( b ) ;
customBurger.addChild( c ) ; public void addChild(Component c) {
customBurger.addChild( t ) ; components.add( c ) ;
customBurger.addChild( p ) ; }
customBurger.addChild( s ) ;
public void removeChild(Component c) {
// Add Custom Burger to the ORder components.remove(c) ;
order.addChild( customBurger ); }
return order ;
} public Component getChild(int i) {
return components.get( i ) ;
} }


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 ;

public Toppings( String d ) public Sauce( String d )


{ {
super(d) ; super(d) ;
} }

// 4 toppings free, extra +.75 // 1 sauce free, extra +.75


public void setOptions( String[] options ) public void setOptions( String[] options )
{ {
this.options = options ; this.options = options ;
if ( options.length > 4 ) if ( options.length > 1 )
this.price += ( this.price += (options.length-1) * 0.75 ;
options.length-4) * 0.75 ; }
}
public String getDescription()
public String getDescription() {
{ String desc = "" ;
String desc = "" ; for ( int i = 0; i<options.length; i++ )
for ( int i = 0; i<options.length; i++ ) {
{ if (i>0) desc += " + " + options[i] ;
if (i>0) desc += " + " + options[i] ; else desc = options[i] ;
else desc = options[i] ; }
} return desc ;
return desc ; }
}
}
}

public class Premium extends LeafDecorator


{
private String[] options ;

public Premium( String d )


{
super(d) ;
}

// premium topping +1.50


public void setOptions( String[] options )
{
this.options = options ;
if ( options.length > 0 )
this.price += options.length * 1.50 ;
}

public String getDescription()


{
String desc = "" ;
for ( int i = 0; i<options.length; i++ )
{
if (i>0) desc += " + " + options[i] ;
else desc = options[i] ;
}
return desc ;
}


Page 5 of 11

You might also like