Implementation of the relationship in the design pattern in the code

Source: Internet
Author: User

Implementation of the relationship in the design pattern in the code

We have all learned UNL, in which class charts have 6 types of relationships: generalization (inheritance), implementation, association, combination, aggregation, and dependency. As there are various relationships between them, I divide them into four categories: dependency, association, inheritance, and implementation.

Organize


From this figure, we can see how I divide it: Aggregation and combination are a special form of association, So we divide it like this. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ICDO0sPH1qq1wKOs1eI2uPa52M + signature + CjxwPiAgxMfDtM/C0ruyvaOsztLDx77Nv7S/tKOsy/Signature + Signature = "http://www.2cto.com/uploadfile/Collfiles/20140811/20140811102137133.png" alt = "\">


1. Dependency

The definition of dependency is very difficult. To put it bluntly, one object depends on the services of another object. The implementation in C # is very simple: A depends on object B. In most cases, object B is the method parameter, method local variable, or static method call of object.

For example:

The growth of plants requires water and sunlight. Sunlight and water are parameters of plant absorption and absorption methods. C # code implementation:

Public class Plant {public void absort (Sunshine sun) {Console. write ("absorbing sunlight");} public void ShutUp (Water water) {Console. write ("moisture absorption ");}}

2. Association

For two relatively independent objects, a fixed ing relationship exists between the instance of one object and the specified instance of another object. Association: Class A uses Class B as the member variable.


For example, the Code implementation is:

Public class Son {Father father = new Father (); public void getGift () {Console. Write ("from" + father. getname () + "get gift ");}}

3. Aggregation

Aggregation is a special association that reflects the relationship between "whole" and "part.

The Coupling Degree of the aggregation relationship is stronger than that of the Association, and their code performance is the same, only in terms of semantics: There is an inclusive relationship between the objects of the aggregation relationship, they are the "Whole-individual" relationship.

Code implementation:

Public class People {Car car; House house; // classes in the aggregate relationship that act as member variables generally use the set method to assign values to public void setCar (Car car) {This. car = car;} public void setHouse (House house) {This. house = house;} public void driver (){
Console. Write ("car model:" + car. getType ();} public void sleep (){
Console. Write ("I am sleeping in the house:" + house. getAddress ());}}


4. Combination

A combination is a special association. The coupling is greater than aggregation, and the code implementation is the same. The difference is that "whole" in a combination cannot be missing "part ".


Code implementation:

Public class Body {Ear ear = new Ear; Eyes eyes = new Eyes; public void listen () {Console. write ("body needs" + ear. getname () + "");} public void sleep () {Console. write ("required during sleep" + ear. getname () + "close ");}}

5. Inheritance

Inheritance can be divided into two types: Inheriting abstract classes and implementing interfaces. The two differences are:

The abstract class indicates what this object is. The interface indicates what the object can do.

First, the interface is a variant of the abstract class, and all the methods in the interface are abstract. Abstract classes are classes that declare the existence of methods rather than implementing them.
Second, the interface can be inherited more than the abstract class.
Third, the interface definition method cannot be implemented, while the abstract class can implement some methods.
Fourth, the basic data type in the interface is static, but the image is not.

6. Implementation

The Coupling Degree and inheritance of the implementation are the same. The implementation is a class to implement one or more interfaces. Interface Definition methods and class implementation methods.

OK. The Inheritance and implementation code is actually very simple. keywords are used, and the keywords in different languages are different.


Summary:

With regard to the design pattern, if you do not know how the relationship in the UML diagram is implemented, then it is completely overhead. The design pattern requires us not only to understand the UML diagram, but also to apply it flexibly, which is the basis of the design pattern.




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.