Considering architecture….
I normally have a shopping cart class with addItem and removeItem methods (amongst a couple of others). However in the spirit of real world shananigans carts should be acted on by customers – therefore should the addtoCart / removefromCart not be a method of the customer?
OR should I have an itermediate CustomerActsOnCart object that takes the customer and cart objects as arguments in the constructor and perform the manipulation in there???
Any musings would be most welcome…
Even if you give a
Customerthe methodsaddToCart()andremoveFromCart(), you would still have to have some logic inShoppingCartto have these methods actually do the state change when an item is added or removed. IMO, the actor in this case is much better represented by a reference to the owning Customer and/or viceversa, e.g.You could approach this from the customer as well, e.g.
But the method name
addItemToShoppingCartalready implies that theCustomeracts on aShoppingCart, so inside you are likely doingIf
ShoppingCartwas a composite element ofCustomerfor which we wanted to hide implementation details, we could use something like that, but since theShoppingCartis IMO not an essential part of a Customer this method shouldnt be on Customer. It’s nothing but a convenience proxy.You could create a Decorator for the
Customerto handle the Shopping Behavior. This would decouple the concern/responsibility of How-To-Shop from the Customer, e.g. something likeThis would also allow you to apply the Shopping Behavior to other actors that might need this behavior.
The
CustomerActsOnCartapproach sounds somewhat like the Mediator Pattern. Not sure if it’s a feasible approach though.