I am learning Core Data so I was confused about the following.
I need to know how the relationships worked in the inherited scenario.
I have a data model with Entities Zoo, Animal , Dog, Lion.
Dog and Lion have Animal as the parent Entity.
Now Zoo has one to many relationship to animal.
I want to know if Zoo will need to have one to many relationship to animal or it need to have to one to many relationship to Dog and Lion. Will the inheritance work in this case or not. Like in future if I add Tiger, Do I need to add that in Zoo relationship or it will work just fine once I inherit Tiger from Animal.
As long as your child entities inherit from the Animal entity, they will also inherit the relationship that the Animal parent entity has with Zoo. It's like class inheritance in OOP, where subclasses inherit variables, methods, etc from their superclasses automatically.
So all you need to do is declare that relationship between Zoo and Animal in your data model, and any new entities you add which extend Animal will have the same relationship with Zoo — there is no extra work to be done to ensure this.
See the Managed Object Models section of Apple's Core Data Programming Guide for details.
Related
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?
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:
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.
I have a question about modeling associations between classes and interfaces. As far as I know, an interface specifies what an object can do; without providing the state or functionality (When to use an interface instead of an abstract class and vice versa?). Also, my book on OOAD (Object Oriented Modeling and Design by James Rubaugh)states that an association describes a group of links with common structure and common semantics, between object instances.
Now, suppose I have the following entities:
1) ICar Interface: Defines the operations a car can do
2) BMW : A class that realizes the ICar interface
3)IWheel : An interface defining the wheel capabilities
4) LuxuryWheel : A class that realizes the IWheel interface
Now, to model the relationship between BMW and LuuryWheel, which of the following do you think is correct, in a design perspective? I have shared my thoughts on each one
A) Create an association between ICar and Iwheel. BMW class can create concrete instances of LuxuryWheel class. This is highly flexible but couples car's capabilities with Wheel' s capabilities. Also, the definition of association says the relation is between instances.
B) Create an association between the BMW class and LuxuryWheel class. Solves the particular problem; but tightly couples BMW to Luxury wheels
C) Create an association between BMW class and Iwheel interface. This way BMW can use any type that realizes the IWheel interface.
Option C) looks better to me. Please share your thoughts.
I agree with Vladimir that, since you want to model cars and wheels with the help of interfaces, the association between them (which is actually a composition) should be modeled between the interfaces ICar and IWheel, as in the following diagram:
Since the classes BMW and LuxuryWheel realize the interfaces ICar and IWheel, they also need to realize/implement this association/composition, e.g. with the help of a 4-valued reference property BMW::wheels, or with the help of 4 different reference properties in BMW: leftRearWheel, rightRearWheel, leftFrontWheel, rightFrontWheel.
In order to get robust solution, create association between ICar and IWheel interfaces. It is possible , because interfaces are types. Connecting interfaces using association means, that any instance of classifier which realizes ICar interface must be associated to instance of classifier which realizes IWheel. You also define abstract classes for car and wheel and make association between them. The result will be similar.
Simply speaking a car can support different type of motors. So you must think to an additional class that permit to add different type of motors. In this case relation between interfaces or classes must be do it with some additional interface.
Let's say you have a many to many relationship between two entities and you use MagicalRecord to manually tie them together.
IE: A Vet has many Pets and a Pet has many Pets
In the code, do you need to add the entity on both sides of the relationship;
ie:
[vet1 addPetObject:cust1Pet1];
[vet1 addPetObject:cust1Pet2];
[vet1 addPetObject:cust1Pet3];
[cust1Pet1 addVetObject:vet1];
[cust1Pet2 addVetObject:vet1];
So, do I need to add the pet object to the vet, and then add the vet to the relevant pet object -- or will Core data / Magical record handle this relationship and tie them on both sides by itself?
Many thanks.
If the relationships are defined as inverse relationships of each other in the Core Data
model inspector then you need only set one of them. Core Data will automatically update
the inverse relationship. (This is in fact unrelated to MagicalRecord.)
On Wikipedia, I'm reading that an association relationship is an instance level relationship so we are talking about the relationship between the objects of two classes.
When we actually draw a class diagram, why do we use association on the class elements or blocks rather than objects? And there are also class level relationships for which we again use class elements. Since we don't have any way to show if we are talking about objects or classes I find this confusing. For example: I've heard people saying "Associate these two classes" Doesn't that sound wrong?
Links are to Associations as Objects are to Classes.
A Class is an abstraction that describes many specific objects. Similarly, an Association is an abstraction that describes many links between objects.
So your statement
an association relationship is an instance level relationship
isn't strictly correct because it mixes the abstraction (Association Relationship) with the instances it represents.
hth.
In fact, when you associate two or more classes it is done thanks to two or more UML Properties.
These latter are the "ends" of your associations and are "instance" i.e. they are typed by classe.
So an association is created between two classes on a class diagram but between each classes and the association you have a UML property.
Hoping it sounds clear ...
When you are making a class diagram you are defining types. Suppose you have a class User and a class Account, you use an association between User and Account to say: User instances can have link(s) with Account instances at runtime.
So, you use classes and associations at type level (class diagram) to define what can be possible at runtime (instance level).
The object is actually the Class that has been created virtualy. So a class is the "static" version of an Object. So, when we speak of UML, we speak about classes and not object.
But correct me if I'm wrong!
Can I improve my design on these 2 diagrams below? If so, how?
I am confused by the use of association classes in my diagrams. Should I use them?
Figure 1
Figure 2
For association classes, they make sense only if they represent associations with some behaviour and state. Look at Article'sSubject. It has nothing more than source, target and identity. You don't need a class for such a association, just use plain association, which has all those properties. There are more such unnecessary association classes in your diagrams. Another important thing when using association classes is that every instance of that association class should have unique source and target pair. For example Comment is not identifiable just by Article and User - one user may have man ycomments on one article and that is not allowed.
An association class is used to capture certain characteristics of an association between two classes. These characteristics do not belong to the classes being associated but instead belong to the relationship between the classes.