gpt4 book ai didi

策略模式的 UML 类图

转载 作者:行者123 更新时间:2023-12-03 18:19:34 25 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

4年前关闭。




Improve this 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.



我尝试过这样的事情,认为它可以适合策略设计模式,但我完全不确定。

enter image description here

最佳答案

首先是商店,可以是杂货店。任何商店都有两种类型的对象:元素和购买。没有元素和/或购买,商店将不复存在。商品将在没有商店的情况下存在(例如,它们必须交付到商店),但购买属于商店本身。这就是为什么 store 和 item 之间存在聚合关系,而 store 和 purchase 之间存在组合关系。

元素有一些特征,如名称、食用、应税、重量和单位。我没有实现一个项目的所有特征,但想法就在那里。有很多方法可以出售这些元素。可能同时使用更多策略。针对这种情况设计了装饰器模式。它可以在运行时动态添加多种策略。
它是装饰元素-界面,因为它与元素相关联,因为销售方式的变化在元素中找到了它的原因。它与类购买有关联,但不是购买的一部分。将出售的每件元素都属于特定的购买。它还可以具有尚未出售的状态。这就是为什么购买的多重性是 0-1。总而言之,归结为:

class diagram of items sold in a store

关于策略模式的 UML 类图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16212314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com