Decorator Pattern (Private vs Protected)

The name of the picture


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 makes it available to subclasses which mostly is essential in the Decorator Pattern. If it was private the subclasses then could not access it anymore
– Lino
2 mins ago




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.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Better method to check all objects' parameters with a single call

Visual Studio Code: How to configure includePath for better IntelliSense results