Say I have a class User and an interface Administrator. Some users are administrator and can thus implement the interface. Most aren't, however, so it's false to say that "User implements Administrator".
How can I represent that in a class diagram? Should I use the classic implementation arrow, or is there an existing stereotype for a class that can implement an interface?
2 Answers 2
What you want is a Decorator.
An Administrator would be a subclass of User, and its primary member attribute would be a User object.
Thus a Student Adminstrator would be an Administrator containing a Student (which is a subclass of User, right? The Administrator-specific attributes are set in the Adminstrator class, with the User methods passed through from the User object.
-
2Oooh, really good idea. Why make Administrator a subclass of User, though?Bobson– Bobson2013年09月25日 15:16:26 +00:00Commented Sep 25, 2013 at 15:16
-
Ah yeah, I've heard a similar idea with having an Administrator class containing a User attribute. Then I can use the Administrator everywhere I need it.Timothy Daniel– Timothy Daniel2013年09月25日 15:37:23 +00:00Commented Sep 25, 2013 at 15:37
-
@Bobson the Decorator pattern allows individual instances of a class to be enhanced without requiring any modification to the design of the original type.. The enhanced object has to be usable wherever the original type can be used. Otherwise, you have to litter the user object with
isaWhatever
flags, and you can only enhance the object in ways specifically designed in.itsbruce– itsbruce2013年09月25日 15:40:59 +00:00Commented Sep 25, 2013 at 15:40 -
1@itsbruce that still doesn't mean that an administrator needs to be a subclass of a user, just that the administrator interface needs to be an extension of the user interface. And even when you don't do that and both interfaces are unrelated (except for the language's root interface interface), you wouldn't need to litter your code with isaWhatever flags. Just request the administrator interface from the user interface reference and have "normal" user interface implementers support a null-pattern for the administrator interface.Marjan Venema– Marjan Venema2013年09月25日 17:05:05 +00:00Commented Sep 25, 2013 at 17:05
-
@MarjanVenema Indeed, the idea behind the decorator pattern would be that the User interface is a generalization (super-type) of the Administrator interface. The concrete realizations of those interfaces are a separate matter.Matthew Flynn– Matthew Flynn2013年09月25日 17:54:24 +00:00Commented Sep 25, 2013 at 17:54
It makes no sense to say that a class can implement an interface. Either it does or it doesn't, and that's determined as the code is written. Specific instances (users) can't make that decision for themselves, although they can set an IsAdministrator
flag instead.
If the IsAdministrator
flag is insufficient, then (as @superM said) the design you want is subclassing. A User
has some properties. Some User
are actually Administrator
, which has additional properties. Therefore Administrator
inherits from User
, and should be represented as such.
IsAdmin
flag on theUser
? It sounds like being an admin is a property of a user, not an extension to it.