Académique Documents
Professionnel Documents
Culture Documents
History
Buzzwords
Refactoring is restructuring code in a series of small, semanticspreserving transformations (i.e. the code keeps working) in order to
make the code easier to maintain and modify
Design Patterns
UML
This talk will cover just a tiny bit of one kind of diagram, the
class diagram
5
Key:
Card
cardId:int
-copy:boolean=false
constructor Card(int id)
+isKind(desiredKind:int)
+isSharable():boolean
+toString():String
UML relationships
A
Factory
1..4
B
Class B
extends
class A
B
Class B
implements
interface A
D
Class C
contains
1 to 4 objects
of class D
creates
Product
Other kinds of
relations
Ill try to use examples that are relevant to problems you have
dealt with in this class (plus some Im particularly fond of)
Examples:
If you do a lot with this object, you code can end up cluttered
with tests for null
9
11
1.
2.
3.
You may find it useful to create an explicitly nullable interface for the
isNull method.
As an alternative you can use a testing interface to test for nullness
Compile.
Find all places that can give out a null when asked for a source
object. Replace them to give out a null object instead.
12
4.
5.
6.
7.
8.
You may be able to do this by replacing one source and its clients at a
time and compiling and testing between working on sources.
A few assertions that check for null in places where you should no
longer see it can be useful.
Refactoring details
BigFish
LittleFish
move()
move()
15
To refactor:
<<abstract>>move()
BigFish
LittleFish
move()
move()
Fish
move()
<<abstract>>okToMove(locn):boolean
BigFish
BigFish
okToMove(locn):boolean
okToMove(locn):boolean
19
You may want to guarantee that you can never have more than one object
of a given class
You may want to create an object only if you dont already have an
equivalent object
You may want to create an object without being sure exactly what kind of
object you want
20
Singleton
You may want just one instance of a null object, which you use in many
places
You may want to create just one AudioStream, so you can only play one
tune at a time
class Singleton {
private static Singleton instance = new Singleton();
// dont let Java give you a default public constructor
private Singleton() { }
Singleton getInstance() {
return instance;
}
...
21
Example:
Example:
If the back ends vary, you man need to write a separate Interface class for each
(all implementing the same interface), but the users of your Facade class dont
need to change
class Ocean {
public void setBigFishGestationPeriod(int period) {
BigFish.setGestationPeriod(period);
}
public int getNumberOfAlgae() {
return Algae.getCount();
}
...
}
24
When you create a subclass, you agree to inherit all its (non-private)
fields and methods
You may want to inherit just some of the functionality, and probably add some of
your own
Inheritance doesnt let you do thatat least, not easily
26
Example: Stacks
class Stack {
Vector contents = new Vector();
public void push(Object o) {
contents.add(o); // delegate to the Vector
}
public Object pop() {
return contents.remove(contents.size() 1);
}
...
}
27
An example
+
2
*
5
Treefor2 + 5 * x
Command
lhs:Command
rhs:Command
0..2 value:int
evaluate():int
28
Command
lhs:Command
rhs:Command
0..2 value:int
evaluate():int
29
You want to control if, when, and in what order the commands
are executed
You want to keep a log of commands executed
Popular reason: You want to manage undo and redo operations
Refactoring
Refactoring is:
You should be at least somewhat familiar with these before inventing your
own
31
When to refactor
32
33
34
A personal view
In my opinion,
35
Back to refactoring
Any time you find that you can improve the design of existing
code
You detect a bad smell (an indication that something is
wrong) in the code
36
37
Example 1, continued
class Animal {
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";
}
}
}
38
Example 1, improved
class Animal {
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"; }
}
39
40
JUnit tests
41
Duplicate Code
Long Methods
Large Classes
Long Parameter Lists
Multi location code changes
Feature Envy
Data Clumps
Primitive Obsession
42
The End
43