Relationships of classes in design patterns

Source: Internet
Author: User

In Java and other object-oriented design patterns, there are 6 main types of relationships between classes and classes: Dependency, association, aggregation, composition, inheritance, implementation . Their coupling degree is increased in turn.


Dependencies are defined as: for two relatively independent objects, when one object is responsible for constructing an instance of another object, or a service that relies on another object, the two objects are primarily dependent. The definition is obscure, but the performance in Java is fairly straightforward: class A uses class b, where class b is the class A a method parameter, a local variable in a method, or a static method call . Class in the legend above: The People class relies on the book class and the Food class, and the book class and the food class appear in the People class as parameter forms of the methods in the class.

Code Sample:

public class people{

Book as the formal parameter of the Read method

public void Read (book book) {

System.out.println ("Read the book is" +book.getname ());

}

}


For two relatively independent objects, when an instance of one object has a fixed correspondence with some specific instance of another object, the two objects are associated. Association relationships are divided into one-way and two-way associations. In Java, a one-way association behaves like this: Class a uses class b, where class b is the class a The member variable. The bidirectional association is represented by the use of class B as a member variable in class a , and the use of Class A in class B as a member variable.

Code Sample:

public class son{

A class that is a member variable in an association relationship is typically assigned a value in a class

Father Father = new Father ();

public void Getgift () {

System.out.println ("from" +father.getname () + "Get Gift");

}

}

public class father{

Son son = new son ();

public void Givegift () {

System.out.println ("Give" +son.getname () + "gift");

}

}


The aggregation relation is one kind of correlation relation, the coupling degree is stronger than the association, their code performance is the same, only has the semantic difference: The association Relation object is mutually independent, but the aggregation relation object has the containment relation, between them is " Overall - individual " of mutual relations.

Code Sample:

public class people{

&nbs p;   car car;

    House House;

A class that is a member variable in a    //aggregation relationship is typically assigned a value using the Set method

     public void Setcar (car car) {

        This.car = Car;

   }

    public void Sethouse (House house) {

        This.house = House;

   }

 

    public void driver () {

        System.out.println (" Model of the car: "+car.gettype ());

   }

    public void sleep () {

        System.out.println (" I slept in the house: "+house.getaddress ());

   }

}

 


In the example, people and soul, the body is a combination of relations, when the life cycle begins, must have both the soul and the body, when the end of the human life cycle, the soul of the body with its demise, whether it is the soul or the body, can not exist alone, they must be a part of human existence.

Sample code:

public class people{

Soul Soul;

Body Body;

member variables in a composite relationship are typically assigned in a constructor method

Public people (Soul Soul, body body) {

This.soul = Soul;

This.body = body;

}

public void Study () {

System.out.println ("Learn to use the Soul" +soul.getname ());

}

public void Eat () {

System.out.println ("Eat with Body:" +body.getname ());

}

}



inheritance represents a parent-child relationship between a class and a class (or interface and interface). in Java, the inheritance relationship is represented by the keyword extends. In the UML legend, the inheritance relationship is represented by a solid + hollow arrow, and the arrow points to the parent class.


represents a method for a class to implement one or more interfaces . The interface defines the set of operations that are implemented by the implementation class to accomplish the specific operation of the interface. In Java, the implements representation is used. In the UML legend, the implementation relationship is indicated by a dashed + hollow arrow, and the arrow points to the interface.

Inheritance in Java uses the extends keyword, which implements the Implements keyword.

Relationships of classes in design patterns

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.