Design Patterns Six relationships: dependency, association, aggregation, composition, inheritance, and implementation _ Design Patterns

Source: Internet
Author: User

Design Patterns Six relationships: dependency, association, aggregation, composition, inheritance, and implementation

In the object-oriented design pattern, there are mainly 6 relationships between classes and classes: Dependency, association, aggregation, composition, inheritance, implementation. Their coupling degree is enhanced in turn

Differences between dependencies, associations, aggregations, and combinations

• A dependency (Dependency) relationship is a join between classes and classes, in which dependencies are generally embodied as local variables, formal parameters of methods, or calls to static methods.

• An association (association) relationship is a join between a class and a class, which enables a class to know the properties and methods of another class. An association can be one-way or bidirectional.

The aggregation (Aggregation) relationship is a kind of association relation, and it is a weak association relation. Aggregation is the relationship between the whole and the individual. Like a band.
(Singer,guitarist,bassist,drumer)

• The combination (composition) relationship is a kind of association relation, and it is a strong association relation. It requires that an object representing the whole in a common aggregation relationship is responsible for representing the life cycle of a part of the object, and that the composite relationship cannot be shared. Like every band member (Soul,body)

The difference between inheritance and implementation

Inheritance: Inheriting an abstract class (and, of course, inheriting common classes)
Implementation: interface is actually a special form of abstract class.

• Inheritance (inherited): Subclasses can inherit abstract classes or generic classes, abstract classes use keyword abstract notation, java and net neutron classes can inherit only one base class, and C + + can inherit multiple base classes.

• Implementation (implement): Subclasses can implement multiple interfaces, the interface using the keyword interface, the interface is not allowed to have ordinary members (that is, constants and variables), net can have properties, methods and events, and Java can only have methods and events, C + + There is no interface interface this keyword, but you can use abstract classes instead of interfaces, which are pure virtual functions.

Note Because properties in NET3.0 can be defined name{get; set;} Without associating with a field, you can define properties in the net interface, and the properties GetName and SetName in Java are actually methods, not real properties, need to be associated with fields, Therefore, you cannot define attributes in the Java interface.

and the attribute in NET2.0 is defined in this way
private string name;
public string Name {' Get {# ' #
Then you cannot define attributes in the interface.

Java inherits the keyword extends, implements the keyword implements, and when the subclass inherits the base class constructor, the keyword super is passed on in the subclass.

":" is used in both inheritance and implementation in net, and when a subclass inherits the base class constructor, the keyword base is passed on in the subclass.

Inheritance in C + + with keyword "::" means

VB6.0 is not inherited, although there is a keyword implements, but the interface is obviously not a substitute for abstract classes.
Note the class module in VB is only encapsulation, such as the Emploeye and department two objects (Emploeye in the DepartmentID, here is the association), that is to provide only the object's fields, properties, methods, events between the call to each other, This is just the primary function of OOP, and inheritance is not the top. Therefore, VB can only be regarded as an object-based programming language, or is not fully object oriented.

Delphi7.0 has inheritance, uses the keyword inherited to express, but also has the interface, uses the keyword interface to express.
Tbass = Class
Procedure Proc (X,y:integer); Virtual
function Fun (X,y:integer): Integer; Dynamic
Var
Variable declare
Begin

End

Tchild = Class (Tbass)
Procedure Proc (X,y:integer); Override
function Fun (X,y:integer): Integer; Override
Var
Variable declare
Begin
inherited;
Code ...
End

Here you see what virtual,dynamic,override, that Delphi has supported polymorphism, this is the symbol of OOP language, that is, fully object-oriented.
The virtual method pays attention to the optimization of the call speed, the dynamic method pays attention to the Code size optimization (occupies the memory space to be small), a focus on the speed, a space saving, usually needs the subclass code to cover the method to use the virtual method, infrequently uses the dynamic method.

Finally, some readers may find out why Delphi and net are similar in some places, in fact, the predecessor of Delphi, the design of Turbo Pascal compiler Anders Hejlsberg is now the famous. NET language chief designer.

Copyright, reprint Please indicate the source of the article Http://blog/csdn.net/cadenzasolo

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.