[[TableOfContents]] = Chapter 11 Dealing With Generalization = == Pull Up Field == * Two subclasses have the same field.[[BR]]''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.[[BR]]''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.[[BR]]''Create a superclass constructor; class this from the subclass methods.'' {{{~cpp class Manager extends Employee... public Manager (String name, String id, int grade) { _name = name; _id = id; _grade = grade; } }}} {{{~cpp 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.[[BR]]''Move it to those subclasses.'' http://zeropage.org/~reset/zb/data/PushDownMethod.gif == Push Down Field == * A field is used only by some subclasses.[[BR]]''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.[[BR]]''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.[[BR]]''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.[[BR]]''Extract the subset into an interface.'' http://zeropage.org/~reset/zb/data/ExtractInterface.gif == Collapse Hierarchy == * A superclass and subclass are not very different.[[BR]]''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.[[BR]]''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.[[BR]]''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.[[BR]]''Make the delegating class a subclass of the delegate.'' http://zeropage.org/~reset/zb/data/ReplaceDelegationWithInheritance.gif ---- ["Refactoring"]