You get a bonus - 1 coin for daily activity. Now you have 1 coin

Refactoring techniques. Solving generalization problems

Lecture



Generalization generates its own group of refactorings, mainly related to moving functionality through the class inheritance hierarchy, creating new classes and interfaces, and also replacing inheritance with delegation and vice versa.

Field rise

Problem: Two classes have the same field.

Solution: Move the field to the superclass, removing it from the subclasses.

Lifting method

Problem: Subclasses have methods that do a similar job.

Solution: In this case, you need to make the methods identical, and then move them to the superclass.

Body builder lift

Problem: Subclasses have constructors with mostly the same code.

Solution: Create a constructor in the superclass and bring the subclassing code into it. Call the superclass constructor in subclass constructors.

Descent method

Problem: Behavior implemented in the superclass is used by only one or several subclasses.

Solution: Move this behavior to subclasses.

Descent field

Problem: The field is used only in some subclasses.

Solution: Move the field to these subclasses.

Extract subclass

Problem: The class has features that are used only in certain cases.

Solution: Create a subclass and use it in these cases.

Removing the superclass

Problem: You have two classes with common fields and methods.

Solution: Create a common superclass for them and transfer the same fields and methods there.

Interface extraction

Problem: Several clients use the same part of the class interface. Or in two classes of the interface was common.

Solution: Highlight this common part in your own interface.

Collapsing hierarchy

Problem: You have a certain class hierarchy in which the subclass is not much different from the superclass.

Solution: Merge the subclass and superclass together.

Creating a template method

Problem: Subclasses have implemented algorithms that contain similar steps and the same order of execution of these steps.

Solution: Take out the structure of the algorithm and the same steps in the superclass, and in the subclasses leave the implementation of the different steps.

Replacing inheritance with delegation

Problem: You have a subclass that uses only part of the methods of the superclass or does not want to inherit its data.

Solution: Create a field and place the superclass object in it, delegate the execution of the methods to the superclass object, remove inheritance.

Replacing delegation with inheritance

Problem: A class contains many simple delegating methods to all methods of another class.

Solution: Make the class a delegate inheritor, after which the delegating methods will lose meaning.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Refactoring theory

Terms: Refactoring theory