O-c Correlation-05: Object-to-object relationships

Source: Internet
Author: User
Tags dashed line

Object-to-object relationships

1. The relationship between objects and objects

Depend on

Association

Combination

It is often discussed that object-to-object relationships provide two of the following: cohesion, coupling

Cohesion generally refers to functional directivity

Coupling generally refers to dependency on correlation

2. Reliance:

One of the weakest associations between objects is a temporary association. The code generally refers to the call relationships that are established by local variables, function parameters, and return values for other objects. Dependency is generally one of the following:

A, the parameter type of a method in ClassA is CLASSB, this situation becomes coupling;

b, the parameter type of a method in ClassA is a property of CLASSB, this situation becomes tight coupling;

C, ClassA the implementation of a method to instantiate CLASSB;

D, the type of the return value of a method in ClassA is CLASSB;

If one of these four cases occurs, the two classes are likely to be "dependent" relationships.

Dependency relationship (Dependency):

Is the connection between classes and classes, and dependencies are always one-way. A dependency represents a class that relies on the definition of another class. In the following example, class A relies on class B, C, and D.

@interface A:nsobject

-(b*) GETB: (c *) C:(d *) d {

e *e = [e new];

b *b = [B new];

}

@end

3. Association:

A reference relationship between objects, such as the relationship between a customer class and an Order class. This relationship is typically expressed using the properties of the class. The two classes involved in an association relationship are at the same level, whereas in an aggregation relationship, two classes are at an unequal level, one representing the whole and one representing the part. (Correlation and aggregation are only syntactically distinguishable and require a logical relationship between the classes involved.) Association is a structural relationship that indicates that the object of one thing is associated with the object of another thing. Given a connection two types of associations, you can navigate from one class object to another object of the class. Association classes are connected by a dashed line

Associations can have a direction, that is, navigation. In general, when not explained, the navigation is bidirectional, do not need to mark the online arrows. In most cases the navigation is unidirectional and can be represented by an arrow. Associations are typically represented in code as attributes (member variables), for example, Class A and B are associated in the following example

@interface A {

b b;

}

@end

If B is also associated with a, then they are two-way associations.

@interfae B {

A;

}

@end

The difference between affinity and dependency:

From the degree of strength of the relationship between the classes, the association represents a strong relationship between the classes, and the dependence represents a weaker relationship between the classes;

From the time perspective of the relationship between classes,

Associations represent "persistent" relationships between classes, which generally represent an important business relationship that needs to be saved, or that needs to be persisted, or saved to a database. For example, in the student management system of the student class and class, a student object belongs to which class is an important business relationship, if this relationship is not saved, the system can not be managed.

In addition, dependency represents a "temporary, transient" relationship between classes, which do not need to be saved, such as the Student class and Stueditscreen (Student login Interface) class is a kind of dependency, Stueditscreen class depends on student class, Relies on information from the student object to display editing student information.

4, aggregation (a kind of association relationship): represents the relationship of Has-a, is an unstable containment relationship. Aggregation classes do not have to be responsible for the aggregated classes. The aggregation relationship is expressed using the collection property, and when object A is added to object B as part of object B, object B and object A are clustered. Aggregation is one kind of correlation relation, it is strong association relation, emphasizing the relation between whole and part. As with associative relationships, aggregation relationships are also implemented by instance variables. There is no way to differentiate the relationship and the aggregation relationship, and the difference between the two can be better differentiated by semantics. Aggregation relationship (Aggregation): is a kind of association relationship, is a strong association relationship. Aggregation is the relationship between the whole and the individual. such as the automobile and the class, the relationship between the tire category is the relationship between the whole and the individual.

As with associative relationships, aggregation relationships are also implemented by instance variables. Hollow diamond representation.

Differences in association and aggregation:

(1) The two objects involved in the association relationship are at the same level. For example, people and bicycles are an association, not an aggregation, because people are not made up of bicycles.

The two objects involved in the aggregation relationship are at an unequal level, one representing the whole, and the other representing the part. For example, the computer and its monitor, keyboard, motherboard and memory are aggregation relationships, because the motherboard is a part of the computer.

(2) for two objects with aggregation relationships, especially strong aggregation relationships, the overall object restricts the life cycle of its constituent objects. The object of a partial class cannot exist alone, its life cycle depends on the life cycle of the object of the whole class, and when the whole disappears, the part disappears. For example, Zhang San's computer is stolen, then all the components of the computer are not there, unless Zhang San in advance to remove some of the computer components (such as hard disk and memory).

5, Combination: Represents the relationship of Contains-a, is a strong inclusion relationship. The combined class is responsible for the life cycle of the grouped classes. The aggregation relationship is also expressed by using set properties, which is one of the relational relationships and is stronger than the aggregation relationship. It requires that the object represented in an ordinary aggregation relationship be responsible for the life cycle of the object representing the part, and that the synthetic relationship cannot be shared.

The object representing the whole needs to be responsible for keeping the object alive, and in some cases is responsible for the annihilation of the object representing the part. An object representing a whole can pass an object representing a part to another object, which is responsible for the life cycle of the object. In other words, the object representing a part can only be composited with one object at a time, and the latter is responsible for its life cycle, (the obvious difference between aggregation and composition is that if Class B contains pointers to class A objects, that is aggregation (life cycle is not the same), if Class B contains a variable of Class A property, Then it must be a combination (the life cycle has to be the same)), a solid diamond

6, Inheritance: Represents the relationship between the Is-a, is the most coupling between the object of a relationship, the subclass inherits all the details of the parent class. Direct use of inheritance expressions in languages. The representation of inheritance in a class diagram is to pull a closed, single-point (or triangle) solid line from the subclass to the base class

From the frequency of use, the association (including aggregation and composition) relationships are the most widely used, followed by dependencies and inheritance

The relationship between design classes is the principle to follow:

First, determine if the class is an "association" relationship,

If it is not a "dependency",

In general, if it's not an association, it's dependency.




An example:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

O-c Correlation-05: Object-to-object relationships

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.