iOS design mode-(2) fine-solution of relationship between UML classes

Source: Internet
Author: User

Before formally speaking design mode, it is necessary to introduce the relationship between UML class diagram, because some tutorials, books, including my post, will use a lot of class diagram, to describe the relationship between the various classes. This is a very intuitive, minimalist way.

Of course, the ability and energy are limited, the introduction of the UML here is only limited to a few common inter-class relationships.

Include: inheritance, implementation, dependency, association, aggregation, composition



Before the second, assuming that you do not understand the class diagram, you can first look at the article I wrote earlier: specific explanation of eight UML class diagram notation


IOS-UML relationship between classes by Colin,

Reprint Please specify source: http://blog.csdn.net/hitwhylz/article/details/40372781






One. Inherited


Refers to the ability of a class (called a subclass, sub-interface) to inherit the functionality of another class (called the parent class, parent interface) and to add its own new functionality, which is the most common relationship between a class and a class or interface and interface;

In the objective-c, this kind of relationship is passed: Clear identification, in the design of the general non-controversial;


UML Class diagram: (represented by a hollow triangle + implementation.)

Direction from sub-class to parent class)



Here comes the iOS class inheritance diagram:





Two. Realize


A class class implements the interface interface (which can be multiple), and implementation is the most common relationship between classes and interfaces;

UML class diagram (denoted by a hollow triangle + dashed line). Point to interface from Class)



Three.

Depend on


It is easy to understand that a class A is used to have a class B. The use of such a relationship is contingency, temporary, very weak, but B-class changes will affect A; For example, a person must cross the river to borrow a boat, when the relationship between man and ship is dependent, for example, people need oxygen metabolism, then people rely on oxygen, they are dependent on the relationship.

The table is now at the code level. For Class B as a parameter is used by Class A in a method;


UML class diagrams (denoted by dashed arrows, from dependent [person] to dependent [oxygen], note: The code shows oxygen as a reference in the human metabolic method, such as: + Metabolism (Ino2: oxygen))



Four. Association


He embodies a strong dependency between the two classes, or the semantic level between classes and interfaces, such as my friends and I, who are more dependent and less dependent on the contingency, and the relationship is not temporary. is usually long-term. And the relationship between the two sides is usually equal, the correlation can be one-way, two-way;

The table is now the code level, for the associated class B in the form of a class attribute is now associated with Class A, or the association Class A refers to a type is a global variable of the associated class B;


UML class diagrams (denoted by implementation arrows)




Five.

Polymerization


Aggregation is a special case of association relationships. He embodies the relationship between the general and the part, the ownership, that is, the relationship between the has-a and the general and the part is separable. They are able to have their own life cycle. Part can belong to multiple overall objects, can also be shared for multiple overall objects, such as computer and CPU, the relationship between the company and employees;

The table is now the code level, and the correlation is consistent, can only be differentiated from the semantic level, that is, the association Class B in the form of a class attribute is now associated with Class A, or the association Class A refers to a type is associated with the Class B global variables.


UML class diagram (denoted by a hollow diamond + solid Arrow)



Six. Combination


The combination is also a special case of the relationship, he embodies a contains-a relationship, which is stronger than aggregation, also known as strong aggregation, he also embodies the relationship between the general and part, but at this time the overall and part is not divided. The end of the overall life cycle also means that part of the life cycle is over, like you and your brain.

The table is now at the code level, and the correlation is consistent and can only be differentiated from the semantic level. That is, the association Class B in the form of a class attribute is now associated with Class A, or the association Class A refers to a type is associated with the Class B global variables.


UML Class diagram: (represented by a solid diamond + solid arrow, both ends can have numbers.) For example, 1 people have 1 brains, the ends of which are 1, 1)




There is little doubt about inheritance and realization of these two relationships, they embody a kind of class and class, or the vertical relationship between class and interface;

