views:

1179

answers:

2

I am still trying to wrap my head around design patterns and for the second time I'm coming up against the same problem that seems to be crying out for a pattern solution.

I have an accounts system with multiple account types. We have restaurant, hotel, service_provider, and consumer account types. Im sure there will be more business account types in the future, and of course there's a global administrator account.

So what I'm wondering is how to implement the switching of account types. Eg. each account will have one or more profiles, but the profile will be different depending on the account type. What kind class relationships should I use here to deal with the multiple types of account - polymorphism or inheritance?

It seems like maybe there should be an abstract base Profile class that the other profiles should extend, but I'm not sure how to implement that (eg a join table between profile types and account types?).

It also feels like an opportunity to implement the factory pattern, I'm just not sure really how to go about it.

Any ideas please?

*

*

Edited to provide some examples as suggested:

Account -> hasMany   -> Users

Account -> belongsTo -> AccountType

Account -> hasOne    -> Profile

The profile is different depending on what type of account it is, eg an account of type restaurant will have a menu, a wine list etc, an account of type hotel will have room types, amenities, an account of type consumer will have personal tastes, home country etc.

The question was what design pattern would best implement these relationships.

Hope thats clearer, thanks!

+1  A: 

I'd suggest aggregation rather than inheritance here for the relationship between account and profile, but have an Account base class that is inherited into multiple account types.

The account contains a profile object, which can be set in the constructor of each polymorphic account type.

You could wrap the account creation in a factory or virtual constructor pattern as well.

workmad3
A: 

thanks for the examples; you may be trying to make this harder than it is. Would the following work?

User <<--> Account
Account <<--> AccountType
Account <--> Profile
Profile <<--> ProfileType

I question the account-profile 1:1 relationship, it seems likely that an account may end up with more than one profile, or that a profile might belong to a user instead of an account, but i don't really know what a profile is/does in this context

Steven A. Lowe