Lecture 18 - Tuesday November 22 2011. Material and some slide content from:
- Mehdi Amoui Kalareh
- Fowler Refactoring book
Code Smells & Refactoring Reid Holmes Lecture 18 - Tuesday - - PowerPoint PPT Presentation
Material and some slide content from: - Mehdi Amoui Kalareh - Fowler Refactoring book Code Smells & Refactoring Reid Holmes Lecture 18 - Tuesday November 22 2011. Program restructuring Software systems represent massive investments.
Lecture 18 - Tuesday November 22 2011. Material and some slide content from:
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
takes place on existing systems.
largest proportion of a system’s total budget.
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
(% from SE Matinenance, Hans van Vilet)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
evolution (beginning in 1974)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
final int MAMMAL = 0, BIRD = 1, REPTILE = 2; int myKind; // set in constructor ... String getSkin() { switch (myKind) { case MAMMAL: return "hair"; case BIRD: return "feathers"; case REPTILE: return "scales"; default: return "integument"; } } }
String getSkin() { return "integument"; } } class Mammal extends Animal { String getSkin() { return "hair"; } } class Bird extends Animal { String getSkin() { return "feathers"; } } class Reptile extends Animal { String getSkin() { return "scales"; } }
BEFORE AFTER [adding Insect is easy] [subclasses probably differ] [avoid other switches]
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
STRATEGY ABSTRACT FACTORY SINGLETON
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
BigFish move() Fish
<<abstract>>move()
LittleFish move()
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
General outline of the method:
public void move() { choose a random direction; // same for both find the location in that direction; // same for both check if it’s ok to move there; // different if it’s ok, make the move; // same for both }
Solution:
Extract the check on whether it’s ok to move In the Fish class, put the actual (template) move() method Create an abstract okToMove() method in the Fish class Implement okToMove() in each subclass
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
duplicating code.
BigFish move() Fish
<<abstract>>move()
LittleFish move() BigFish
Fish
move()
<<abstract>>okToMove(locn):boolean
BigFish
When%a%BigFish%tries% to%move,%it%uses%the% move()%method%in% Fish%
method%in%Fish%uses% the%okToMove(locn) method%in%BigFish
LittleFish%