The problem
This is indeed a popular scenario in some industries such as apparels, where each item in the catalogue is configurable regarding size, color and style.
The stock of a configurable merchandise such as "Shirt"
does not make sense except for statistical purpose, but what really matters is the stock of the configured merchandises, e.g. {Item: "Shirt", size: "M", color="white", style:"italian colar"}
. Here it's even more complicated, since the configuration elements are dynamic.
Your solution
Your second diagram models this very well by using a combination that you've called Flavor
. So for the shirt, each possible combination of the configuration variables (Variety
), e.g. the tuple ( "M","white","italian colar)
would be a distinct flavor. Your association would class would hold the stock quantity of the Flavor
.
The multiplicity on the Variety
side would be by deduction 1..*
. The constraint then needs to express that for an occurence of Flavor
, the size of set of associated Variety
occurrences is the same than those indirectly associated with the Merchandise
. A full-text expression like you did is just fine. The other alternative would be to express this with a complex OCL predicate, which is very difficult considering some missing role names and the multiple indirections. Btw, most readers wouldn't anyway understand it.
However, I would not keep this solution:
- Its main weakness is that the
Flavor
seems independent from the Merchandise
, whereas in reality it only makes sense for a given Merchandise
(your constraint proves it).
- It is not easy to manage more complex stock, for example if you'd have a stock per warehouse.
Better alternatives
If you'd consider Flavor
as a flavor of a given Merchandise
, you could make this explicit and simplify the design: Flavor
would become the configured Merchandise
(instead of just a combination of Variety
) and could make it a component of the Merchandise
composite.
You could then simplify and store the stock quantity at the level of the Flavor
. Or you could manage the stock quantity in an association class between the Flavor
and a Warehouse
(which you could not really do with your current model).
Everywhere you'd use a Merchandise
, you'd use a Flavor
instead, facilitating for example the ordering or the shiping of configured products, which is much more difficult if you'd keep the flavor independent of the merchandise.
To avoid confusion, it would nevertheless be better to rename Flavor in something more explicit that reminds that it's a product that you manage in your system.