سؤال

I have an exam in a couple of hours and I am trying to solve sample questions for it. I couldn't find a solution to following question.

Draw a class diagram corresponding to the following situation:

A grocery store (e.g., a supermarket) sells items. There are two types of items: edible (i.e., any item that can be used as food) and non-edible. Some items are sold by weight, and some are sold per unit. Some items are taxable, while others are not. Some items have special prices when sold in groups (e.g., 2 for $3). A purchase may contain many items.

In your class diagram, show only the classes/abstract classes/interfaces, associations, and multiplicities for the associations. Do not include any operations! Clearly indicate interfaces and abstract classes (if any) in your design.

Furthermore, your design should be as flexible as possible. In particular, you need to pay attention to the following points:

1) An item is either edible or not and this fact does not change during the lifetime of the item, whereas the pricing strategies may change during the lifetime of the item.

2) Each pricing strategy is associated with a certain set of operations. For example, for the taxing strategy, whether or not an item is taxable, we may have an operation that computes the tax (this function can then return 0 for non-taxable items)

3) Although the set of operations for a pricing strategy stays the same, the ways those operations are handled may change over time. For example, the way we compute taxes for edible and non-edible items may differ over time.

4) Although the set of operations for a pricing strategy stays the same across different items, we may want to implement those operations in different ways for different items. For example, we may want to compute the tax differently for oranges and apples.

I tried something like this thinking that it could suit to the Strategy Design Pattern, but I am not sure at all.

enter image description here

هل كانت مفيدة؟

المحلول

First is there a store, which can be a grocery store. Any store has two type of objects: items and purchases. Without items and/or purchases will the store stop to exist. Items will exist without the store (they must be delivered to the store for instance), but the purchases belong to the store itself. That is why there is an aggregational relationship between store and item, yet a compositional relationship between store and purchase.

Items have some features, like name, edible, taxable, weight and unit. I did not implement all characteristics of an item, but the idea is there. There are a lot of ways to sell those items. There might be more strategies used at the same time. For that situation is the decorator pattern designed. It can add several strategies dynamically at runtime. It is decorating the item - interface, because it is associated with the item, because change in the way to sell finds its cause in the Item. It has an association with the class purchase, but it is not part of the purchase. Every item that will be sold belongs to a specific purchase. It can also have the state not being sold yet. That is why the multiplicity of the purchase is 0-1. All in all boils it down to this:

class diagram of items sold in a store

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top