Pull Up refactoring
Encyclopedia
In software engineering
Software engineering
Software Engineering is the application of a systematic, disciplined, quantifiable approach to the development, operation, and maintenance of software, and the study of these approaches; that is, the application of engineering to software...

, Pull Up refactoring
Refactoring
Code refactoring is "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior", undertaken in order to improve some of the nonfunctional attributes of the software....

 involves moving a member of a class, such as a method
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...

, from a Subclass into a Superclass.

How and when can this refactoring be applied?

This refactoring is especially useful when subclasses of a certain class share some functionality but each implements it separately. Moving the shared functionality (using Extract Method if need be, or just moving the method) to the superclass will keep the functionality of the subclasses as it is, and give us less code duplication and better code readability.Please note that this refactoring shouldn't be used if only relatively few subclasses share a functionality, since this will add (probably) unintended functionality to other (most of the other ) subclasses.
Suppose we have a class A and it's subclasses B, C, D, E, F and G. If B and C share a method (for example), then a better solution in this case is creating a class that will have the B and C's shared method (let's call it Z), and have B and C use the method from that class (Z in this example). That process is called Extract Class
Extract class
In Software engineering, the Extract class refactoring is applied when a class becomes overweight with too many methods and its purpose becomes unclear. It involves creating a new class and moving methods and/or data to the new class.-Further reading:...

.
Another option would be making Z extend A and then making B and C extend Z.

Examples

Compare the following Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

classes before and after the Pull Up refactoring is applied on myMethod2:

public class Superclass{

void myMethod{
//do something
}
}

public class Subclass extends Superclass{

void myMethod {
//do something
}
void myMethod2 {
//do something else
}
}


After the Pull Up refactoring is applied:

public class Superclass{

void myMethod{
//do something
}
void myMethod2 {
//do something else
}
}

public class Subclass extends Superclass{

void myMethod {
//do something
}
}
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK