Design Pattern Learning: Combinatorial reuse principle

Source: Internet
Author: User
Tags extend inheritance

Combinatorial reuse principle Definition:

Use some existing objects within a new object to become part of the new object, and the new object can be reused for the purpose by delegating to those objects

Another argument is : try to use (object) combinations rather than inheriting

Then the combination and inheritance have what characteristics, advantages and disadvantages, why should have such a priority! First, compare the combination and inheritance.

Combination VS. Inherited

Advantages and disadvantages of combinatorial reuse

Combination

Advantages:

The 1 container class can only be accessed through the interface of the containing object.

2 "black box" multiplexing, since the internal details of the contained objects are externally invisible.

3 Good encapsulation.

4 by obtaining references to other objects of the same type, you can dynamically define (object) combinations during run time.

Disadvantages:

1 causes too many objects in the system.

2 to be able to use multiple different objects as composite blocks (Compositionblock), interfaces must be defined carefully.

Inherited

(Class) Inheritance is a reuse method that obtains new functionality by extending the implementation of an existing object. A generalization class (superclass) can explicitly capture those public properties and methods. Special classes (subclasses) are extensions that are implemented through additional properties and methods.

Advantages and disadvantages of inheritance

Advantages:

1 easy to make new implementations, because most of them can be inherited.

2 easy to modify or extend those implementations that are reused.

Disadvantages:

1 destroys encapsulation because it exposes the implementation details of the parent class to subclasses.

2 "white box" reuse because the inner details of the parent class are usually visible to subclasses.

3 when the implementation of the parent class changes, the subclass also has to change.

4 An implementation that inherits from the parent class will not be able to change during run time.

In order to use inheritance correctly, we must understand the principle of the Richter substitution and the Coad law thoroughly. The principle of the Richter substitution was studied before, and the Coad law was presented by Peter Coad, which summed up some of the conditions for using inheritance as a reuse tool. You should use an inheritance relationship only if the following coad conditions are all satisfied:

1 subclasses are a special kind of superclass, rather than a role of superclass, which distinguishes between "has-a" and "is-a". Only the "is-a" relationship conforms to the inheritance relationship, and the "has-a" relationship should be described in combination.

2 There will never be a case where you need to replace a subclass with a subclass of another class. Do not use inheritance if you are not sure whether you will become another subclass in the future.

3 subclasses have the responsibility to extend the superclass, rather than having a permutation (override) or logoff (nullify) superclass. If a subclass needs a lot of substitution for the behavior of the superclass, then the class should not be a subclass of the superclass.

4 inheritance can only be used if it is meaningful in taxonomic terms. Do not inherit from the tool class.

In many cases, the choice of inheritance relationships to describe the relationships between two classes is due to a lack of understanding of the inheritance relationship. The differences between IS-A and Has-a are discussed below.

Is-a and Has-a differences:

Is-a, as the name suggests, is "a", meaning that a class is one of another; Has-a is "one", meaning that a class is part of another class.

The wrong use of inheritance instead of using a combination is to "has-a" as "is-a".

Consider such a situation. Each of us has an identity, such as I am a teacher, you are a student, he is an athlete. So, join in to design our class diagram for such a situation. This may be the case below.

The corresponding code is as follows:

Class person
{public Person
	()
	{
	}
}

class Teacher extends person
{public
	Teacher ()
	{
		System.out.println ("Iam a teacher.");
	}

Class Student extends person
{public
	Student ()
	{
		System.out.println ("Iam a Student.");
	}

class athlete extends
person {Public
	athlete ()
	{
		System.out.println ("Iam a Athlete");
	}

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.