如何对只有某些实体组可以参与角色的复杂角色关系进行建模?
假设我必须对餐馆的饭菜进行建模。
一顿饭可以由几个“组成部分”组成:(
- 薯条或米饭或楔子)
- 和(六种不同饮料之一)
- 和(七种不同酱汁中的一种或两种,或根本没有)
另一顿饭可以包括:(
- 沙拉或米饭) )
- 和(大蒜或无大蒜)
其他膳食可包括:
仅薯条
仅一杯饮料
仅...
我该如何建模? (UML、实体关系、代码……任何你能解释得最好的东西)
如果你知道我想要执行的一些任务,也许会有帮助,所以:
- 允许顾客首先选择一顿饭并显示所有剩余的“附加组件” 。
- 从成分列表中检测膳食。 例如,如果顾客点了薯条、酱汁和饮料,则应该可以从第一个示例中检测到餐食。
我考虑过将所有组件划分为文章,然后添加某种角色映射来将“薯条”标记为“芝士汉堡”、“炸肉排”、“...”的补充,但后来我想知道,如何对多个添加进行建模-ons、可选附加组件、n-out-of-m附加组件...
我希望你能帮助我...
Let's say I have to model the meals of a diner.
A meal can consist of several "components":
- (Fries OR rice OR wedges)
- AND (One of six different beverages)
- AND (One or two out of seven different sauces OR none at all)
Another meal can consist of:
- (Salad OR rice)
- AND (Garlic OR no garlic)
Further meals can consist of:
Just fries
Just a beverage
Just ...
How can I model this? (UML, entity-relationship, code, ... whatever you can explain best)
Perhaps it helps if you know some tasks I want to perform, so:
- Allowing the customer to choose a meal first and display all remaining "add-ons".
- Detecting a meal from a list of components. For example if the customer ordered fries, a sauce and a beverage, it should be possible to detect the meal from the first example.
I've thought about dividing all components into articles and then adding some kind of role mapping to mark "fries" as supplement to "cheeseburger", "schnitzel", "..." but then I wondered, how I could model multiple add-ons, optional add-ons, n-out-of-m add-ons...
I hope you can help me out...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果这是家庭作业,可能没关系......
但是 - 如果这要在现实世界的应用程序中使用,我强烈建议不要为每种食物使用具体的类,即。 可乐类、沙拉类、米饭类等如上面推荐。
这肯定会使您的应用程序变得不灵活。
如果有一个带有名称属性或类似属性的食品类和饮料类会好得多。
想象一下,仅仅因为现在有一个新的特殊食品或食品,就必须重建整个应用程序...不酷;)。
我认为其他答案中缺少的是团体的想法。
每个食物项目可以与其他项目一起属于一个组,或者单独属于一个组。
假设薯条、米饭和楔子属于 A 组。饮料属于 B 组。
然后,您可以将组合建模为组列表 - 即。 1 个 A 组项目和 1 个 B 组项目,或 2 个 A 组项目和 1 个 B 组项目。
您还可以使食品能够同时属于多个组......以使选项更加灵活。
数据库模型定义所有关系可能会变得复杂,但我认为这是必要的。
也许是这样的:
group(id, name, desc)
- 一组类似的物品 - 主菜、开胃菜、饮料...或任何东西foodItem(id, name, desc)
- 代表单个项目 - 薯条、米饭等。foodItem_group(foodIgem_Id, group_Id)
- 将食品项目映射到其组 - 多对多combo(id, name, desc)
code> - 描述组合combo_group(combo_Id, group_Id)
- 将组映射到组合 - 多对多我认为这可以用于表示基本所需的模型 - 您可能需要额外的表来存储客户的内容实际订购的..当然,检测客户订单是否与组合匹配取决于您的业务逻辑。
If this is homework, it may not matter...
But - if this is going to be used in a real-world app, I would strongly recommend against using concrete classes for each food item ie. Coke class, Salad class, Rice class, etc. as recommended above.
This is a sure way to make your application inflexible.
It would be much better to have a food item class and a drink class with a name property or some such..
Imagine having to rebuild your whole application just because there is now a new special or food item... not cool ;).
I think what is missing from the other answers is the idea of a group.
Each food item could belong to a group along with other items, or by itself.
Say fries, rice, and wedges belong to group A. Drinks belong to group B.
Then you could model a combo as a list of groups - ie. 1 group A item and 1 group B item, or two group A items and1 group B item.
You could also make food items able to belong to multiple groups at the same time... to make the options more flexible.
The db model could get complicated defining all of the relationships, but I think it's necessary.
Perhaps something like this:
group(id, name, desc)
- group of like items - entrees, appetizers, drinks... or anythingfoodItem(id, name, desc)
- represents a single item - fries, rice, etc.foodItem_group(foodIgem_Id, group_Id)
- maps food items to their group - many to manycombo(id, name, desc)
- describes a combocombo_group(combo_Id, group_Id)
- maps groups to combos - many to manyI think this would do for representing the basic required model - you may want additional tables to store what the customer actually ordered.. and of course detecting if a customer order matches a combo is left up to your business logic.
Order
类,其中包含Component
列表,并且膳食
。Meal
应该是Component
的子类,或者它们应该实现相同的接口。Meal
由几个“槽位”组成,这些“槽位”可以由一些Component
集填充。Meal
应该知道它们有多少个槽位以及哪些Component
可以填充它们。Component
列表中检测Meal
”问题很棘手。 在我的脑海中,我能想到的最好方法是为每个Meal
提供一个方法,该方法采用Component
列表,并在该Meal 时返回 true
可以由它们制成(或者可能是构成Meal
的Component
的子集)。Order
将遍历它所知道的Meal
列表,并查看是否可以从当前Component
中制作其中任何一个。代码>订单。 不过,可能有更好的方法来做到这一点。希望这可以帮助!
Order
class that has a list ofComponent
s andMeal
s.Meal
should either subclassComponent
, or they should implement the same interface.Meal
consists of several "slots," which can be filled by some set ofComponent
s.Meal
s should know how many slots they have and whatComponent
s can fill them.Meal
from a list ofComponent
s" question is tricky. Off the top of my head, the best way I can think of is giving eachMeal
a method that takes a list ofComponent
s and returns true if thatMeal
can be made from them (or maybe the subset ofComponent
s that would make up thatMeal
).Order
would go through the list ofMeal
s it knows about and see if any of them can be made from theComponent
s in the currentOrder
. There may be a better way to do this, though.Hope this helps!
创建Component类,以及所有可能类型的组件的子类(或对象)。
创建一个抽象Meal类,以及所有可能的Meals类型的子类。 膳食可以检查某个组件列表是否与其匹配(用于从组件列表中检测膳食)。 一顿饭可以向顾客展示这顿饭的所有成分选择。
我同意阿曼达的观点,即膳食应该由“老虎机”组成。 每个槽代表膳食的一种成分选择,例如薯条或米饭或楔子。 槽还可以对 m-outof-n 选项进行建模。
膳食类:
具体膳食之一:(沙拉或米饭)和(大蒜或无大蒜)
创建一个 Order 类,其中将包含膳食名称和组件列表。 如果订购了餐食,请调用 Meal.PresentChoices() 。 如果给出了组件列表,请检查所有餐食并调用 Meal.DoesItMatch 。
Create Component class, and sublcasses (or objects) for all possible types of Components.
Create an abstract Meal class, and subclasses for all possible types of Meals. A Meal can check, whether a certain list of Components matches it (for detecting a meal from a list of components). And a Meal can present to a customer all the choices of components for this meal.
I agree with Amanda that the Meal should be built of the "Slots". Each slot represents one of the Component choices of the Meal, e.g. Fries OR rice OR wedges. A Slot may also model the m-outof-n option.
The Meal class:
One of Concrete Meals: (Salad OR rice) AND (Garlic OR no garlic)
Create an Order class which will contain a Meal name, and a list of Components. If a Meal is ordered, call Meal.PresentChoices() . And if a list of Components is given, go over all the Meals and call Meal.DoesItMatch .
我认为这最终会存储在数据库中。 我建议创建两个表:
根据“属于”关系,您可以查找所有成分是否属于某餐。 然后,也许可以去选择属于该餐食的所有成分,如果所选成分占该餐食的 50% 或更多,则建议该餐食。
根据“搭配”关系,您可以建议膳食或所选成分的附加内容。
I assume this will eventually get stored in a database. I suggest to create two tables:
Based on the "belongs to" relationship, you could find if all components belong to a certain meal. Maybe then go and select all components that belong to that meal and suggest the meal if the components selected make up 50% or more of the meal.
Based on the "goes with" relationship, you could suggest add-ons to the meal, or to the components selected.
看起来你的餐几乎可以是任何食物的集合,所以从食物的一个抽象基类(或接口)开始。 制作许多具体的子类,每种食物一个:可乐、茶、牛排、鸡肉、土豆、米饭、面食、汤、沙拉等。
制作组件接口:开胃菜、晚餐、蛋白质、淀粉、甜点、饮料等。
当您编写代码和测试时,将您的具体类重构为它们似乎想要进入的任何层次结构。
在组件接口中洒一些有意义的地方。
seems like you meal can be almost any collection of food items, so start with one abstract base class (or interface) for a Food. make lots of concrete subclasses, one for each food: coke, tea, steak, chicken, potato, rice, pasta, soup, salad, etc.
make your components interfaces: appetizer, dinner, protein, starch, dessert, drink, etc.
refactor your concrete classes into whatever hierarchy they seem to want to go into as you write code and tests.
sprinkle in the component interfaces where they make sense.