Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 9. Dealing with Generalization Course: Refactoring.

Similar presentations


Presentation on theme: "Module 9. Dealing with Generalization Course: Refactoring."— Presentation transcript:

1 Module 9. Dealing with Generalization Course: Refactoring

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

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

4 Pull Up Field Two subclasses have the same field. Move the field to the superclass.

5 Pull Up Field: Mechanism 1. Inspect all uses of the candidate fields. 2. Rename the fields. 3. Compile and test. 4. Create a new field in the superclass. 5. Delete the subclass fields. 6. Compile and test. 7. Self Encapsulate Field.

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

7 Pull Up Method: Mechanism 1. Inspect the methods. 2. Change the signatures. 3. Create a new method. 4. Delete one subclass method. 5. Compile and test. 6. Keep deleting subclass methods and testing. 7. Change a required type to the superclass.

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

9 Pull Up Constructor Body 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; }

10 Pull Up Constructor Body: Mechanism 1. Define a superclass constructor. 2. Move the common code. 3. Call the superclass constructor. 4. Compile and test.

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

12 Push Down Method: Mechanism 1. Declare a method in all subclasses. 2. Remove method from superclass. 3. Compile and test. 4. Remove if does not need it. 5. Compile and test.

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

14 Push Down Field: Mechanism 1. Declare the field in all subclasses. 2. Remove the field from the superclass. 3. Compile and test. 4. Remove the field from all subclasses that don't need it. 5. Compile and test.

15 Exercise Pull Up field, method Extract Interface

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

17 Extract Subclass Before:  a class has behavior used for some instances of the class and not for others After:  delegate vs inheritance  can't change the class-based behavior

18 Extract Subclass: Mechanism 1. Define a new subclass. 2. Provide constructors. 3. Replace Constructor with Factory Method 4. Find all calls to constructors [and replace]. 5. Move features onto the subclass. 6. Self Encapsulate Field 7. Replace Conditional with Polymorphism 8. Compile and test after each push down.

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

20 Extract Superclass: Mechanism 1. Create an abstract superclass. 2. Move common elements to the superclass. 3. Compile and test after each pull. 4. See if there are common parts. 5. Use only the common interface.

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

22 Extract Interface Before:  use of a subset of a class's responsibilities  a class works with any class that can handle certain requests After:  make the subset of responsibilities a thing in its own right  clear in the use of the system Hints:  distinct roles in different situations  describe the outbound interface of a class

23 Extract Interface: Mechanism 1. Create an empty interface. 2. Declare the common operations. 3. Declare the relevant class(es). 4. Adjust client type declarations.

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

25 Collapse Hierarchy: Mechanism 1. Choose class. 2. Move all the behavior and data. 3. Compile and test with each move. 4. Adjust references. 5. Remove the empty class. 6. Compile and test.

26 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 can pull them up.

27 Form Template Method

28

29 Form Template Method: Mechanism 1. Decompose the methods. 2. Pull Up Method. 3. Rename Method. 4. Compile and test after each signature change. 5. Pull Up Method. 6. Compile and test. 7. Remove the other methods 8. Compile and test.

30 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. Replace Inheritance with Delegation

31 Before:  the superclass operations aren't really true of the subclass  load of data that is not appropriate for the subclass  protected superclass methods  one thing when your intention is something else After:  only partial use of the delegated class  choose an interface part  extra delegating methods Replace Inheritance with Delegation

32 Replace Inheritance with Delegation: Mechanism 1. Create a field. 2. Use the delegate field. 3. Compile and test after changing each method. 4. Remove the subclass and replace the delegate. 5. For each superclass add a simple delegating method. 6. Compile and test.

33 You're using delegation and are often writing many simple delegations for the entire interface. Make the delegating class a subclass of the delegate. Replace Delegation with Inheritance

34 Before:  use all the methods of the delegate  sick of writing all those simple delegating methods Hints:  Don’t use you aren't using all the methods the delegate is shared by more than one object Replace Delegation with Inheritance

35 Replace Delegation with Inheritance: Mechanism 1. Make the delegating object a subclass of the delegate. 2. Compile. 3. Set the delegate field to be the object itself. 4. Remove the simple delegation methods. 5. Compile and test. 6. Replace all other delegations. 7. Remove the delegate field.

36 Exercise Form Template method Real Project

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


Download ppt "Module 9. Dealing with Generalization Course: Refactoring."

Similar presentations


Ads by Google