The other four relationships reflect the class and class, or the class and interface between the reference, the horizontal relationship is more difficult to distinguish, there are a lot of things to prepare for positioning is very difficult, as mentioned earlier, these relationships are semantic level, so from the code level does not completely distinguish between the various relations;

However, in general, the following relationships show the degree of strength: Combination > Aggregation > association > dependency;

Here's a careful comparison:



1. Aggregation and Combination
(1) Aggregation and combination are a kind of binding relationship, which is simply an additional general-part implication.



(2) different life cycle of parts

In an aggregation relationship, the entire piece does not have a life cycle of the part, so the part is not deleted when the whole piece is deleted. Furthermore. Multiple entire pieces can share the same part.
In a composite relationship, the entire piece has the life cycle of the part. So when the whole piece is deleted. Parts must be deleted. And. Multiple entire pieces cannot share the same part at the same time.

(3) The aggregation relationship is a "has-a" relationship, and the combined relationship is a "contains-a" relationship.

2. Associations and Aggregations
(1) The table is now the code level, and the association is consistent, can only be differentiated from the semantic level.



(2) The difference between association and aggregation is mainly semantically, and the associated two objects are usually equal to each other. For example, you are my friend, aggregation is generally not equal.

(3) Association is a structured relationship, which refers to an object that is associated with another object.



(4) Correlation and aggregation are dependent on the problem domain, such as in the field of car care. Tyres must be combined in a car class, as it is meaningless to leave the car. But in the business of selling tires. Even if the tires leave the car. It also makes sense, and it can be aggregated.

3. Associations and Dependencies
(1) In the association relationship. Represents a strong dependency between two classes, or the semantic level of a class and an interface. For example, my friend and I, this relationship is more than relying on a stronger, non-dependent contingency, the relationship is not temporary, usually long-term. And the relationship between the two parties is usually equal.

(2) In the dependency relationship, it is simple to understand that a class A is used to have a class B, and such a use relationship is contingency, temporary, very weak, but the change of Class B will affect A.

4. Comprehensive comparison
These relationships are semantically level, so it is not possible to completely differentiate relationships from the code level, but in general. The degree of strength of the latter relationship is as follows:

Combination > Aggregation > Correlation > dependencies;


Here's a simple example that distinguishes between aggregations and combinations.

Wild geese like the lively fear of loneliness, so they have been living in a gregarious life, so that there are geese flock every goose has its own flock. Every flock has a lot of geese, geese and geese such a relationship can be called aggregation.

In addition, each goose has two wings only, the relationship between the wild Goose and the Wild Goose Wings is called the combination. It can be seen that the relationship between the aggregation is obviously not tightly knit, the geese will not be able to survive because of their group of geese, and the wild Goose wings can not be separated from the wild Goose and live alone-the combination of the class has the same life cycle.

Aggregation diagram:

Composition diagram:

Code performance:

Flock of wild Goose class:

public class Goosegroup    {public Goose Goose;        Public Goosegroup (Goose Goose) {this.goose = Goose; }    }

Wild Goose Category:

    public class Goose    {public        Wings Wings;        Public Goose ()        {            wings=new wings ();        }    }

The class of the aggregation relationship contains a class as a parameter.
The Wild Goose (Goose) is used as a reference in the goosegroup of the Wild Geese (the Goose) can be separated from the wild geese and exist independently.
The class of the combined relationship contains an instantiation of another class.
Wild Goose (Goose) before instantiating, be sure to instantiate the Wings Class (Wings) Two classes tightly coupled together they have the same life cycle Wing class (Wings) can not be separated from the Wild Goose (Goose) and independent existence
The encapsulation of information is different
In the aggregation relationship, the client can understand the wild geese and the wild geese at the same time, because they are all independent
And in the combinatorial relationship. The client only knows the Wild goose class. There is no knowing the existence of wing class, because the wing class is tightly encapsulated in the Wild Goose class.



That's roughly the case. Of course, this is only part of the UML. If you are interested, you can learn in depth.


Study on the road, with June encouragement

iOS design mode-(2) fine-solution of relationship between UML classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.