Refactoring/Dealing With Generalization

1. Chapter 11 Dealing With Generalization

1.1. Pull Up Field

  • Two subclasses have the same field.
    Move the field to the superclass.

1.2. Pull Up Method

  • You have methods with identical results on subclasses.
    Move them to the superclass

1.3. Pull Up Constructor Body

  • You have constructors on subclasses with mostly identical bodies.
    Create a superclass constructor; class this from the subclass methods.

class Manager extends Employee...
	public Manager (String name, String id, int grade) {
		_name = name;
		_id = id;
		_grade = grade;

public Manager (String name, String id, int grade) {
	super (name, id);
	_grade = grade;

1.4. Push Down Method

  • Behavior on a superclass is relevant only for some of its subclasses.
    Move it to those subclasses.

1.5. Push Down Field

  • A field is used only by some subclasses.
    Move the field to those subclasses.

1.6. Extract Subclass

  • A class has features that are used only in some instances.
    Create a subclass for that subset of features.

1.7. Extract Superclass

  • You have two classes with similar features.
    Create a superclass and move the common features to the superclass.

1.8. Extract Interface

  • Several clients use the same subset of a class's interface, or two classes have part of their interfaces in common.
    Extract the subset into an interface.

1.9. Collapse Hierarchy

  • A superclass and subclass are not very different.
    Merge them together.

1.10. Form Template Method

  • You have two methods in subclasses that perform similar steps in the same order, yet the steps are different.
    Get the steps into methods with the same signature, so that the original methods become the same. Then you call pull them up.

1.11. Replace Inheritance with Delegation

  • A subclass uses only part of a superclasses interface or does not want to inherit data.
    Create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing.

1.12. Replace Delegation with Inheritance

  • You're using delegation and are ofter writing many simple delegations for the entire interface.
    Make the delegating class a subclass of the delegate.

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0931 sec