Mastering Java Design Patterns - Day 9: Decorator Pattern
Emmanuel Hadjistratis (he/him)
No more security gaps or inefficient APIs | I optimize your backend infrastructure for maximum performance
Hello, everyone! Today, we continue our exploration of Structural Design Patterns with the Decorator Pattern. Yesterday, we covered the Composite Pattern. Now, let’s see how the Decorator Pattern can help us extend the functionality of objects dynamically and flexibly.
What is the Decorator Pattern?
The Decorator Pattern allows you to add behavior to objects dynamically by placing them inside wrapper objects that provide the additional functionality. This pattern is particularly useful when you want to add responsibilities to individual objects without affecting other objects of the same class.
Why Use the Decorator Pattern?
How to Implement the Decorator Pattern in Java
Here's a simple implementation of the Decorator Pattern:
// Component interface
interface Coffee {
String getDescription();
double getCost();
}
领英推荐
// Concrete Component
class SimpleCoffee implements Coffee {
public String getDescription() {
return "Simple coffee";
}
public double getCost() {
return 2.0;
}
}
// Decorator
abstract class CoffeeDecorator implements Coffee {
protected Coffee decoratedCoffee;
public CoffeeDecorator(Coffee coffee) {
this.decoratedCoffee = coffee;
}
public String getDescription() {
return decoratedCoffee.getDescription();
}
public double getCost() {
return decoratedCoffee.getCost();
}
}
// Concrete Decorators
class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee coffee) {
super(coffee);
}
public String getDescription() {
return decoratedCoffee.getDescription() + ", milk";
}
public double getCost() {
return decoratedCoffee.getCost() + 0.5;
}
}
class SugarDecorator extends CoffeeDecorator {
public SugarDecorator(Coffee coffee) {
super(coffee);
}
public String getDescription() {
return decoratedCoffee.getDescription() + ", sugar";
}
public double getCost() {
return decoratedCoffee.getCost() + 0.2;
}
}
// Client code
public class DecoratorPatternDemo {
public static void main(String[] args) {
Coffee coffee = new SimpleCoffee();
System.out.println(coffee.getDescription() + " $" + coffee.getCost());
coffee = new MilkDecorator(coffee);
System.out.println(coffee.getDescription() + " $" + coffee.getCost());
coffee = new SugarDecorator(coffee);
System.out.println(coffee.getDescription() + " $" + coffee.getCost());
}
}
Discussion:
Have you used the Decorator Pattern to add functionalities to your objects? What scenarios have you found it most beneficial? Share your experiences and examples in the comments below!
?? Call to Action: If you found this post helpful, please like, share, and comment! Follow #ehadjistratis for more insights into Java Design Patterns and other tech topics. Let's continue building a community of learners and practitioners!
Looking forward to your thoughts and experiences!
Stay tuned for tomorrow's topic: Facade Pattern.
#Java #DesignPatterns #DecoratorPattern #Programming #Coding #SoftwareDevelopment #LearningJourney #JuniorDevelopers #TechCommunity #ehadjistratis