Decorator
pattern allows to add new functionality in an existing object without altering
its structure.
Example:
Suppose
in a sandwich shop, first customer order a sandwich with additional cheese and
corn and chicken.
Now,
the second customer needs a sandwich with cheese and third customer needs
sandwich with chicken only.
Say
price list is as follows,
Sandwich è RS. 30/-
Additional Chicken è RS. 20/-
Additional Cheese è RS. 10/-
Additional Corn è RS. 7/-
Now,
for this we can use decorator pattern
to make the billing software for sandwich shop.
package com.exmpl.decorator;
public interface Sandwich {
public int getPrice();
}
============================================================
============================================================
package com.exmpl.decorator;
public class PlaneSandwich implements Sandwich {
@Override
public int getPrice() {
return 30;
}
}
============================================================
============================================================
package com.exmpl.decorator;
/* Make this decorator class as abstract, because no need to define/impleement interface's (Sandwich) "getPrice()" method under this decorator class. It will be overridden under each concrete classes (eg. CheesSandwich, ChickenSandwich etc). */
abstract public class SandwichDecorator implements Sandwich {
protected Sandwich specialSandwich;
public SandwichDecorator(Sandwich specialSandwich){
this.specialSandwich=specialSandwich;
}
/*public int getPrice(){
return specialSandwich.getPrice();
}*/
}
package com.exmpl.decorator;
public class CheesSandwich extends SandwichDecorator{
public
CheesSandwich(Sandwich specialSandwich) {
super(specialSandwich);
}
public int getPrice(){
return specialSandwich.getPrice() +
addCheesPrice();
}
private int addCheesPrice() {
return 10;
}
}
============================================================
============================================================
package com.exmpl.decorator;
public class ChickenSandwich extends SandwichDecorator{
public
ChickenSandwich(Sandwich specialSandwich) {
super(specialSandwich);
}
public int getPrice(){
return specialSandwich.getPrice() +
addChickenPrice();
}
private int addChickenPrice() {
return 20;
}
}
============================================================
============================================================
package com.exmpl.decorator;
public class CornSandwich extends SandwichDecorator{
public
CornSandwich(Sandwich specialSandwich) {
super(specialSandwich);
}
public int getPrice(){
return specialSandwich.getPrice() +
addCornPrice();
}
private int addCornPrice() {
return 7;
}
}
============================================================
package com.exmpl.decorator;
public class OrderPriceCheck {
public static void main(String[] args)
{
// Plane sandwich
............
Sandwich
planeSandwich = new PlaneSandwich();
System.out.println("Plane
sandwich price ==>"
+
planeSandwich.getPrice());
// Sandwich with
additional cheese and corn and chicken.
Sandwich
planeSandwichWithCheesCornChicken = new CheesSandwich(
new CornSandwich(new ChickenSandwich(new PlaneSandwich())));
System.out
.println("Sandwich with
additional cheese and corn and chicken ==>"
+
planeSandwichWithCheesCornChicken.getPrice());
// Ssandwich
with cheese.
Sandwich
planeSandwichWithChees = new CheesSandwich(new PlaneSandwich());
System.out.println("Sandwich with
additional cheese ==>"
+
planeSandwichWithChees.getPrice());
// Ssandwich
with corn.
Sandwich
planeSandwichWithCorn = new CornSandwich(new PlaneSandwich());
System.out.println("Sandwich with
additional cheese ==>"
+
planeSandwichWithCorn.getPrice());
}
}
============================================================
============================================================
Output è
Plane sandwich price ==>30
Sandwich with additional cheese and
corn and chicken ==>67
Sandwich with additional cheese
==>40
Sandwich with
additional cheese ==>37
No comments:
Post a Comment