Reputation: 32392
I have a class called pet
, which is dynamically associated to either 1 dog
or cat
but not both at the same time.
What's the name for this type of dynamic association? How can I represent this in a UML class diagram while making it clear that each pet
is associated to either one dog
or cat
but not both at the same time?
Upvotes: 1
Views: 519
Reputation: 5673
The wording of your question is bit funny when reading it as a model of the real world (a domain model).
In the real world, a pet is not associated with an animal. Rather, a pet IS an animal. Consequently, the class pets
is a (role) subclass of animals
, in a domain model, based on the meaning of the term "pet" in English.
The concept of role classes is not very well supported by mainstream OOP languages. An object may play many roles (that is, instantiate many role classes) at the same time (multiple classification) and it may cease to play a role, that is, cease to instantiate the corresponding role class (dynamic classification).
Maybe you are not interested in making a domain model first, before making a (technology-independent) design model, which you may then turn, e.g., into a Java or C# class model.
Maybe you want to jump to a C++ class model directly, without first trying to understand the underlying domain concepts.
You can do this, but I don't think it's a good idea.
Upvotes: 1
Reputation: 2352
Is what you're after simply inheritance? Pet seems to me to be an abstract concept, where as Dog and Cat would be concrete concepts. My initial solution in your situation would probably be to have an abstract Pet class (which cannot be instantiated) which is specialized to Dog and Cat (which can).
If you are really keen to have an instance of a Pet which is associated with an instance of either a Cat or a Dog, then you'd probably have to manage this by inheritance anyway. Something like this perhaps:
Upvotes: 2