Inheritance vs. Strategy Pattern
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 more information, please let me know! :)
Thanks in advance.
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.