More actions
imported>Unknown No edit summary |
(Repair MoniWiki formatting after migration) |
||
| Line 2: | Line 2: | ||
= Chapter 11 Dealing With Generalization = | = Chapter 11 Dealing With Generalization = | ||
== Pull Up Field == | == Pull Up Field == | ||
* Two subclasses have the same field.''Move the field to the superclass.'' | * Two subclasses have the same field.<br />''Move the field to the superclass.'' | ||
http://zeropage.org/~reset/zb/data/PullUpField.gif | http://zeropage.org/~reset/zb/data/PullUpField.gif | ||
== Pull Up Method == | == Pull Up Method == | ||
* You have methods with identical results on subclasses.''Move them to the superclass'' | * You have methods with identical results on subclasses.<br />''Move them to the superclass'' | ||
http://zeropage.org/~reset/zb/data/PullUpMethod.gif | http://zeropage.org/~reset/zb/data/PullUpMethod.gif | ||
== Pull Up Constructor Body == | == Pull Up Constructor Body == | ||
* You have constructors on subclasses with mostly identical bodies.''Create a superclass constructor; class this from the subclass methods.'' | * You have constructors on subclasses with mostly identical bodies.<br />''Create a superclass constructor; class this from the subclass methods.'' | ||
class Manager extends Employee... | class Manager extends Employee... | ||
public Manager (String name, String id, int grade) { | public Manager (String name, String id, int grade) { | ||
| Line 22: | Line 22: | ||
== Push Down Method == | == Push Down Method == | ||
* Behavior on a superclass is relevant only for some of its subclasses.''Move it to those subclasses.'' | * 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 | http://zeropage.org/~reset/zb/data/PushDownMethod.gif | ||
== Push Down Field == | == Push Down Field == | ||
* A field is used only by some subclasses.''Move the field to those subclasses.'' | * A field is used only by some subclasses.<br />''Move the field to those subclasses.'' | ||
http://zeropage.org/~reset/zb/data/PushDownField.gif | http://zeropage.org/~reset/zb/data/PushDownField.gif | ||
== Extract Subclass == | == Extract Subclass == | ||
* A class has features that are used only in some instances.''Create a subclass for that subset of features.'' | * 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 | http://zeropage.org/~reset/zb/data/ExtractSubclass.gif | ||
== Extract Superclass == | == Extract Superclass == | ||
* You have two classes with similar features.''Create a superclass and move the common features to the 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 | http://zeropage.org/~reset/zb/data/ExtractSuperClass.gif | ||
== Extract Interface == | == 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.'' | * 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 | http://zeropage.org/~reset/zb/data/ExtractInterface.gif | ||
== Collapse Hierarchy == | == Collapse Hierarchy == | ||
* A superclass and subclass are not very different.''Merge them together.'' | * A superclass and subclass are not very different.<br />''Merge them together.'' | ||
http://zeropage.org/~reset/zb/data/CollapseHierarchy.gif | http://zeropage.org/~reset/zb/data/CollapseHierarchy.gif | ||
== Form Template Method == | == 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.'' | * 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 | http://zeropage.org/~reset/zb/data/FormTemplateMethod.gif | ||
== Replace Inheritance with Delegation == | == 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.'' | * 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 | http://zeropage.org/~reset/zb/data/ReplaceInheritanceWithDelegation.gif | ||
== Replace Delegation with Inheritance == | == 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.'' | * 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 | http://zeropage.org/~reset/zb/data/ReplaceDelegationWithInheritance.gif | ||
---- | ---- | ||
[[Refactoring]] | [[Refactoring]] | ||
Revision as of 14:01, 26 March 2026
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