I think a classic example is that the Window class can have a Border decorator and a ScrollBar decorator in the GoF book.
What are some situations you think, know of, in which the Decorator Pattern solves the problem really well?
I think a classic example is that the Window class can have a Border decorator and a ScrollBar decorator in the GoF book.
What are some situations you think, know of, in which the Decorator Pattern solves the problem really well?
Anything that is abstractable to an extendable root upon which you have to define different overlapping and interchangeable behaviors:
and so on...
I've used the Decorator pattern for managing complex roles.
Example off the top of my head:
public abstract class DecoratedUser : User
{
protected User _decoratedUser;
public DecoratedUser(User decoratedUser)
{
_decoratedUser = decoratedUser;
}
public abstract List<Permissions> GetPermissions;
}
public EditorUser : DecoratedUser
{
public EditorUser(User decoratedUser)
: base(decoratedUser)
{}
public override List<Permissions> GetPermissions
{
// return previous permissions + editor permissions
}
}
public ModeratorUser : DecoratedUser
{
public ModeratorUser(User decoratedUser)
: base(decoratedUser)
{}
public override List<Permissions> GetPermissions
{
// return previous permissions + moderator permissions
}
}