Object-Oriented Design law (1) -- give priority to (object) combination, rather than (class) Inheritance

Source: Internet
Author: User
[Favor composition over inheritance]

Combination

1. (object) combination is a method of reusing new functions by creating an object that combines other objects.

2. delegate the function to a combined object to obtain the new function.

3. Sometimes it is called "aggregation" or "inment", although some authors give special meanings to these terms

4. For example:

A. Aggregation: an object owns another object or is responsible for another object (that is, an object contains another object or a part of another object), and the aggregation object has the same lifecycle as its owner. (Note: the so-called "co-occurrence, co-occurrence, and Death" relationship can be found in the gof Design

Patterns: Introduction to elements of reusable object-oriented software .)

B. Inclusion: a special type of combination. For other objects, the contained objects in the container are invisible. Other objects can only access the contained objects through container objects. (Coad)

5. The following two methods can be used:

A. By reference)

B. By Value)

6. C ++ allows inclusion based on values or references.

7. But in Java, everything is an object reference!

Advantages and disadvantages of Combination

1. Advantages:

A. The Container class can only access the contained object through the interface.

B. Reuse the "black box" because the internal details of the contained object are invisible to the outside world.

C. Good installation.

D. The implementation dependency is relatively small. (Note: The dependency between the contained object and the container object is relatively small)

E. Each class only focuses on one task.

F. By getting reference to other objects of the same type, you can dynamically define (object) Combinations during running.

2. Disadvantages:

A. This causes too many objects in the system.

B. In order to use multiple different objects as composition blocks, the interface must be carefully defined.

Inheritance

1. (class) inheritance is a method of reusing new functions by extending the implementation of an existing object.

2. Generalized classes (superclasses) can explicitly capture public attributes and methods.

3. Special classes (subclasses) are extended by adding attributes and methods.

Advantages and disadvantages of Inheritance

1. Advantages:

A. It is easy to implement new implementations because most of them can be inherited.

B. It is easy to modify or extend reusable implementations.

2. Disadvantages:

A. This destroys encapsulation because it exposes the implementation details of the parent class to the Child class.

B. "white box" reuse, because the internal details of the parent class are usually visible to the Child class.

C. when the implementation of the parent class is changed, the subclass has to be changed accordingly.

D. The implementation inherited from the parent class cannot be changed during running.

Coad rules

Inheritance can be used only when all of the following criteria are met:

A. Subclass expresses "is... Is a special type... Role ".

An instance of subclass B never needs to be converted to an object of another class.

C. Subclass is to expand the responsibility of its parent class, rather than rewrite or abolish (nullify ).

D. The subclass does not extend those functions that act only as a utility class.

E. For a class located in the actual problem domain, its subclass refers to a role, transaction, or device ).

Inheritance/combination Example 1

1. "is... Is a special type... Role"

--> Failed. A passenger is a role played by a person. The proxy is also.

2. Never need to convert

--> Failed. With the development of time, a person sub-class instance may change from passenger to agent, and then to Agent passenger.

3. scale, not rewrite or abolish

--> Pass.

4. Do not extend a tool class

--> Pass.

5. In the problem domain, a role, transaction, or device is specified.

--> Failed. Person is not a role, transaction, or device.

Inheritance does not apply here!

Use combinations to save!

Inheritance/combination Example 2

1. "is... Is a special type... Role"

--> Pass. Passengers and agents are roles played by special types of people.

2. Never need to convert

--> Pass. A passp object will remain unchanged, and the agent object will also remain unchanged.

3. scale, not rewrite or abolish

--> Pass.

4. Do not extend a tool class

--> Pass.

5. In the problem domain, a role, transaction, or device is specified.

--> Pass. Personrole is a type of role.

Inheritance applies here!

Inheritance/combination Example 3

1. "is... Is a special type... Role"

--> Pass. Booking and purchasing are both special types of transactions.

2. Never need to convert

--> Pass. A reservation object will remain unchanged; the purchase object will also be.

3. scale, not rewrite or abolish

--> Pass.

4. Do not extend a tool class

--> Pass.

5. In the problem domain, a role, transaction, or device is specified.

--> Pass. Is a transaction.

Inheritance applies here!

Inheritance/combination Example 4

1. "is... Is a special type... Role"

--> Failed. Booking is not a special type of observable.

2. Never need to convert

--> Pass. A reservation object will remain unchanged.

3. scale, not rewrite or abolish

--> Pass.

4. Do not extend a tool class

--> Failed. Observable is a tool class.

5. In the problem domain, a role, transaction, or device is specified.

--> Not applicable. Observable is a tool class, not a class of the problem domain ..

Inheritance does not apply here!

Inheritance/combination Summary

1. Combination and inheritance are important reuse methods.

2. inheritance was overused in the early stage of OO development.

3. With the development of time, we found that preference combinations can achieve better reusability and simplicity design.

4. Of course, the set of composable classes can be expanded through inheritance ).

5. Therefore, combination and inheritance can work together.

6. But our basic law is:

Object combination is preferred, rather than (class) Inheritance

From: http://doc.linuxpk.com/type212.html

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.