Software Engineering Modern Approaches Chapter 24. Refactoring
Using a Refactoring Wizard Shift/Alt/R © 2010 John Wiley & Sons Ltd.
Fowler’s Refactoring Taxonomy Big Refactorings Composing Methods Moving Features Between Objects Organizing Data Dealing With Generalization Simplifying Conditional Expressions Making Method Calls Simpler © 2010 John Wiley & Sons Ltd.
Big Refactorings 1: Tease Apart Inheritance * Employee before SoftwareEmp MaintceEmp ClericalEmp FulltimeSoftwareEmp FulltimeMaintceEmp FulltimeClericalEmp ParttimeSoftwareEmp ParttimeMaintceEmp ParttimeClericalEmp RetiredSoftwareEmp RetiredMaintceEmp RetiredClericalEmp *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Big Refactorings 1: Tease Apart Inheritance * Employee before SoftwareEmp MaintceEmp ClericalEmp FulltimeSoftwareEmp FulltimeMaintceEmp FulltimeClericalEmp ParttimeSoftwareEmp ParttimeMaintceEmp ParttimeClericalEmp RetiredSoftwareEmp RetiredMaintceEmp RetiredClericalEmp after Employee Status SoftwareEmp MaintceEmp ClericalEmp Fulltime Parttime Retired *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Big Refactorings 2*: Convert Procedural Design to Objects before Control startGame() displayCharacter() moveCharacter() VideoGame GameCharacter *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Big Refactorings 2*: Convert Procedural Design to Objects before Control startGame() displayCharacter() moveCharacter() VideoGame GameCharacter after GameCharacter display() move() VideoGame start() *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Big Refactorings 3*: Separate Domain from Presentation Account name balance … displayStandard() displayHTML() Big Refactorings 3*: Separate Domain from Presentation before *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Big Refactorings 3*: Separate Domain from Presentation Account name balance … displayStandard() displayHTML() Big Refactorings 3*: Separate Domain from Presentation before StandardAccountGUI display () after Account name balance … HTMLAccountGUI display () *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Big Refactorings 4*: Extract Hierarchy before Project *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Big Refactorings 4*: Extract Hierarchy before Project after Project SoftwareEngineeringProject CustomerEntertainmentProject MobileApplication DesktopApplication *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Organizing Data 3* Replace Record with Data Class Simplest object with private data field, accessor Replace Type Code with Class Account … type before AccountType REGULAR: AccountType BIG_DISCOUNT: AccountType SMALL_DISCOUNT: AccountType Account … after *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Replace Type Code with Subclass Organizing Data 4* Replace Type Code with Subclass Replace Type Code with State/Strategy Account … type Account … type *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
WholesaleAccountType Organizing Data 4* Replace Type Code with Subclass Replace Type Code with State/Strategy Account … Account … type RegularAccount WholesaleAccount Account … AccountType … Account … type RegularAccountType WholesaleAccountType *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Dealing with Generalization 2* Extract Subclass Extract Superclass Order quantity discount minimum type Manager name salary numSupervisees Engineer name salary skillSet *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Dealing with Generalization 2* Order quantity Extract Subclass Extract Superclass Order quantity discount minimum type WholesaleOrder discount minimum Employee name salary Manager name salary numSupervisees Engineer name salary skillSet Manager numSupervisees Engineer skillSet *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Dealing with Generalization 3* Extract Interface Collapse Hierarchy Inherited class not special enough Manager name salary numSupervisees billRate Engineer name salary skillSet billRate *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Dealing with Generalization 3* Extract Interface Collapse Hierarchy Inherited class not special enough Employee getName() getSalary() Billable getRate() Manager name salary numSupervisees billRate Engineer name salary skillSet billRate Manager numSupervisees Engineer skillSet *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Dealing with Generalization 4*: Form Template Method BicycleAssemblyInstructions writeBikeInstructions() TricycleAssemblyInstructions writeTrikeInstructions() *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Dealing with Generalization 4*: Form Template Method AssemblyInstructions writePrep() writeSafety() writeWrapUp() writeManual() Dealing with Generalization 4*: Form Template Method BicycleAssemblyInstructions writePrep() writeSafety() writeWrapUp() BicycleAssemblyInstructions writeBikeInstructions() TricycleAssemblyInstructions writeTrikeInstructions() TricycleAssemblyInstructions writePrep() writeSafety() writeWrapUp() *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Fowler: Dealing with Generalization* Replace Inheritance with Delegation Replace Delegation with Inheritance Record lastChanged() Account *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.
Fowler: Dealing with Generalization* Replace Inheritance with Delegation Replace Delegation with Inheritance Record lastChanged() record.lastChanged() Account record Account lastChanged() Record lastChanged() *Fowlers’ taxonomy © 2010 John Wiley & Sons Ltd.