Law 1: Priority use (object) combination, not (class) inheritance
[Favor Composition Over Inheritance]
combination
N (object) combination is a multiplexing method for obtaining new functions by creating an object that combines other objects.
n Entrust the function to an object of the combination to obtain new features.
n Sometimes also known as "aggregation" or "inclination", although some authors give special meaning to these terms
n, for example:
F Aggregate: An object has another object or is responsible for another object (ie, an object contains another object or part of another object), and the aggregate object and its owner have the same lifecycle. (Translator Note: The so-called "same life is dead" relationship, see the introduction part of the DESIGN PATTERNS: Elements of Reusable Object-Oriented Software.)
F Inclusion: A special type of combination. For other objects, the contained objects in the container are invisible, and other objects can only access the contained objects through the container object. (COAD)
n The included can be implemented in two ways:
F According to reference (by reference)
F according to value (by value)
N C allows you to be included based on values or references.
n But in Java, everything is referenced by the object!
Advantages and disadvantages of combination
n Advantages:
F containers can only be accessed by interfaces that are included.
F "Black Box" multiplexed because it is not visible to the internal details of the object.
F is good.
F Implementation is relatively small. (Translator Note: It is relatively less dependence between the object and the container object)
F Every class is only focused on one task.
F By getting a reference to other objects that have the same type, (object) can be dynamically defined during operation.
n Diffility:
F thus leads to too many objects in the system.
F In order to use multiple different objects as a Composition Block, you must be carefully defined.
inherit
N (class) inheritance is a multiplexing method for obtaining new functions by extending an existing object.
n Ultrafial (Class) can explicitly capture those common attributes and methods.
n Special classes (subclasses) are achieved by additional properties and methods.
Advantages and disadvantages of inheritance
n Advantages:
F is easy to perform new implementation because most of them can inherit.
F is easy to modify or extend those implementations.
n Diffility:
F destroyed encapsulation because this exposes the implementation details of the parent class to the child class.
F "White Box" is reused because the internal details of the parent class are often visible for subclasses.
F When the parent class is achieved, the subclass has to change. F The implementation of inheritance from the parent class will not change during operation.
COAD rules
For all the criteria of the following, they can use inheritance:
The N subclass expression "is a special type of ...", rather than "is a role played by ...".
An instance of the N subclass will never need to transmute as an object of other classes.
N subclasses are expanded for its parent class, rather than rewriting or abolishing (NULLIFY).
The N subclass does not extends only those functions that are only a tool class (Utility Class).
N For a class in which the actual problem domain is, the subclass is specifically referred to as a Role, transaction or device.
Inheritance / Combination Example 1
n "is a special type of ..." instead of "is a role played by ...
F failed. Passengers are a role that people play. Agent is also.
n Never need to transform
F failed. Over time, a subclass example of a Person may turn from Passenger into an agent, and then go to the Agent Passenger.
N expand, not retroactive and abolishment
F pass.
N Don't expand a tool class
F pass.
n In the problem domain, specifically, a role, transaction or equipment
F failed. Person is not a role, trading or device.
Inheritance is not applicable!
Use a combination to save!
Inheritance / Combination Example 2
n "is a special type of ..." instead of "is a role played by ...
F pass. Passengers and agents are all roles of special types of people.
n Never need to transform
F pass. A Passenger object will remain unchanged; Agent object is also.
N expand, not retroactive and abolishment
F pass.
N Don't expand a tool class
F pass.
n In the problem domain, specifically, a role, transaction or equipment
F pass. Personrole is a type of role.
Inherit is available here!
Inheritance / Combination Example 3
n "is a special type of ..." instead of "is a role played by ...
F pass. Book and purchase are a special type of transaction.
n Never need to transform
F pass. A RESERVATION object will remain unchanged; the Purchase object is also.
N expand, not retroactive and abolishment
F pass.
N Don't expand a tool class
F pass.
n In the problem domain, specifically, a role, transaction or equipment
F pass. It is a transaction.
Inherit is available here!
Inheritance / Combination Example 4
n "is a special type of ..." instead of "is a role played by ...
F failed. Book is not a special type of Observable.
n Never need to transform
F pass. A RESERVATION object will remain unchanged.
n extension, not retro and abolish F.
N Don't expand a tool class
F failed. Observable is a tool class.
n In the problem domain, specifically, a role, transaction or equipment
F does not apply. Observable is a tool class, not a class of a problem domain. .
Inheritance is not applicable!
Summary of inheritance / combination
N combined with inheritance is an important reuse method
N in the early days of OO development, inheritance is excessively used
N Over time, we find that priority use combination can achieve reuse and simplicity better design.
n of course, can be inherited to expand the set of composable classes.
n So the combination and inheritance can work together
n But our basic law is:
Priority use of object combination, not (class) inheritance [Favor Composition over Inheritance]