Design Patterns — Zero to Hero — Decorator Pattern

public abstract class Item {    String description = "Unknown Description";
// getDescription is implemented
public String getDescription(){
return description;
}
// need to implement Cost in the sub-classes who extends Item Class public abstract double cost();}
public abstract class ItemDecorator extends Item {
public abstract String getDescription();
}
public class VegetarianFood extends Item {     public VegetarianFood(){
description = "Basic Vegetarian Food";
// description is a variable inherited from Item
}
public double cost()
return 100;
}
}
public class NonVegetarianFood extends Item {public NonVegetarianFood(){
description = "Basic Non-Vegetarian Food with curries";
// description is a variable inherited from Item
}
public double cost()
return 150;
}
}
public class IndianVegFood extends ItemDecorator{      Item item;
// instance variable to hold the item we are going to wrap
public IndianVegFood(Item item){
this.item = item;
// passing the wrapping item
}

publich String getDescription(){
return beverage.getDescription() + ", Indian Veg Curries";
}

public double cost(){
return item.cos() + 50;
}
}// Implementation For ChineseVegFoodpublic class ChineseVegFood extends ItemDecorator{ Item item;
// instance variable to hold the item we are going to wrap
public IndianVegFood(Item item){
this.item = item;
// passing the wrapping item
}

publich String getDescription(){
return item.getDescription() + ", Chinese Veg Curries";
}

public double cost(){
return item.cos() + 70;
}
}
public class MainApp{    public static void main (String args[]){          Item item = new VegetarianFood();
System.out.println(item.getDescription + " $"+ item.cost());
Item item1 = new NonVegetarianFood();
item2 = new IndianVegFood(item1);
item2 = new IndianVegFood(item1);
item2 = new ChineseVegFood(item1);
System.out.println(item1.getDescription + " $"+ item1.cost());
}}
Basic Vegetarian Food $100
Basic Non-Vegetarian Food with curries, Indian Veg Curries, Indian Veg Curries, Chinese Veg Curries $320
  • Decorator Pattern is used in Java IO classes: FileInputStream acts as abstract decorator pattern.
  • Decorators have the same super type as the objects they decorate
  • Can use one or more decorators to wrap an object
  • decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job
  • Objects can be decorated at any time, so we can decorate objects dynamically at runtime with as many decorators as we like
  • The Decorator Pattern provides an alternative alternative to sub classing for extending behavior.
  • The Decorator Pattern involves a set of decorator classes that are used to wrap concrete components

For Note:

  • If a lot of small classes are created as a decorators for objects it would become more complex for a one to read or implement the other part of the code (introducing decorators can increase the complexity of the code)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store