[Small talk design mode] relationship between classes

Source: Internet
Author: User
4. Composition)

Definition:Compared with aggregation, combination is a more coupling Association. A class with a composite relationship represents the association between "Whole-part", and "whole" is responsible for the life cycle of "part". They are co-occurrence and co-occurrence; and the "part" exists independently. If it is a composite relationship, if the whole is damaged, the individual will be damaged, while the aggregated individual may be shared by multiple entities, it may not be damaged as a whole.

Symbol: Solid diamond solid line arrow

1. The compositing relationship is a kind of association relationship, which is stronger than the aggregation relationship.
2. It requires that the object representing the whole in a common aggregation relationship be responsible for representing the lifecycle of some objects.


Code:

Package COM. ex. composition;/***** Project name: UML * Class Name: CPU * class description: A computer computing device * Creator: yinxiangbing * Creation Time: 04:23:33 * @ version 6.0 **/public class CPU {private string code; private string name; Public CPU (string code, string name) {This. code = Code; this. name = Name ;}} package COM. ex. composition;/***** Project name: UML * Class Name: Computer * class description: a development tool computer * created by: yinxiangbing * Creation Time: 01:34:49 * @ version 6.0 **/public class computer {private string code; private string name; private CPU; // The member variables in the composite relationship are usually assigned the Public Computer (string code, string name, CPU) {This. code = Code; this. CPU = CPU ;}}
5. Generalization)

Definition: Inheritance indicates the parent-child relationship between a class and a class (or between an interface and an interface. In Java, the extends keyword indicates the inheritance relationship. Inheritance (special individual is kind of general individual.

Symbol: Solid line representation with hollow arrows

Arrows point to normal individuals


Code:

Package COM. ex. generalization;/***** Project name: UML * Class Name: Animal * class description: Animal * Creator: yinxiangbing * Creation Time: 10:20:32 * @ version 6.0 **/public class animal {public void eat () {}public void sleep () {}} package COM. ex. generalization;/***** Project name: UML * Class Name: Bird * class description: Bird * Creator: yinxiangbing * Creation Time: 10:20:49 * @ version 6.0 **/public class bird extends animal {public void fly (){}}

6. Implementation)

Definition:Indicates the method that a class implements one or more interfaces. The interface defines the set of operations, and the Implementation class completes the specific operations of the interface. Use implements in Java.

Symbol:Represented by dotted lines + hollow arrows

Arrow pointing to interface


Code:

Package COM. ex. implementation;/***** Project name: UML * Class Name: USB * class description: USB interface * Creator: yinxiangbing * Creation Time: 10:23:01 * @ version 6.0 **/public interface USB {// access the computer public void connect (); // disconnect the computer public void disconnect ();} package COM. ex. implementation;/***** Project name: UML * Class Name: Camera * class description: Camera * Creator: yinxiangbing * Creation Time: 10:44:10 * @ version 6.0 **/public class camera implements USB {public void connect () {system. out. println ("normal camera access to computer");} public void disconnect () {system. out. println ("camera disconnected") ;}} package COM. ex. implementation;/***** Project name: UML * Class Name: MP3 * class description: MP3 * Creator: yinxiangbing * Creation Time: 10:44:38 * @ version 6.0 **/public class MP3 implements USB {public void connect () {system. out. println ("MP3 normal computer access");} public void disconnect () {system. out. println ("MP3 disconnected") ;}} package COM. ex. implementation;/***** Project name: UML * Class Name: Computer * class description: Computer * Creator: yinxiangbing * Creation Time: 10:22:03 * @ version 6.0 **/public class computer {public void useusb (USB) {USB. connect (); USB. disconnect ();} public static void main (string [] ARGs) {computer CPU = new computer (); // camera cam = new camera (); CPU. useusb (CAM); // camera MP3 MP3 = new MP3 (); CPU. useusb (mp3 );}}



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.