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

Source: Internet
Author: User
Rule 1 : Give priority to (object) combination, rather than (class) Inheritance

[Favor composition over inheritance]

Combination

N(Object) is a combinationCombinationTo obtain the reuse method of new functions.

NDelegates a feature to a combined object to obtain the new feature.

NSometimes it is also called "aggregation "(Aggregation) Or "inclusive "(Containment), Although some authors give special meanings to these terms

NFor example:

FAggregation: 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 aggregate object has the same lifecycle as its owner. (Note: the so-called "co-occurrence, co-occurrence, and Death" relationship can be found inGofOfDesign Patterns: Elements of reusable object-oriented software.)

FInclusion: 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)

 

NIt can be implemented in the following two ways:

FAccording to the reference (By Reference)

FAccording to the value (By Value)

NC++ Allows inclusion based on values or references.

NHoweverJava, Everything is an object reference!

Advantages and disadvantages of Combination

NAdvantages:

FThe container class can only access the contained object through the interface.

FThe black box is reused because the internal details of the contained object are invisible to the outside world.

FGood installation.

FImplementation is less dependent on each other. (Note: The dependency between the contained object and the container object is relatively small)

FEach class only focuses on one task.

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

 

NDisadvantages:

FAs a result, there are too many objects in the system.

FIn order to use multiple different objects as a combination block (Composition blockMust be carefully defined.

Inheritance

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

NGeneric classes can explicitly capture public attributes and methods.

NSpecial classes (Child classes) are extended by adding attributes and methods.

Advantages and disadvantages of Inheritance

NAdvantages:

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

FIt is easy to modify or extend reusable implementations.

NDisadvantages:

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

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

FWhen the implementation of the parent class is changed, the Child class also has to be changed.

FThe 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:

NSubclass expresses "is...Is...".

NA subclass instance never needs to be converted (Transmute) Is an object of other classes.

NSubclass is the responsibility for its parent class (Responsibility), Instead of rewriting or abolishing (Nullify).

NSubclass does not (Utility Class.

NFor an actual problem domain (Problem domain), Its subclass refers to a role (Role), Transaction (Transaction) Or device (Device).

Inheritance / Combination example 1

N"Is...Is...Roles"

FFailed.A passenger is a role played by a person. The proxy is also.

NNever need to convert

FFailed.With the development of timePersonThe subclass instance mayPassengerConvertAgent, And thenAgent passenger.

NExtension instead of rewriting and abolition

FPass.

NDo not extend a tool class

FPass.

NA role, transaction, or device in a problem domain.

FFailed.PersonNot a role, transaction, or device.

Inheritance does not apply here!

 

Use combinations to save!

Inheritance / Combination example 2

N"Is...Is...Roles"

FPass.Passengers and agents are roles played by special types of people.

NNever need to convert

FPass.OnePassengerThe object will remain unchanged;AgentObject.

NExtension instead of rewriting and abolition

FPass.

NDo not extend a tool class

FPass.

NA role, transaction, or device in a problem domain.

FPass.PersonroleIs a type of role.

Inheritance applies here!

Inheritance / Combination example 3

N"Is...Is...Roles"

FPass.Booking and purchasing are both special types of transactions.

NNever need to convert

FPass.OneReservationThe object will remain unchanged;PurchaseObject.

NExtension instead of rewriting and abolition

FPass.

NDo not extend a tool class

FPass.

NA role, transaction, or device in a problem domain.

FPass.Is a transaction.

Inheritance applies here!

Inheritance / Combination example 4

N"Is...Is...Roles"

FFailed.Booking is not a special typeObservable.

NNever need to convert

FPass.OneReservationThe object will remain unchanged.

NExtension instead of rewriting and abolition

FPass.

NDo not extend a tool class

FFailed.ObservableIs a tool class.

NA role, transaction, or device in a problem domain.

FNot applicable.ObservableIs a tool class, not a problem domain class ..

Inheritance does not apply here!

Inheritance / Combination Summary

NCombination and inheritance are important reuse methods.

NInOoInheritance was used excessively in the early stages of development.

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

NOf course, it can be expanded by inheritance (Enlarge) Available combination classes (The set of composable classes).

NTherefore, combination and inheritance can work together.

NBut our basic law is:

Object combination is preferred, rather than (class) Inheritance
[Favor composition over inheritance]

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.