Class-To-class relationships, expressed in C # and JavaScript

Source: Internet
Author: User


Objective

There are six types of relationships between classes, namely:

    1. Association Relationship (Association)
    2. Generalization relationship (generalization)
    3. Dependency (Dependency)
    4. Aggregation (Aggregation)
    5. Combination (Composite)
Relationships between classes and classes

Association (Association)

Two relatively independent objects, when an instance of an object has a fixed relationship to an instance of another object, there is an association between the two objects, and the association embodies a strong association, with four Kinds of association forms: one-way association, two-way association, Self-Association and Multidimensional Association.

One-Way Association
Scene: Orders and items, orders contain goods, but the goods do not know the existence of the order.
UML Class Diagrams:

The code reflects:

C#

public class Order    {publicly        list<product> products;        Public Order ()        {Products            = new list<product> ();        }        public void Addproduct (product product)        {products            . ADD (product);        }        public void Print ()        {            foreach (var product)            {                Console.WriteLine (product). Name);    }}} public class Product    {public        string Name {get; set;}    }

Javascript

   var Order = function () {            var products = [];            this.addproduct = function (product) {                Products.push (product);            };            This. Print = function () {for                (var i = 0; i < products.length; i++) {                    console.log (products[i). Name);}}        ;        var Product = function (name) {this            . name = name;        }

Bidirectional correlation
Scenario: Orders and customers, orders belong to customers, customers have some orders
UML Class Diagrams:

The code reflects:

C#

public class Order    {public        User Getuserbyorderid (string orderId)        {            return new User ();        }    }
public class User    {public        list<order> getorders ()        {            return new list<order> ();        }    }

Javascript

        var User = function (name) {this            . name = name;            This.getorder = function () {                console.log ("GetOrder");};        }        ; var Order = function () {            This.getuserbyorderid = function (id) {                console.log ("Getuserbyorderid:" + ID);            };        }

Self-correlation
Association between the same object
UML Class Diagrams:

Multidimensional Associations
There is a connection between multiple objects
Scenario: The company hires employees, and the company needs character pay to employees

Dependency (Dependency)
Class A must reference Class B to complete a function, and a and B have dependencies, and the dependency is a weakly associative relationship.
Scene: Driver and car
UML Class Diagrams:

The code reflects:

  public class Driver    {public        void drive (car car)        {            car. Run ();        }    }    public class Car    {public        void run ()        {            Console.WriteLine ("Running Up");}    }

Javascript

        var Driver = function () {            This.dirve = function (car) {                if (car instanceof car) {                    car.run ();                } else {
   
    throw new Error ("parameter exception, must be car type");}}        ;        var Car = function () {            this.run = function () {                Console.log ("ran up");}        }
   

Generalization (generalization)
Generalization is the inheritance relationship between classes and classes, and the implementation of classes and interfaces.
Scene: A dog is an animal; a bird can fly.
UML Class Diagrams:

The code reflects:

C#

   Public abstract class Animal    {    } public    class Dog    {    }

Javascript

        function Animal () {}        function Dog () {}        Dog.prototype = new Animal ();        Dog.prototype.constructor = Dog;        var dog = new Dog ();        Console.log (dog.constructor);        Console.log (dog instanceof dog);

Aggregation (Aggregation)
When object A is added to object B, called part of Object B, there is an aggregation relationship between objects A and B. The aggregation relation is one kind of association relation, is a strong association relation, emphasizes the relation between the whole and the part.
Scene: Wild Goose and wild goose flock
UML Class Diagrams:

Code embodies

C#

public class Goosegroup    {public        Goose Goose;        Public Goosegroup (Goose g)        {            Goose = g;        }    }    public class Goose    {    }

Javascript

var Goose = function () {        };        var goosegroup = function (goose) {            var _goose = goose;            Console.log (_goose);        };

combination (composition)
Object A contains object B, and object B has no meaning to leave object A, and is a stronger association relationship.
Scene: Wild Goose and Wings
UML Class Diagrams:

Code embodies

C#

  public class Goose    {public        wing wing;        Public Goose ()        {            wing = new Wing ();        }    }    public class Wing    {    }

Javascript

    var Goose = function () {            var wing = new Wing ();        };        var Wing = function () {};

Difference

Correlation and Dependency differences:

    • From the point of view of the code, dependency is the parameter or return value of the method, or the variable form of the method, and the association is embodied as a property of a class, and the association is a more fixed, more persistent relationship than dependency.
    • From the life cycle, the dependency is generated as the method of the class is called and ends with the end of the method, and the association is generated when the class is instantiated, when the class is destroyed, and the associated life cycle is longer than the dependency.

Aggregation and Composition differences:

    • Unlike constructors, the constructor arguments of an aggregate class are instances of another class, and the constructors of a composite class contain the instantiation of another class.
    • The encapsulation of information is different, in the aggregation relationship, the Goosegroup class and the Goose class are independent, the client can access it; In a combined relationship, the client can only access the Goose class, and the wing class cannot access it because it is encapsulated in the Goose class.

Class-To-class relationships, expressed in C # and JavaScript

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.