• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Pattern for roles ?

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
[email protected]
-after office hours-
necro-mani-cide
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic