Decorator Pattern (Private vs Protected)
Decorator Pattern (Private vs Protected)
I noticed that most of the examples use protected for the Instance that is being decorated. Could I use private too?
public abstract class CoffeeDecorator implements Coffee {
protected Coffee decoratedCoffee;
public CoffeeDecorator(Coffee c) {
this.decoratedCoffee = c;
}
...
}
protected
private
private access modified variables/methods aren`t accessible by subclasses.– Alan
2 mins ago
private
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
protectedmakes it available to subclasses which mostly is essential in the Decorator Pattern. If it wasprivatethe subclasses then could not access it anymore– Lino
2 mins ago