Posts

Showing posts with the label design-patterns

Inheritance vs. Strategy Pattern

Image
Clash Royale CLAN TAG #URR8PPP Inheritance vs. Strategy Pattern I'm learning about programming patterns and for our final assignment we were asked to build an "online store". I was thinking about how to model the User which can either be Admin or Customer . User Admin Customer I thought about the Strategy pattern to model each individual User with their particular behaviors. User They don't share any behaviors so far, so the Admin can't addToCart and Customer can't registerNewProduct . Admin addToCart Customer registerNewProduct However, they could share behaviors / methods as the system evolves! Furthermore, the User won't change it's type in run-time. I.e., once you log in as Customer, you can't log back in as Admin. User Even if they shared behaviors such as seeProductList , this could be achieved with good ol' inheritance, right? seeProductList Should I use Strategy, Inheritance or would you recommend another pattern? If you need mor...

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 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 ...