200人1992 X- 200 200 X- 200 200 -40 200 200 -40 200 200 -40 200 200 -40 200 200 200 200: [英] Modelling "I'm a * but I'm also a **"
问题描述
在[这篇文章]中,我正在努力执行@jonp建议的状态模式。我不太了解如何使用他发布的内容,但它导致了这样的想法,也许我试图将一个方形钉插入一个圆孔。所以我的问题:
如果我有一个可以播放多个角色的访问者,即用户
可以一个供应商
,一个雇主
,一个广告客户
或全部以上,应该用继承?我已经宣称:
class Vendor:User {}
class Advertiser:User {}
等等,但是当用户既是供应商又是雇主,则不同类的实例确实指向相同的底层对象...我不知道这可以工作。如何建模?
*更新*
感谢大家所有得到一点(这是我可以给的))。我一直在用EF,downcasting和最近几天的状态,把头发拉出来。角色方法更有意义。
这听起来像一个属性模式 (或所以我称之为)将是非常合适的。这是一种比简单继承更加松散的方法,可以用于指定多个行为,或者在您的案例类型用户
中。这实际上没有比具有另一种对象的标签的对象更复杂。
实现它的最简单的方法是具有一个具体的用户
类,具有只读属性 IList< UserRole>
(内部可以是列表< T> code>字段)您的
UserRole
类将是抽象的,而 VendorRole
/ AdvertiserRole
/等。将从它导出,允许您将任意数量的不同角色(甚至是相同类型的角色)标记给给定的用户。另外还可以定义一个 GetRole< TRole>
用户
类中的code>方法,以便于访问特定类型的角色(假设每个用户
仅具有单个角色
的特定子类型)。 旁注:您还可以考虑 装饰者patern ,这与上述模式密切相关 - 虽然我个人觉得这在这里是过度的,在灵活性或权力方面真的没有增加。它常常掩盖你想要做的事情;尽管如此,还是要随时调查。
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?
* update *
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.
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.
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.
这篇关于200人1992 X- 200 200 X- 200 200 -40 200 200 -40 200 200 -40 200 200 -40 200 200 200 200:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!