Refactoring


1. Chapter 7 Moving Features Between Objects

1.1. Move Method

A method is, or will be, using or used by more features of another class than the class on which it is defined.

Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.

1.2. Move Field

A field is, or will be, used by another class more than the class on which it is defined.

Create a new field in the target class, and change all its users.

1.3. Extract Class

You have one class doing work that should be done by two.

Create a new class and move the relevant fields and methods from the old class into the new class.

1.4. Inline Class

A class isn't doing very much.

Move all its features into another class and delete it.

1.5. Hide Delegate

A client is calling a delegate class of an object.

Create methods on the server to hide the delegate.

1.6. Remove Middle Man

A class is doing too much simple delegation.

Get the client to call the delegate directly.

1.7. Introduce Foreign Method

A server class you are using needs an additional method, but you can't modify the class.

Create a method in the client class with an instance of the server class as its first argument.
~cpp 
Date newStart = new Date (previousEnd.getYear(), previousEnd.getMonth(), previousEnd.getDate() + 1);

~cpp 
Date newStart = nextDay (previousEnd);

private static Date nextDay (Date arg) {
	return new Date (arg.getYear(), arg.getMonth(), arg.getDate() + 1);
}


1.8. Introduce Local Extension

A server class you are using needs serveral additional methods, but you can't modify the class.

Create a new class that contains these extra methods. Make the extension class a subclass or a wapper of the original.


Retrieved from http://wiki.zeropage.org/wiki.php/Refactoring/MovingFeaturesBetweenObjects
last modified 2021-02-07 05:27:53