如何对只有某些实体组可以参与角色的复杂角色关系进行建模?

发布于 2024-07-14 04:14:06 字数 624 浏览 5 评论 0原文

假设我必须对餐馆的饭菜进行建模。

一顿饭可以由几个“组成部分”组成:(

  1. 薯条或米饭或楔子)
  2. 和(六种不同饮料之一)
  3. 和(七种不同酱汁中的一种或两种,或根本没有)

另一顿饭可以包括:(

  1. 沙拉或米饭) )
  2. 和(大蒜或无大蒜)

其他膳食可包括:

  1. 仅薯条

  2. 仅一杯饮料

  3. 仅...

我该如何建模? (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":

  1. (Fries OR rice OR wedges)
  2. AND (One of six different beverages)
  3. AND (One or two out of seven different sauces OR none at all)

Another meal can consist of:

  1. (Salad OR rice)
  2. AND (Garlic OR no garlic)

Further meals can consist of:

  1. Just fries

  2. Just a beverage

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秋心╮凉 2024-07-21 04:14:06

如果这是家庭作业,可能没关系......
但是 - 如果这要在现实世界的应用程序中使用,我强烈建议不要为每种食物使用具体的类,即。 可乐类、沙拉类、米饭类等如上面推荐。
这肯定会使您的应用程序变得不灵活。

如果有一个带有名称属性或类似属性的食品类和饮料类会好得多。

想象一下,仅仅因为现在有一个新的特殊食品或食品,就必须重建整个应用程序...不酷;)。

我认为其他答案中缺少的是团体的想法。
每个食物项目可以与其他项目一起属于一个组,或者单独属于一个组。

假设薯条、米饭和楔子属于 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 anything

foodItem(id, name, desc) - represents a single item - fries, rice, etc.

foodItem_group(foodIgem_Id, group_Id) - maps food items to their group - many to many

combo(id, name, desc) - describes a combo

combo_group(combo_Id, group_Id) - maps groups to combos - many to many

I 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.

望笑 2024-07-21 04:14:06
  1. 看起来订单可以包含餐食、成分或两者的混合,所以我想说,有一个 Order 类,其中包含 Component 列表,并且膳食Meal 应该是 Component 的子类,或者它们应该实现相同的接口。
  2. 一顿Meal由几个“槽位”组成,这些“槽位”可以由一些Component集填充。 Meal 应该知道它们有多少个槽位以及哪些 Component 可以填充它们。
  3. “从 Component 列表中检测 Meal”问题很棘手。 在我的脑海中,我能想到的最好方法是为每个 Meal 提供一个方法,该方法采用 Component 列表,并在该 Meal 时返回 true 可以由它们制成(或者可能是构成MealComponent 的子集)。 Order 将遍历它所知道的 Meal 列表,并查看是否可以从当前 Component 中制作其中任何一个。代码>订单。 不过,可能有更好的方法来做到这一点。

希望这可以帮助!

  1. It seems like an order can consist of either meals, components, or a mix of both, so I would say, have an Order class that has a list of Components and Meals. Meal should either subclass Component, or they should implement the same interface.
  2. A Meal consists of several "slots," which can be filled by some set of Components. Meals should know how many slots they have and what Components can fill them.
  3. The "detecting a Meal from a list of Components" question is tricky. Off the top of my head, the best way I can think of is giving each Meal a method that takes a list of Components and returns true if that Meal can be made from them (or maybe the subset of Components that would make up that Meal). Order would go through the list of Meals it knows about and see if any of them can be made from the Components in the current Order. There may be a better way to do this, though.

Hope this helps!

救星 2024-07-21 04:14:06

创建Component类,以及所有可能类型的组件的子类(或对象)。

创建一个抽象Meal类,以及所有可能的Meals类型的子类。 膳食可以检查某个组件列表是否与其匹配(用于从组件列表中检测膳食)。 一顿饭可以向顾客展示这顿饭的所有成分选择。

我同意阿曼达的观点,即膳食应该由“老虎机”组成。 每个槽代表膳食的一种成分选择,例如薯条或米饭或楔子。 槽还可以对 m-outof-n 选项进行建模。

膳食类:

class Meal
{
    class MealSlot
    {
        Add(Component);
        bool DoesItMatch(vector<Component> ComponentsVector)
        {
            //Check if this Slot is filled by some element(s)
            // of ComponentsVector 
        }
        PrintSlotOptions();
        vector<Component> Options;

        // for m-of-n option, if multiple items can be chosen in this slot
        int HowManyNeededToFillIt; 
    };

    bool DoesItMatch(vector<Component> ComponentsVector)
    {
        //Check if all Slots are filled by ComponentsVector elements,
        //using Slot::DoesItMatch function
    }
    void PresentChoices()
    {
        for(i=0; i < Slots.size(); i++)
             Slots[i].PrintSlotOptions;
    }
    vector<Slot> Slots;
};

具体膳食之一:(沙拉或米饭)和(大蒜或无大蒜)

class MealType2 : public Meal
{
    MealType2()
    {
        Slots[0].Add(Salad);
        Slots[0].Add(Rice);
        Slots[1].Add(Garlic);
        Slots[1].Add(NOTHING);
    }    
};

创建一个 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:

class Meal
{
    class MealSlot
    {
        Add(Component);
        bool DoesItMatch(vector<Component> ComponentsVector)
        {
            //Check if this Slot is filled by some element(s)
            // of ComponentsVector 
        }
        PrintSlotOptions();
        vector<Component> Options;

        // for m-of-n option, if multiple items can be chosen in this slot
        int HowManyNeededToFillIt; 
    };

    bool DoesItMatch(vector<Component> ComponentsVector)
    {
        //Check if all Slots are filled by ComponentsVector elements,
        //using Slot::DoesItMatch function
    }
    void PresentChoices()
    {
        for(i=0; i < Slots.size(); i++)
             Slots[i].PrintSlotOptions;
    }
    vector<Slot> Slots;
};

One of Concrete Meals: (Salad OR rice) AND (Garlic OR no garlic)

class MealType2 : public Meal
{
    MealType2()
    {
        Slots[0].Add(Salad);
        Slots[0].Add(Rice);
        Slots[1].Add(Garlic);
        Slots[1].Add(NOTHING);
    }    
};

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 .

皓月长歌 2024-07-21 04:14:06

我认为这最终会存储在数据库中。 我建议创建两个表:

  1. 将存储组件(薯条、沙拉、大蒜等)
  2. 将有:id1、id2、关系。 关系是:
    • 属于
    • 搭配

根据“属于”关系,您可以查找所有成分是否属于某餐。 然后,也许可以去选择属于该餐食的所有成分,如果所选成分占该餐食的 50% 或更多,则建议该餐食。

根据“搭配”关系,您可以建议膳食或所选成分的附加内容。

I assume this will eventually get stored in a database. I suggest to create two tables:

  1. Would store the components (fries, salad, garlic, etc)
  2. Would have: id1, id2, relationship. Relationship being:
    • belongs to
    • goes with

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.

早乙女 2024-07-21 04:14:06

看起来你的餐几乎可以是任何食物的集合,所以从食物的一个抽象基类(或接口)开始。 制作许多具体的子类,每种食物一个:可乐、茶、牛排、鸡肉、土豆、米饭、面食、汤、沙拉等。

制作组件接口:开胃菜、晚餐、蛋白质、淀粉、甜点、饮料等。

当您编写代码和测试时,将您的具体类重构为它们似乎想要进入的任何层次结构。

在组件接口中洒一些有意义的地方。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文