[Reconstruction learning] 10. Reconstruction of inheritance relationships and 10 reconstruction of inheritance relationships

Source: Internet
Author: User

[Reconstruction learning] 10. Reconstruction of inheritance relationships and 10 reconstruction of inheritance relationships

1. Move fields up

Modification point: the two subclasses have the same field.

Practice: Move this field to the parent class

2. Move functions up

Modification point: some functions have the same effect in each subclass.

Practice: Move the function to the parent class

This is also required in special cases: the subclass function overwrites the parent class, but still does the same work.

In This refactoring, you may encounter a situation where the function you extract calls a function that has a subclass but does not exist in the parent class. Therefore, in the parent class, assign a function that does not exist in the parent class, create a virtual function.

3. Move the constructor ontology up

Modification point: you have some constructors in each subclass, and their ontology is almost identical.

Practice: Create a New constructor in the parent class and call it in the subclass constructor.

4. Move the function down

Modification point: a function in the parent class is only related to some (not all) subclasses.

Practice: Move the function to the related subclass.

5. Move the field down

Modification point: a field in the parent class is used only by some (not all) subclasses.

Practice: Move this field down to the related subclass.

6. Extract sub-classes

Modification point: some features in the class are only used by some (not all) instances.

Practice: create a new class and move the features mentioned above to the subclass.

7. Refine the parent class

Modification point: two classes have similar features

Practice: Create a parent class for the two classes and move the same features to the superclass.

8. Extraction Interface

Modification point: several customers use the same subset of the class interface, or the interfaces of the two classes are partially the same

Practice: extract the same subset to an independent interface

The meaning is very simple. It refers to some business-related functions in a class, or some of the same functions in two classes, extracted as Interface (the English language is not used here because the Interface word in the previous article may be more understood as a class public function)

9. Folding Inheritance System

Modification point: there is no big difference between the parent class and the subclass.

Practice: integrate them into one

10. Create template Functions

Modify point: you have some sub-classes, some of which execute similar operations in the same order, but the details of each operation are different.

Practice: Put these operations into independent functions and keep them with the same signature, so the original functions become the same, and then move the original functions to the parent class.

That is to say, you can see that some functions have different operations, but the combined operation sequence of several functions is the same. Then, extract the operation sequence of these functions into the parent class.

11. Replacing inheritance with Delegation

Modification point: a subclass only uses a part of the parent class interface or data that does not need to be inherited.

Practice: Create a new field in the subclass to save the parent class; adjust the subclass function to delegate the superclass; then remove the inheritance relationship between the two.

Motivation: as demand changes, the inheritance relationships at the beginning may gradually change, and you will find that many operations in the parent class are not applicable to child classes.

Troy: at this time, you can certainly get these methods that do not apply to subclass into another subclass, But if you cannot do so, take the parent class object as an object of the subclass, remove the inheritance relationship, which seems to be called the combination mode in the design mode.

12. Replace the delegate with inheritance (you can see that there are always two reconstruction operations on the contrary)

Modification point: you use the delegate relationship between two classes and often write many very simple delegate functions for the entire interface.

Practice: Let the delegate class inherit the delegate class

Two tips:

1. If you do not use all functions of the delegate class. You may not need to use this method. You can remove the intermediary, directly call the delegate class using the client, or extract a superclass between the delegate class and the delegate class. Of course, you can also extract interfaces.

2. The trustee object is shared by more than one other object, and the trustee object is variable. In this case, you should not use this refactoring, because data cannot be shared. Data sharing is a responsibility of the delegated relationship, and you cannot convert it into an inherited relationship. You can do this if the trustee object is unchangeable.

Related Article

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.