Presentation is loading. Please wait.

Presentation is loading. Please wait.

Catalog of Refactoring

Similar presentations


Presentation on theme: "Catalog of Refactoring"— Presentation transcript:

1 Catalog of Refactoring
(7) Dealing with Generalization

2 Catalog Pull Up Field Pull Up Method Push Down Method Push Down Field
Pull Up Constructor Body Replace Constructor with Factory Method Form Template Method Extract Subclass Extract Superclass Extract Interface Collapse Hierarchy Replace Inheritance with Delegation Replace Delegation with Inheritance

3 Pull Up Field Two subclasses have the same field.
Move the field to the superclass. Each refactoring method name is written in blue.

4 Pull Up Method You have methods with identical results on subclasses.
Move them to the superclass.

5 Mechanism Inspect the methods to ensure they are identical.
If the methods look like they do the same thing but are not identical, use algorithm substitution on one of them to make them identical. If the methods have different signatures, change the signatures to the one you want to use in the superclass Create a new method in the superclass, copy the body of one of the methods to it, adjust, and compile If the method calls another method that is present on both subclasses but not the superclass, declare an abstract method on the superclass Delete one subclass method and testing until only the superclass method remains

6 Pull Up Constructor Body
You have constructors on subclasses with mostly identical bodies. Create a superclass constructor; call this from the subclass methods.

7 Mechanics Define a superclass constructor.
Move the common code at the beginning from the subclass to the superclass constructor. Call the superclass constructor as the first step in the subclass constructor If there is any common code later, use Extract Method to factor out common code and use Pull Up Method to pull it up Compile and test in every step

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

9 Example class Employee protected Employee (String name, String id) {
_name = name; _id = id; } public Manager (String name, String id, int grade) { super (name, id); _grade = grade;

10 Another Example class Employee... boolean isPriviliged() {..}
void assignCar() {..} class Manager... public Manager (String name, String id, int grade) { super (name, id); _grade = grade; if (isPriviliged()) assignCar(); //every subclass does this } boolean isPriviliged() { return _grade > 4;

11 Another Example class Employee... void initialize() {
if (isPriviliged()) assignCar(); } class Manager... public Manager (String name, String id, int grade) { super (name, id); _grade = grade; initialize();

12 Push Down Method Behavior on a superclass is relevant only for some of its subclasses. Move it to those subclasses.

13 Mechanics Declare a method in all subclasses and copy the body into each subclass. Remove method from superclass. If it makes sense to access the method through a superclass variable, you don't intend to remove the method from any subclasses, and the superclass is abstract, you can declare the method as abstract, in the superclass Remove the method from each subclass that does not need it. Compile and test each step.

14 Push Down Field A field is used only by some subclasses.
Move the field to those subclasses.

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

16 Mechanics Define a new subclass of the source class.
Provide constructors for the new subclass Find all calls to constructors of the superclass. If they need the subclass, replace with a call to the new constructor. One by one use Push Down Method and Push Down Field to move features onto the subclass Look for any field that designates information now indicated by the hierarchy (usually a boolean or type code). Eliminate it by using Self Encapsulate Field and replacing the getter with polymorphic constant methods. All users of this field should be refactored with Replace Conditional with Polymorphism. Compile and test every step.

17 Example class JobItem ... public JobItem (int unitPrice, int quantity, boolean isLabor, Employee employee) { _unitPrice = unitPrice; _quantity = quantity; _isLabor = isLabor; _employee = employee; } public int getTotalPrice() { return getUnitPrice() * _quantity; public int getUnitPrice(){ return (_isLabor) ? _employee.getRate() : _unitPrice; public int getQuantity(){ return _quantity; public Employee getEmployee() { return _employee; private int _unitPrice; private int _quantity; private Employee _employee; private boolean _isLabor; class Employee... public Employee (int rate) { _rate = rate; public int getRate() { return _rate; private int _rate;

18 Example class JobItem... protected JobItem (int unitPrice, int quantity, boolean isLabor) { _unitPrice = unitPrice; _quantity = quantity; protected boolean isLabor() { return false; } public int getUnitPrice(){ return _unitPrice; class LaborItem extends JobItem public LaborItem (int quantity, Employee employee) { super (0, quantity, true); _employee = employee; return true; return _employee.getRate();

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

20 Example class Employee...
public Employee (String name, String id, int annualCost) { _name = name; _id = id; _annualCost = annualCost; } public int getAnnualCost() { return _annualCost; public String getId(){ return _id; public String getName() { return _name; private String _name; private int _annualCost; private String _id;

21 public class Department...
public Department (String name) { _name = name; } public int getTotalAnnualCost(){ Enumeration e = getStaff(); int result = 0; while (e.hasMoreElements()) { Employee each = (Employee) e.nextElement(); result += each.getAnnualCost(); return result; public int getHeadCount() { return _staff.size(); public Enumeration getStaff() { return _staff.elements(); public void addStaff(Employee arg) { _staff.addElement(arg); public String getName() { return _name; private String _name; private Vector _staff = new Vector();

22 Example class Party... abstract public int getAnnualCost()
protected Party (String name) { _name = name; } private String _name; class Employee extends Party public Employee (String name, String id, int annualCost) { super (name); _id = id; _annualCost = annualCost; class Department extends Party public Department (String name) { public int getAnnualCost(){ Enumeration e = getStaff(); int result = 0; while (e.hasMoreElements()) { Party each = (Party) e.nextElement(); result += each.getAnnualCost(); return result;

23 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.

24 Example double charge(Employee emp, int days) {
int base = emp.getRate() * days; if (emp.hasSpecialSkill()) return base * 1.05; else return base; } interface Billable { public int getRate(); public boolean hasSpecialSkill(); class Employee implements Billable ... double charge(Billable emp, int days) {

25 Collapse Hierarchy A superclass and subclass are not very different.
Merge them together.

26 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.

27 Example class MyStack { class MyStack extends Vector {
public void push(Object element) { insertElementAt(element,0); } public Object pop() { Object result = firstElement(); removeElementAt(0); return result; class MyStack { private Vector _vector = new Vector(); public boolean isEmpty() { return _vector.isEmpty(); } public void push(Object element) { _vector.insertElementAt(element,0); public Object pop() { Object result = _vector.firstElement(); _vector.removeElementAt(0); return result;


Download ppt "Catalog of Refactoring"

Similar presentations


Ads by Google