Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Refactoring/DealingWithGeneralization

From ZeroWiki
Revision as of 00:29, 27 March 2026 by Maintenance script (talk | contribs) (Repair batch-0003 pages from live compare)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Chapter 11 Dealing With Generalization

Pull Up Field

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

http://zeropage.org/~reset/zb/data/PullUpField.gif

Pull Up Method

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

http://zeropage.org/~reset/zb/data/PullUpMethod.gif

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;
}

Push Down Method

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

http://zeropage.org/~reset/zb/data/PushDownMethod.gif

Push Down Field

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

http://zeropage.org/~reset/zb/data/PushDownField.gif

Extract Subclass

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

http://zeropage.org/~reset/zb/data/ExtractSubclass.gif

Extract Superclass

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

http://zeropage.org/~reset/zb/data/ExtractSuperClass.gif

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.

http://zeropage.org/~reset/zb/data/ExtractInterface.gif

Collapse Hierarchy

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

http://zeropage.org/~reset/zb/data/CollapseHierarchy.gif

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.

http://zeropage.org/~reset/zb/data/FormTemplateMethod.gif

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.

http://zeropage.org/~reset/zb/data/ReplaceInheritanceWithDelegation.gif

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.

http://zeropage.org/~reset/zb/data/ReplaceDelegationWithInheritance.gif


Refactoring