This problem is the reason that they came up with a sample Design Decorator . The template allows you to add "features" to your class. For example, let's say you have a Coffee class. Each instance of coffee can be with sugar, milk, cream, sweeteners, or whipped cream (let none of them be mutually exclusive). Therefore, instead of five logical parameters (one for each coffee characteristic). You will have the following hierarchy:
public interface Coffee { public void drink(); } public class CupOfCofee implements Coffee { public void drink() { print("Yumm coffee"); } } abstract class CoffeeDecorator implements Coffee { protected Coffee decoratedCoffee; public CoffeeDecorator(Coffee decoratedCoffee) { this.decoratedCoffee = decoratedCoffee; } public void drink() { decoratedCoffee.drink(); } }
This design makes your code more readable and extensible, dividing the behavior of each logical parameter into a separate class. The defiant drink will print: "Yumm sugar milk Yumm coffee milk"
source share