software reengineering refactoring to patterns · 2016. 6. 23. · creation move functionality to...
TRANSCRIPT
![Page 1: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/1.jpg)
Software ReengineeringRefactoring To Patterns
Martin Pinzger & Andy ZaidmanDelft University of Technology
![Page 2: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/2.jpg)
2
Outline
Introduction
Design Patterns
Refactoring to Patterns
Conclusions
![Page 3: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/3.jpg)
3
The Reengineering Life-Cycle
(1) requirementanalysis
(2) modelcapture
(3) problemdetection (4) problem
resolution
NewRequirements
Designs
Code
![Page 4: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/4.jpg)
4
Object-Oriented Design Patterns
„Descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.“ !
[Gamma, Helm, Johnson, Vlissides 1995]
![Page 5: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/5.jpg)
5
Design Patterns Idea
Reoccurring design problems ! idea:Do not solve the problem again
... use an existing pattern that can be parameterized
from which concrete solutions can be derived
Reuse design knowledge
Vocabulary for communicating design
![Page 6: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/6.jpg)
6
Elements of Design Patterns
Pattern nameDesign Vocabulary
ProblemWhen to apply the pattern?
List of preconditions
SolutionAbstract description of a design problem and how a general arrangements of classes and objects solves it
ConsequencesImpact on flexibility, extensibility, portability, etc.
![Page 7: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/7.jpg)
7
Describing Design Patterns
Elements of the descriptionPattern name and classification
Intent, Also known as, Motivation
Applicability
Structure, Participants, Collaborations
Consequences
Implementation
Sample code, Known uses, Related patterns
![Page 8: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/8.jpg)
Design Patterns (GoF) Classification
8
PurposePurposePurposePurposePurposeCreational Structural Behavioral
Scope Class Factory Method Adapter (class) Interpreter
Template Method
Object Abstract Factory Adapter (object) Chain of Responsibility
Prototype Bridge Command
Builder Composite Iterator
Singleton Decorator Mediator
Facade Memento
Flyweight Observer
Proxy State
Strategy
Visitor
![Page 9: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/9.jpg)
Additional Reading
9
Design Patterns: Elements of Reusable Object-Oriented SoftwareErich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides
Online: http://sourcemaking.com/design_patterns
![Page 10: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/10.jpg)
Refactoring to Patterns
![Page 11: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/11.jpg)
Design Patterns and Refactorings
“There is a natural relation between patterns and refactoring. Patterns are where you want to be; refactorings are ways to get there from somewhere else.”
[Fowler 1999]
11
![Page 12: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/12.jpg)
Refactoring to Patterns: Book
12
Joshua Kerievsky, Addison-Wesley, 2005
A set of composite refactorings to refactor towards design patterns
Some patterns online: http://www.informit.com/articles/article.aspx?p=1398607
![Page 13: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/13.jpg)
Overview of Refactorings
13
CreationMove functionality to create instances of complex classes to Factory and Builder classes
SimplificationSimplify the source code by introducing strategy, state, commands, composites, and decorators
GeneralizationTransform specific code into general-purpose code by introducing Template Method, Composite, Observer, Adapter, and Interpreter
![Page 14: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/14.jpg)
Overview of Refactorings (cont.)
ProtectionProtect existing code from modifications by introducing a Singleton and Null Object
AccumulationAccumulating information by introducing a Visitor
14
![Page 15: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/15.jpg)
15
Refactoring to Patterns: Examples
Factory MethodIntroduce Polymorphic Creation with Factory Method
StateFactor out State
ObserverReplace Hard-Coded Notifications with Observer
![Page 16: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/16.jpg)
Example 1: Creation
Introduce Polymorphic Creation with Factory Method
![Page 17: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/17.jpg)
Example BuilderTest
17
public class DOMBuilderTest extends TestCase {private OutputBuilder builder;public void testAddAboveRoot() {
String invalidResult = “<orders> ... </orders>”;builder = new DOMBuilder(“orders”);builder.addBelow(“order”);try {
builder.addAbove(“customer”);} catch (RuntimeException re) {
fail(“Message”, re); }
}}
public class XMLBuilderTest extends TestCase {private OutputBuilder builder;public void testAddAboveRoot()
// the samebuilder = new XMLBuilder(“orders”);// the same
}}
![Page 18: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/18.jpg)
18
Introduce Factory Method
ProblemClasses in a hierarchy implement a method similarly, except for an object creation step
SolutionMake a single superclass version of the method that calls a Factory Method to handle the instantiation
![Page 19: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/19.jpg)
19
Factory Method Pattern
IntentDefine an interface for creating an object, but let subclasses decide which class to instantiate.
MotivationEncapsulate the knowledge of which subclass to create to a factory method
ApplicabilityClass wants its subclasses to specify the objects it creates
![Page 20: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/20.jpg)
20
Factory Method Pattern: Structure
FactoryMethod()AnOperation()
Creator
FactoryMethod()ConcreteCreator
Product
ConcreteProduct
product = FactoryMehod()
return new ConcreteProduct()
![Page 21: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/21.jpg)
21
Example BuilderTest: Class Diagram
TestCase
testAddAboveRoot(): voidDomBuilderTest
...builder = new XMLBuilder("orders");...
testAddAboveRoot(): voidXMLBuilderTest
...builder = new XMLBuilder("orders");...
OutputBuilder
DOMBuilder
XMLBuilder
![Page 22: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/22.jpg)
22
Introduce Factory Method: Mechanics
Extract Method on creation codeRepeat in sibling subclasses
Extract Superclass to create Creator
Pull Up Method and Form Template Method of testAddAboveRoot()
Add abstract Factory MethodImplement concrete factory method in subclasses
![Page 23: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/23.jpg)
Extract Method on creation code
23
public class DOMBuilderTest extends TestCase {private OutputBuilder builder;private OutputBuilder createBuilder(String rootName) {
return new DOMBuilder(“orders”);}public void testAddAboveRoot() {
String invalidResult = “<orders> ... </orders>”;builder = createBuilder(“orders”);builder.addBelow(“order”);...
}}public class XMLBuilderTest extends TestCase {
private OutputBuilder builder;private OutputBuilder createBuilder(String rootName) {
return new XMLBuilder(“orders”);}public void testAddAboveRoot()
...builder = createBuilder(“orders”);...
}}
![Page 24: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/24.jpg)
Extract Superclass
24
public abstract class AbstractBuilderTest extends TestCase {
}
public class DOMBuilderTest extends AbstractBuilderTest {...
}
public class XMLBuilderTest extends AbstractBuilderTest {...
}
![Page 25: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/25.jpg)
Form Template Method and Pull Up Field
25
public abstract class AbstractBuilderTest extends TestCase {protected OutputBuilder builder;
protected abstract OutputBuilder createdBuilder (String rootName);}
![Page 26: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/26.jpg)
createBuilder(rootName:String): OutputBuilderDomBuilderTest
return new DOMBuilder(rootName);
createBuilder(rootName:String): OutputBuilderXMLBuilderTest
return new XMLBuilder(rootName);
createBuilder(rootName:String) : OutputBuildertestAddAboveRoot() : void
builder : OutputBuilderAbstractBuilderTest
TestCase
...builder = createBuilder("orders");...
26
Result Refactoring BuilderTest
FactoryMethod
ConcreteCreator
![Page 27: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/27.jpg)
27
Benefits and Liabilities
+ Reduces duplication resulting from a custom object creation step
+ Effectively communicates where creation occurs and how it may be overridden
+ Enforces what type a class must implement to be used by a Factory Method
- May require you to pass unnecessary parameters to some Factory Method implementers
![Page 28: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/28.jpg)
Example 2: Simplicfication
Factor out State
![Page 29: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/29.jpg)
29
Example SystemPermissionpublic class SystemPermission... private SystemProfile profile; private SystemUser requestor; private SystemAdmin admin; private boolean isGranted; private String state; public final static String REQUESTED = "REQUESTED"; public final static String CLAIMED = "CLAIMED"; public final static String GRANTED = "GRANTED"; public final static String DENIED = "DENIED"; public SystemPermission(SystemUser requestor, SystemProfile profile) { this.requestor = requestor; this.profile = profile; state = REQUESTED; isGranted = false; notifyAdminOfPermissionRequest(); } ...
![Page 30: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/30.jpg)
30
Example SystemPermission (cont.) ... public void claimedBy(SystemAdmin admin) { if (!state.equals(REQUESTED)) return; willBeHandledBy(admin); state = CLAIMED; }
public void deniedBy(SystemAdmin admin) { if (!state.equals(CLAIMED)) return; if (!this.admin.equals(admin)) return; isGranted = false; state = DENIED; notifyUserOfPermissionRequestResult(); } public void grantedBy(SystemAdmin admin) { if (!state.equals(CLAIMED)) return; if (!this.admin.equals(admin)) return; state = GRANTED; isGranted = true; notifyUserOfPermissionRequestResult(); }}
![Page 31: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/31.jpg)
31
Factor out State
ProblemHow to make a class extensible whose behavior depends on a complex evaluation of its state?
SolutionEliminate complex conditional code over an object’s state by applying the State Pattern
![Page 32: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/32.jpg)
32
State Pattern
IntentAllow an object to alter its behavior when its internal state changes. The object will appear to change its class.
ApplicabilityAn object’s behavior depends on its state, and it must change its behavior at run-time depending on that state
Operations have large, multipart conditional statements that depend on the object’s state.
![Page 33: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/33.jpg)
33
State Pattern: Structure
Handle()State
Handle()ConcreteStateA
Handle()ConcreteStateB
Request()Context
state.Handle()
![Page 34: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/34.jpg)
Example SystemPermission: Class Diagram
34
claimedBy(...) : voidgrantedBy(...) : voiddeniedBy(...) : vodi...
state : StringREQUESTES : StringCLAIMED : StringGRANTED : StringDENIED : String...
SystemPermission
if (! state.equals(REQUESTED) return;willBeHandledBy(admin);state = CLAIMED;
![Page 35: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/35.jpg)
Change: Adding two more states
35
![Page 36: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/36.jpg)
36
Replace Conditionals with State: Mechanics
Replace Type Code with Class On the original state field in the context class
Extract Subclass To produce one subclass per constant (state) and declare state superclass as abstract
Move Method On context class methods that change the value of original state variable
![Page 37: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/37.jpg)
Replace Type Code with State Class
37
public class PermissionState { private String name;
private PermissionState(String name) { this.name = name; }
public final static PermissionState REQUESTED = new PermissionState("REQUESTED"); public final static PermissionState CLAIMED = new PermissionState("CLAIMED"); public final static PermissionState GRANTED = new PermissionState("GRANTED"); public final static PermissionState DENIED = new PermissionState("DENIED"); public final static PermissionState UNIX_REQUESTED = new PermissionState("UNIX_REQUESTED"); public final static PermissionState UNIX_CLAIMED = new PermissionState("UNIX_CLAIMED");
public String toString() { return name; }}
![Page 38: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/38.jpg)
Replace Type Code with State Class (cont.)
38
public class SystemPermission { private PermissionState permissionState;
public SystemPermission(SystemUser requestor, SystemProfile profile) { ... setState(PermissionState.REQUESTED); ... }
private void setState(PermissionState state) { permissionState = state; }
public void claimedBy(SystemAdmin admin) { if (!getState().equals(PermissionState.REQUESTED) && !getState().equals(PermissionState.UNIX_REQUESTED)) return; ... } ...}
![Page 39: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/39.jpg)
Extract Subclass for each State (Constant)
39
public abstract class PermissionState { private String name; private PermissionState(String name) { this.name = name; } public final static PermissionState REQUESTED = new PermissionRequested(); ...}
public class PermissionRequested extends PermissionState { public PermissionRequested() { super(“REQUESTED”); }}public class PermissionClaimed extends PermissionState { ... }public class PermissionDenied extends PermissionState { ... }public class PermissionGranted extends PermissionState { ... }public class UnixPermissionRequested extends PermissionState { ... }public class UnixPermissionClaimed extends PermissionState { ... }
![Page 40: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/40.jpg)
Move State Trans. Logic to State Class
40
public abstract class PermissionState... public void claimedBy(SystemAdmin admin, SystemPermission permission) { if (!permission.getState().equals(REQUESTED) && !permission.getState().equals(UNIX_REQUESTED)) return; permission.willBeHandledBy(admin); if (permission.getState().equals(REQUESTED)) permission.setState(CLAIMED); else if (permission.getState().equals(UNIX_REQUESTED)) { permission.setState(UNIX_CLAIMED); } }
![Page 41: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/41.jpg)
Move State Trans. Logic (cont.)
41
public class SystemPemission { ... void setState(PermissionState state) { // now has package-level visibility permissionState = state; } public void claimedBy(SystemAdmin admin) { state.claimedBy(admin, this); } void willBeHandledBy(SystemAdmin admin) { this.admin = admin; }}
![Page 42: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/42.jpg)
Move State Trans. Logic to Subclasses
42
public class PermissionRequested extends PermissionState { ... public void claimedBy(SystemAdmin admin, SystemPermission permission) { permission.willBeHandledBy(admin); permission.setState(CLAIMED); }}
![Page 43: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/43.jpg)
Move State-Transition to Subclasses (cont.)
43
public class PermissionClaimed extends PermissionState... public void deniedBy(SystemAdmin admin, SystemPermission permission) { if (!permission.getAdmin().equals(admin)) return; permission.setIsGranted(false); permission.setIsUnixPermissionGranted(false); permission.setState(DENIED); permission.notifyUserOfPermissionRequestResult(); } public void grantedBy(SystemAdmin admin, SystemPermission permission) { if (!permission.getAdmin().equals(admin)) return; if (permission.getProfile().isUnixPermissionRequired() && !permission.isUnixPermissionGranted()) { permission.setState(UNIX_REQUESTED); permission.notifyUnixAdminsOfPermissionRequest(); return; } permission.setState(GRANTED); permission.setIsGranted(true); permission.notifyUserOfPermissionRequestResult(); }
![Page 44: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/44.jpg)
Move State-Transition to Subclasses (cont.)
44
public abstract class PermissionState { public String toString(); public void claimedBy(SystemAdmin admin, SystemPermission permission) {} public void deniedBy(SystemAdmin admin, SystemPermission permission) {} public void grantedBy(SystemAdmin admin, SystemPermission permission) {}}
![Page 45: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/45.jpg)
45
Result Refactoring SystemPermission
claimedBy(...) : voidgrantedBy(...) : voiddeniedBy(...) : void
SystemPermission
permissionState.claimedBy(...);
PermissionState(name : String)claimedBy(...) : voidgrantedBy(...) : voiddeniedBy(...) : vodi
name : StringREQUESTES : PermissionStateCLAIMED : PermissionStateGRANTED : PermissionStateDENIED : PermissionState...
PermissionState
grantedBy(...) : voiddeniedBy(...) : void
PermissionClaimedclaimedBy(...) : void
PermissionRequested
permissionState
...Permission...
permission.willBeHandledBy(admin);permission.setState(CLAIMED);
![Page 46: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/46.jpg)
46
Benefits and Liabilities
+ Reduces or removes state-changing conditional logic
+ Simplifies complex state-changing logic
+ Provides a good bird’s-eye view of state-changing logic
- Complicates design when state transition logic is already easy to follow
![Page 47: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/47.jpg)
Example 3: Generalization
Replace Hard-Coded Notifications with Observer
![Page 48: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/48.jpg)
Example: TestResult
48
public class UITestResult extends TestResult { private TestRunner runner; UITestResult(TestRunner runner) { this.runner = runner; } public synchronized void addFailure(Test test, Throwable t) { super.addFailure(test, t); runner.addFailure(this, test, t); // notification of TestRunner }}public class TestRunner extends Frame { private TestResult testResult; protected TestResult createdTestResult() { return new UITestResult(this); } public synchronized void runSuite() { testResult = createTestResult(); testSuite.run(testResult); } public void addFailure(TestResult result, Test test, Throwable t) { // display the test result }}
![Page 49: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/49.jpg)
49
Replace Notifications with Observer
ProblemSubclasses are hard-coded to notify a single instance of another class
SolutionRemove the subclass by making their superclass capable of notifying one or more instances of any class that implements an observer interface
![Page 50: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/50.jpg)
Observer Pattern
50
IntendMaintain a dependency between a central object (Subject) and multiple dependent objects (Observers)
MotivationDecouple a subject from its observers
ApplicabilityWhen an instance must notify more than one receiver instance,
E.g., when there are various views (Observers) on the same model instance (Subject)
![Page 51: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/51.jpg)
51
Observer Pattern: Structure
update() : voidObserverattach(observer:Observer) : void
detach(observer:Observer) : voidnotify() : void
Subject
for (Observer o : observers) { o.update();}
getState() : Statestate : State
ConcreteSubject
observerState = subject.getState();
observers
update() : voidobserverState : StateConcreteObserver
subject
![Page 52: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/52.jpg)
52
Example TestResult: Class Diagram
TestResults
textui.TextTestResults
textui.TestRunner
ui.UITestResults
ui.TestRunner
Hard-Coded Notifications
![Page 53: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/53.jpg)
53
Repl. Notific. with Observer: Mechanics
Move custom behavior to receiver
Extract Interface on a receiver to produce an observer interfaceOnly methods called by its notifier
Make every receiver implement the observer interface
Pull Up Method on notification methods in notifier classes
Update notification implementation
Add collection to subject and update observer to register and communicate with the subject
![Page 54: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/54.jpg)
Move Custom Behavior to Receiver
54
package textui;public class TextTestResult extends TestResult { private TestRunner runner; TextTestResult(TestRunner runner) { this.runner = runner; } public synchronized void addError(Test test, Throwable t) { super.addError(test, t); runner.addError(this, test, t); }}
package textui;public class TestRunner ... protected TextTestResult createdTestResult() { return new TextTestResult(this); }
public void addError(TestResult result, Test test, Throwable t) { System.out.println(“E”); }}
![Page 55: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/55.jpg)
Extract Observer Interface
55
public class TextTestResult extends TestResult ... public synchronized void addError(Test test, Throwable t) { super.addError(test, t); runner.addError(this, test, t); } public synchronized void addFailure(Test test, Throwable t) { super.addFailure(test, t); runner.addFailure(this, test, t); } public synchronized void startTest(Test test) { super.startTest(test); runner.startTest(this, test); }}public interface TestListener { public void addError(TestResult testResult, Test test, Throwable t); public void addFailure(TestResult testResult, Test test, Throwable t); public void startTest(TetResult testResult, Test test); public void endTest(TestResult testResult, Test test);}public class TestRunner implements TestListener ... public void endTest(TestResult testResult, Test test) { }}
![Page 56: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/56.jpg)
Make Receiver Implement the Interface
56
package ui;public class TestRunner extends Frame implements TestListener { ...}
package textui;public class TextTestResult extends TestResult ... protected TestListener runner;
TextTestResult(TestListener runner) { this.runner = runner; }}
package ui;public class UITestResult extends TestResult ... protected TestListener runner;
UITestResult(TestListener runner) { this.runner = runner; }}
![Page 57: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/57.jpg)
Pull Up Methods in Observers
57
public class TestResult ... protected TestListener runner; public TestResult() { failures = new ArrayList<TestFailure>(); errors = new ArrayList<TestError>(); runTests = 0; stop = false; } public TestResult(TestListener runner) { this(); this.runner = runner; } public synchronized void addError(Test test, Throwable t) { errors.add(new TestError(test, t)); runner.addError(this, test, t); } ...}package ui;public class UITestResult extends TestResult { }
package textui;public class TextTestResult extends TestResult { }
![Page 58: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/58.jpg)
Update Observers to Work with Subject
58
package textui;public class TestRunner implements TestListener ... protected TestResult createTestResult() { return new TestResult(this); }}
package ui;public class TestRunner implements TestListener ... protected TestResult createTestResult() { return new TestResult(this); } public synchronized void runSuite() { ... TestResult result = createTestResult(); testSuite.run(testResult);}
![Page 59: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/59.jpg)
Update Subject to Notify many Observers
59
public class TestResult ... protected List<TestListener> observers = new ArrayList<TestListener>();
public void addObserver(TestListener observer) { observers.add(observer); }
public synchronized void addError(Test test, Throwable t) { errors.add(new TestError(test, t)); for (TestListener observer : observers) { observer.addError(this, test, t); } } ...}
![Page 60: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/60.jpg)
Update Observer to Register with Subject
60
package textui;public class TestRunner implements TestListener ... protected TestResult createTestResult() { TestResult testResult = new TestResult(this); testResult.addObserver(this); return testResult; }}
![Page 61: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/61.jpg)
61
Result: Refactoring TestResult
TestResult
TestListener
textui.TestRunner
ui.TestRunner
![Page 62: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/62.jpg)
62
Benefits and Liabilities
+ Loosely couples a subject with its observers
+ Supports one or many observers
- Complicate design When a hard-coded notification will suffice
When you have cascading notifications
- May cause memory leaks when observers aren’t removed from their subjects
![Page 63: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/63.jpg)
More Refactorings To Patterns
63
Joshua Kerievsky, Addison-Wesley, 2005
A set of composite refactorings to refactor towards design patterns
Some patterns online: http://www.informit.com/articles/article.aspx?p=1398607
![Page 64: Software Reengineering Refactoring To Patterns · 2016. 6. 23. · Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification](https://reader033.vdocuments.us/reader033/viewer/2022060521/605061cc156f585c9a1a5528/html5/thumbnails/64.jpg)
64
Conclusions
Refactoring to PatternsKnown and tested solutions for similar design problems
Encapsulates and simplifies logic
Increases extensibility (interfaces, loose coupling)
But, don’t overdo itOnly use a pattern when it (really) makes sense