Decorator Design Pattern

Decorator Design Pattern

The Decorator Design Pattern is a structural pattern that allows behavior to be added to individual objects, dynamically, without affecting the behavior of other objects from the same class. This is particularly useful for adding responsibilities to objects without modifying their code.


Ice Cream Example with Decorator Design Pattern

Imagine an ice cream shop where customers can start with a basic ice cream cone and then add scoops of different flavors like chocolate, mango, or vanilla. Each scoop adds a new flavor and cost to the basic ice cream. The Decorator Pattern is perfect for this scenario because it allows us to "decorate" the basic ice cream with additional flavors in a flexible manner.

Key Components of the Decorator Pattern:

  1. Component (IceCream): An interface or abstract class that defines the base behavior for all types of ice cream.
  2. Concrete Component (BasicIceCream): The basic implementation of the component (e.g., plain ice cream).
  3. Decorator (IceCreamDecorator): An abstract class that implements the component interface and has a reference to a component object.
  4. Concrete Decorators (ChocolateScoop, MangoScoop, VanillaScoop): These classes extend the decorator class and add additional behavior (e.g., flavor and cost).


Example:

Let's break it down in code:

Step 1: Define the IceCream interface

// The Component interface

public interface IceCream {

String getDescription();

double getCost();

}

Step 2: Create the BasicIceCream Concrete Component

// The Concrete Component

public class BasicIceCream implements IceCream {

@Override

public String getDescription() {

return "Basic Ice Cream";

}

@Override

public double getCost() {

return 1.00; // Base cost of the ice cream

}

}

Step 3: Create the IceCreamDecorator Abstract Decorator

// The Decorator class

public abstract class IceCreamDecorator implements IceCream {

protected IceCream iceCream;

public IceCreamDecorator(IceCream iceCream) {

this.iceCream = iceCream;

}

@Override

public String getDescription() {

return iceCream.getDescription();

}

@Override

public double getCost() {

return iceCream.getCost();

}

}

Step 4: Create Concrete Decorators for Different Scoops

// Chocolate Scoop Decorator

public class ChocolateScoop extends IceCreamDecorator {

public ChocolateScoop(IceCream iceCream) {

super(iceCream);

}

@Override

public String getDescription() {

return iceCream.getDescription() + ", Chocolate Scoop";

}

@Override

public double getCost() {

return iceCream.getCost() + 0.75; // Cost of a chocolate scoop

}

}

// Mango Scoop Decorator

public class MangoScoop extends IceCreamDecorator {

public MangoScoop(IceCream iceCream) {

super(iceCream);

}

@Override

public String getDescription() {

return iceCream.getDescription() + ", Mango Scoop";

}

@Override

public double getCost() {

return iceCream.getCost() + 0.80; // Cost of a mango scoop

}

}

// Vanilla Scoop Decorator

public class VanillaScoop extends IceCreamDecorator {

public VanillaScoop(IceCream iceCream) {

super(iceCream);

}

@Override

public String getDescription() {

return iceCream.getDescription() + ", Vanilla Scoop";

}

@Override

public double getCost() {

return iceCream.getCost() + 0.70; // Cost of a vanilla scoop

}

}

Step 5: Demonstrate the Ice Cream Decoration

public class Main {

public static void main(String[] args) {

// Start with a basic ice cream

IceCream iceCream = new BasicIceCream();

// Add a chocolate scoop

iceCream = new ChocolateScoop(iceCream);

// Add a mango scoop

iceCream = new MangoScoop(iceCream);

// Add a vanilla scoop

iceCream = new VanillaScoop(iceCream);

// Output the final ice cream description and cost

System.out.println("Description: " + iceCream.getDescription());

System.out.println("Cost: $" + iceCream.getCost());

}

}

Output:

Description: Basic Ice Cream, Chocolate Scoop, Mango Scoop, Vanilla Scoop

Cost: $3.25

Explanation:

  1. IceCream Interface: Defines the base structure of all ice cream objects.
  2. BasicIceCream: The base implementation, which starts as a plain ice cream.
  3. IceCreamDecorator: An abstract decorator that holds a reference to an IceCream object and delegates the base behavior to it.
  4. ChocolateScoop, MangoScoop, VanillaScoop: Concrete decorators that extend IceCreamDecorator and add their own behavior (e.g., flavor and cost) to the base ice cream.

Advantages:

  • Flexibility: New functionality (flavors) can be added without modifying the existing code.
  • Reusability: Decorators can be reused across different ice cream objects or in different combinations.
  • Single Responsibility: Each decorator class is responsible for adding one specific behavior, making the code easier to manage.

Conclusion:

The Decorator Design Pattern provides a powerful way to extend the functionality of objects in a flexible and reusable manner. In our ice cream example, we can dynamically add different scoops to a basic ice cream, building complex combinations without altering the core class structure. This pattern is especially useful when you need to add responsibilities to objects without affecting others.


#SoftwareDesign #DecoratorDesign #Pattern #Architecture #DesignPatterns #Developers #ProgrammingTips


要查看或添加评论,请登录

Saji Kuttan的更多文章

  • The SOLID Principles

    The SOLID Principles

    The SOLID Principles are a set of five design principles in object-oriented programming that help developers create…

  • Facade Design Pattern

    Facade Design Pattern

    The Facade Design Pattern is a structural design pattern that provides a simplified interface to a complex subsystem of…

  • Observer Design Pattern

    Observer Design Pattern

    The Observer Design Pattern is a behavioral pattern where an object (called the subject) maintains a list of its…

  • Factory Design Pattern.

    Factory Design Pattern.

    The Factory Design Pattern is a creational pattern used to create objects without specifying the exact class of object…

  • State Design Pattern

    State Design Pattern

    The State Design Pattern is a behavioral design pattern that allows an object to change its behavior when its internal…

社区洞察

其他会员也浏览了