Contents
- 1. Chapter 11 Dealing With Generalization
- 1.1. Pull Up Field
- 1.2. Pull Up Method
- 1.3. Pull Up Constructor Body
- 1.4. Push Down Method
- 1.5. Push Down Field
- 1.6. Extract Subclass
- 1.7. Extract Superclass
- 1.8. Extract Interface
- 1.9. Collapse Hierarchy
- 1.10. Form Template Method
- 1.11. Replace Inheritance with Delegation
- 1.12. Replace Delegation with Inheritance
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.
~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; }
1.4. Push Down Method ¶
- Behavior on a superclass is relevant only for some of its subclasses.
Move it 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.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.