建模与QUOT;我是一个*,但我也是一个**" [英] Modelling "I'm a * but I'm also a **"

查看:130
本文介绍了建模与QUOT;我是一个*,但我也是一个**"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在[这个帖子],我挣扎实施状态模式为@jonp建议。我不完全得到如何使用什么是他发布的,但它导致了想,也许我试图适应方钉成一个圆孔。所以我的问题:

In [this post], I'm struggling to implement a State Pattern as @jonp suggests. I don't quite get how to use what's he's posted but it leads to the thought that maybe I'm trying to fit a square peg into a round hole. So my question:

如果我有一个客人到我的网站,可以扮演多种角色,即一个用户可能是一个供应商,一个雇主,一个广告客户或全部综上所述,我应该使用继承?我宣布:

If I have a visitor to my site that can play multiple roles i.e. a User could be a Vendor, an Employer, an Advertiser, OR all of the above, should I be using inheritance? I've declared:

class Vendor : User {}
class Advertiser : User {}

等等,但是当用户既是供应商和雇主则不同类的实例确实指向同一个底层对象...我不知道这是可行的。我怎么建模呢?

et cetera, but when a user is both a vendor and an employer then instances of different classes really point to the same underlying object... I'm not sure this can work. How do I model it?

*更新*

感谢大家(你所有拿到一分(这是我能给))。我一直在拉我的头发过深拷贝与EF,向下转型,为过去几天的状态模式。角色的做法更有道理。

thanks everyone (you all get a point (it's all I can give)). I've been pulling my hair out over deep-copies with EF, downcasting and the state pattern for the last several days. The role approach makes much more sense.

推荐答案

这听起来像一个情况,其中的属性模式 (还是让我称呼它)将是非常合适的。它比简单的继承一个更加松耦合的方式,可用于指定多个行为,或在您的案件种类用户的。这真是什么比另一种对象的标签的对象更加复杂。

This sounds like a situation to which the attribute pattern (or so I call it) would be very appropriate. It's a much more loosely-coupled approach than simple inheritance that can be used to specify multiple "behaviours" or in your case kinds of User. It's really nothing more complicated than an object having tags of another kind of object.

要实现这将是有一个具体的用户最简单的方法类,具有只读属性的IList< UserRole的> (内部这可能是一个列表< T> 字段也许)。然后,您的的UserRole 类是抽象的,而 VendorRole / AdvertiserRole /等。会从中获得,允许在不同的角色(即使是相同类型的那些)的任意数量的标记到一个给定的用户。这些角色除了可以定义自己的自定义行为,实用的方法,等等。

The easiest way to implement it would be to have a concrete User class, with a read-only property IList<UserRole> (internally this can be a List<T> field perhaps). Your UserRole class would then be abstract, and VendorRole/AdvertiserRole/etc. would derive from it, allowing you to tag on an arbitrary number of different roles (even ones of the same type) onto a given user. These roles can in addition define their own custom behaviours, utility methods, etc.

此外,你可以定义一个 GetRole< TRole> 用户方式类不但便于访问特定类型的角色(假设每个用户。有一个单一的角色特定亚型)

In addition, you could define a GetRole<TRole> method on your User class to facilitate access to roles of a specific type (assuming each User only has a single Role of a specific subtype).

附注:您还可以考虑的 装饰百通 ,这是密切相关的上述模式 - 尽管我个人感觉这是矫枉过正这里,真的没有增加任何在灵活性和动力方面。它往往只是掩盖你想要做什么;虽然随意反正进行调查。

Side note: you may also consider the decorator patern, which is closely related to the above mentioned pattern -- though personally I feel it is overkill here, and really adds nothing in terms of flexibility or power. It often just obscures what you're trying to do; though feel free to investigate anyway.

这篇关于建模与QUOT;我是一个*,但我也是一个**&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