我应该使用哪种设计模式来建模人员-角色关系?

发布于 2024-10-15 22:16:18 字数 842 浏览 2 评论 0 原文

我只是不知道我应该在这里采用哪种设计模式。假设我有这样的类:

class Person

String role;

public void takeRole(String role) {
  this.role = role;
}

public void do() {

  switch(role)

    case a:
      do this and that;
      this.role = b;

    case b:
      do this and that;

    case c:
      do this and that;
      this.role=a;

    ....

简而言之,一个 Person 有角色, do() 方法取决于他的角色是什么。在某些情况下,他可能不得不转换角色。我认为这个 do() 应该被抽象(更是如此,因为将来可能会定义其他角色)——但是如何抽象呢?应该有一个角色类吗?

任何帮助将不胜感激。

编辑:

感谢大家抽出时间。我还想补充一件事。我确实想到了(至少作为一个想法)许多建议的解决方案。以下是我的困难:如果我对 Person 类进行子类化(例如 PersonTypeA、personTypeB 等),并将每个特定角色分配给适当的人员类型,那么我在切换角色时会遇到困难(例如,工程师变成会计师! ---至少可以说

,如果我为每个角色创建类,那么(1)角色感觉不像一个对象,因为(至少在我的情况下)角色没有属性。 我都必须创建一个新的角色对象——即使他们都共享相同的角色。

但只有方法。因此,role_A_1 与 role_A_2 没有什么不同,但对于每个人, 我自己很清楚,我不确定我的观点是否有意义。

I just couldn't figure out which design pattern I should adopt here. Say I have class like this:

class Person

String role;

public void takeRole(String role) {
  this.role = role;
}

public void do() {

  switch(role)

    case a:
      do this and that;
      this.role = b;

    case b:
      do this and that;

    case c:
      do this and that;
      this.role=a;

    ....

In short, a Person has roles and do() method depends on on what his role is. In some cases, he may have to switch roles. I think this do() should be abstracted (more so because there may be other roles defined in the future)---but how? Should there be a Role class?

Any help will be appreciated.

Edit:

Thanks for sparing your time, people. I'd like to add one more thing. I did think (at least as an idea) many of the suggested solutions. Here are are my difficulties: If I subclass Person class (such as PersonTypeA, personTypeB, etc., and assign each particular role to the appropriate person type, then I have a difficulty when switching roles (for instance, an engineer becomes an accountant!---Weird, to say the least.

On the other hand, if I create classes for each role; then (1) a role does not feel like an object, because (at least in my case) a role does not have attributes but only method(s). Consequently, role_A_1 is no different than role_A_2. But for each person I'll have to create a new role object---even if they both share the same role.

I'm not sure if I made myself clear and I'm not sure if my points make sense at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

计㈡愣 2024-10-22 22:16:18

不是以设计模式的方式,而是更多以逻辑的方式:

而不是有一个巨大的“如果角色是这个,那么做那个,否则如果是这个”语句,带有 DoThis() 方法的某种 Role 类会更容易。

一个人有一个角色。一个人可以DoThis(),而该角色也可以DoThis()。

角色不是说他们根据角色做什么,而是说他们可以做什么。

Not in a design pattern way, but more in a logical way:

Instead of having a huge 'if role is this, do that, else if it's this' statement, some kind of Role class with a DoThis() method would be easier.

A Person has-a Role. A Person can DoThis() and that the Role then also can DoThis().

Rather than a Person saying what they do according to the Role, the Role says what they can do.

篱下浅笙歌 2024-10-22 22:16:18

我还看到这里出现了一种状态模式。例如,您可以按照以下方式进行设计:

interface Role {
    public void doRole(Context context);
}

class RoleA implements Role {
    public void doRole(Context context) {
        // do this and that
        context.setRole(new RoleB());
    }
}

class RoleB implements Role {
    public void doRole(Context context) {
        // do that and this
        context.setRole(new RoleA());
    }
}

class Context {
    private Role _role;

    public Context() {
        // set initial role
        setRole(new RoleA());
    }

    public void setRole(Role newRole) { _role = newRole; }

    public doSomething() {
        _role.doRole(this);
    }
}

在此示例中,您可以通过询问其当前分配的角色Context对象执行的行为> 反对这样做。具体角色本身具有定义角色之间转换的方法。实际上,这里出现的是一个简单的状态机,其中具体的角色是节点,而对 setRole() 的调用是边。

I see a State Pattern emerging here, as well. For example, you can design something along the line of:

interface Role {
    public void doRole(Context context);
}

class RoleA implements Role {
    public void doRole(Context context) {
        // do this and that
        context.setRole(new RoleB());
    }
}

class RoleB implements Role {
    public void doRole(Context context) {
        // do that and this
        context.setRole(new RoleA());
    }
}

class Context {
    private Role _role;

    public Context() {
        // set initial role
        setRole(new RoleA());
    }

    public void setRole(Role newRole) { _role = newRole; }

    public doSomething() {
        _role.doRole(this);
    }
}

In this example, you delegate what behavior a Context object does by asking its currently assigned Role object to do it. The concrete roles themselves have methods that define the transitions between roles. In effect, what emerges here is a simple state machine where concrete Roles are the nodes and calls to setRole() the edges.

你丑哭了我 2024-10-22 22:16:18

我发现与人、公司和角色打交道是一次又一次重复的事情。 Martin Fowler 的分析模式书(与 Ward Cunningham 和 Ralph Jackson 合着)进行了广泛的讨论,值得深入阅读。

“处理角色”一章的在线版本(或其摘要,我手边没有这本书)可以在以下位置找到:http://martinfowler.com/apsupp/roles.pdf

Martin Fowler 的网站还有一个完整的部分涉及分析模式:http://martinfowler.com/apsupp/roles。 pdf

I have found that dealing with persons, companies and roles is something that is repeated over and over again. Martin Fowler's analysis patterns book (with Ward Cunningham and Ralph Jackson) has an extensive discussion that is worth reading in depth.

An online version of the "Dealing with roles" chapter (or a summary of it, I don't have the book handy) can be found as: http://martinfowler.com/apsupp/roles.pdf.

Martin Fowler's site also has an entire section dealing with analysis patterns: http://martinfowler.com/apsupp/roles.pdf

梦亿 2024-10-22 22:16:18

您可能需要查看 Coad 的域中立组件:

http://www.petercoad.com /download/bookpdfs/jmcuch01.pdf

这让人们扮演多个角色,但通过包含事件(时刻间隔)使事情更进一步。他还谈论了很多关于色彩建模的内容,这很有趣,但不要让它让你失望——我认为这些想法非常合理。

伊恩.

You may want to look at Coad's Domain-Neutral Component:

http://www.petercoad.com/download/bookpdfs/jmcuch01.pdf

This has people with multiple roles but takes things a bit further by including events (moment-intervals). He also talks a lot about modelling in colour which is interesting but don't let it put you off - I think the ideas are pretty sound.

Ian.

淡写薰衣草的香 2024-10-22 22:16:18

我将使用 do 方法创建一个接口,例如

interface Action {
    void do()
}

然后定义一个 Map ,以便给定角色,可以通过 actionMap.get(role 检索相关操作)。然后只需通过 actionMap.get(role).do() 对操作调用 do()

I would make an interface with a do method, like

interface Action {
    void do()
}

And then define a Map<String, Action>, so that given a role, the related action could be retrieved by actionMap.get(role). Then simply call do() on the action by actionMap.get(role).do()

入怼 2024-10-22 22:16:18

我想说的是,角色有一个人。我有一个 Role 接口,其具体实现将包装一个 Person 并用我需要的任何新功能来装饰它。

public interface Role
{
    Person getPerson();
    void action(); // might need more here
};

public class Person
{
   // whatever fields you need
}

public class Admin
{
    private Person person;

    public Admin(Person p) { this.person = person; }

    public void action() { } // some some admin stuff.
}

I would say that a Role HAS-A Person. I'd have a Role interface whose concrete implementation would wrap a Person and decorate it with whatever new capabilities I needed.

public interface Role
{
    Person getPerson();
    void action(); // might need more here
};

public class Person
{
   // whatever fields you need
}

public class Admin
{
    private Person person;

    public Admin(Person p) { this.person = person; }

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