Pretty simple setup: I've got a
Cart and the cart has
Items. I've got many varying types of "products" that can't really be boiled down into one thing or even a common ancestor, so I've instead decided to simply create a representation of the product as a
CartItem, which contains all the information from the product needed to make the sale.
That's where the question comes in. I could handle this in three ways that I see. For these examples we'll just say I have two types of "products":
Gizmos. Again, these two things are so vastly different that it doesn't make sense for them to inherit from any base class.
1) Both a
Widget and a
Gizmo have the ability to create a
Cart Item to represent themselves, but this requires both to have knowledge of
CartItem and any change to
CartItem will require me to touch both
Gizmo (not ideal, obviously)
CartItem can create itself from either a
Widget or a
Gizmo. This would essentially be a factory pattern and would require
CartItem to have knowledge of both
Gizmo. This is better than #1 in that I don't have to make changes to every kind of "product" if I change
CartItem, but I do have to remember that if I change a particular "product" then I must change the factory method of
CartItem for that. (still not ideal, because Murphy says I will forget).
3) Use something like the controller as the "glue".
GizmoController will have
AddToCart actions that translate a
Gizmo, respectively, into a
CartItem. This really sort of presents the same problems as #1, in that if
CartItem changes, I need to update all the "product" controllers, and in fact mixes in the problems of #2, in that I must remember to always maintain this mapping if something on either side changes. However, it feels more like a job for the controller: it's oriented for dealing with multiple types, whereas it just feels wrong to mix in types at the model level.
4) Three that I can see; #4 would be something I have yet to consider that one of you kind individuals may happen to suggest.
I'm open to any comments here. If I'm missing something completely obvious, feel free to call me a moron and point how just how big a moron I am. I like feeling stupid; that's when I'm learning ;).