• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pattern for roles ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have a Person class.
And I have two roles which I want to model:

  1. Buyer
  2. Seller

  3. How do you implement this ?
    I've tried using interfaces.
    e.g.

    The problem I see here is that I can't changed my role
    dynamically. For example, the program might allow a Person
    to assume another role in the middle of the program.
    Thanks
    Pho
 
Junilu Lacar
Bartender
Posts: 7599
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Fowler discusses this in his book "Analysis Patterns". You might find some material on his website: http://www.martinfowler.com
Junilu
 
Jes Sie
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:
Say I have a [b]Person class.
And I have two roles which I want to model:

  1. Buyer
  2. Seller

  3. How do you implement this ?
    I've tried using interfaces.
    e.g.

    The problem I see here is that I can't changed my role
    dynamically. For example, the program might allow a Person
    to assume another role in the middle of the program.
    Thanks
    Pho[/B]

This maybe a late reply, but, heh, it's better never.
OK, maybe you can try like this: -
Person has a "1 to 0..*" composition relationship with PersonRole.
Place a Condition on the relationship like "Max 1 each".
PersonRole is an abstract class.
Both Buyer and Seller inherits from PersonRole.
This way, Person can have zero or a max of either Buyer or Seller role.

------------------
Thank you.
- Simon See
simon-ivy@usa.net
-after office hours-
necro-mani-cide
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic