Views: 7.5K
Replies: 1
Archived
|
Question - Strategy Design PatternHi All,
Following is the structure of my class public abstract class BaseUser { protected List<Perm> permissions; public abstract void AddPerm(Perm perm); } public class NormalUser : BaseUser { public override void AddPerm(Perm perm) { throw new InvalidOperationException("A normal user cant add permissions"); } } public class SpecialUser : BaseUser { public override void AddPerm(Perm perm) { if(permissions==null) permissions=new List<Perm>(); this.permissions.Add(perm); } } class Container { List<BaseUser> users; } What is required: 1. Container will keep both types of users 2. SpecialUser will have the functionality of add permissions - Done 3. Normal User will not be allowed add permissions - Done I have chosen strategy pattern to achieve above The thing I am not able to achieve is 4. Both Types of users will be hydrated from the Database (The users will be initialized with list of default permissions) Am I right in choosing this pattern in this situation? If yes then how do I address requirement 4? Many Thanks, asolvent A Solvent, Dec 28, 2011
|
|
Reply 1Hi,
Your selection is correct. When you're populating the users in Container from database, do you've any option to segregate the user type? If you, at the time of populating the users collection, based on the user type, you can create the relevant object and then while looping thru the users collection, the relevant object's method will be called. Thanks Sivakumar Sivakumar Sivaprakasam, Jan 05, 2012
|