Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Similar documents
Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Design Patterns. An introduction

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Using Design Patterns in Java Application Development

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

CS/CE 2336 Computer Science II

Lecture 13: Design Patterns

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Applying the Observer Design Pattern

Introduction and History

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

SDC Design patterns GoF

Design Patterns. Gunnar Gotshalks A4-1

Object-Oriented Design

The GoF Design Patterns Reference

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Object-Oriented Design

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

What is Design Patterns?

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

DESIGN PATTERN - INTERVIEW QUESTIONS

An Introduction to Patterns

Kerievsky_book.fm Page 355 Thursday, July 8, :12 PM. Index

Information systems modelling UML and service description languages

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Tuesday, October 4. Announcements

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

CHAPTER 6: CREATIONAL DESIGN PATTERNS

CS251 Software Engineering Lectures 18: Intro to DP

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

Design patterns. OOD Lecture 6

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

Applying the Decorator Design Pattern

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

Introduction to Software Engineering: Object Design I Reuse & Patterns

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Topics in Object-Oriented Design Patterns

What is Design Patterns?

Application Architectures, Design Patterns

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

1 Software Architecture

2.1 Design Patterns and Architecture (continued)

Review Software Engineering October, 7, Adrian Iftene

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Applying the Factory Method Design Pattern

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Object-Oriented Oriented Programming

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Design Patterns Reid Holmes

2.1 Design Patterns and Architecture (continued)

The Strategy Pattern Design Principle: Design Principle: Design Principle:

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Template Method Pattern. George Blankenship

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Overview of Patterns: Introduction

Crash course on design patterns

UNIT I Introduction to Design Patterns

What is Design Patterns?

An Introduction to Patterns

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Software Design COSC 4353/6353 D R. R A J S I N G H

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

CSE870: Advanced Software Engineering (Cheng) 1

Design patterns generic models

Applying Design Patterns to SCA Implementations

Dr. Xiaolin Hu. Review of last class

6.3 Patterns. Definition: Design Patterns

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

administrivia today UML start design patterns Tuesday, September 28, 2010

Design patterns. Jef De Smedt Beta VZW

UNIT I Introduction to Design Patterns

Reuse at Design Level: Design Patterns

Brief Note on Design Pattern

Software Engineering I (02161)

SOFTWARE ENGINEERING SOFTWARE DESIGN. Saulius Ragaišis.

Design Patterns. CSC207 Fall 2017

A Primer on Design Patterns

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

A Reconnaissance on Design Patterns

Software Design COSC 4353/6353 D R. R A J S I N G H

Object Oriented Paradigm

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

CS 247: Software Engineering Principles. Design Patterns

Design Patterns. Definition of a Design Pattern

Factory Method Pattern Creational. » Define an interface for creating an object but lets subclasses decide the specific class to instantiate

Creational Design Patterns

Transcription:

Software Reengineering Refactoring To Patterns Martin Pinzger Delft University of Technology

Outline Introduction Design Patterns Refactoring to Patterns Conclusions 2

The Reengineering Life-Cycle (1) requirement analysis New Requirements (3) problem detection (4) problem resolution Designs (2) model capture Code 3

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] 4

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 5

Elements of Design Patterns Pattern name Design Vocabulary Problem When to apply the pattern? List of preconditions Solution Abstract description of a design problem and how a general arrangements of classes and objects solves it Consequences Impact on flexibility, extensibility, portability, etc. 6

Describing Design Patterns Elements of the description Pattern name and classification Intent, Also known as, Motivation Applicability Structure, Participants, Collaborations Consequences Implementation Sample code, Known uses, Related patterns 7

Design Patterns (GoF) Classification Purpose Creational 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 8

Additional Reading Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides Online: http://sourcemaking.com/design_patterns 9

Refactoring to Patterns

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

Refactoring to Patterns: Book A set of composite refactorings to refactor towards design patterns Joshua Kerievsky, Addison-Wesley, 2005 Some patterns online: http://www.informit.com/articles/article.aspx?p=1398607 12

Overview of Refactorings Creation Move functionality to create instances of complex classes to Factory and Builder classes Simplification Simplify the source code by introducing strategy, state, commands, composites, and decorators Generalization Transform specific code into general-purpose code by introducing Template Method, Composite, Observer, Adapter, and Interpreter 13

Overview of Refactorings (cont.) Protection Protect existing code from modifications by introducing a Singleton and Null Object Accumulation Accumulating information by introducing a Visitor 14

Refactoring to Patterns: Examples Factory Method Introduce Polymorphic Creation with Factory Method State Factor out State Observer Replace Hard-Coded Notifications with Observer 15

Example 1: Creation Introduce Polymorphic Creation with Factory Method

Introduce Factory Method Problem Classes in a hierarchy implement a method similarly, except for an object creation step Solution Make a single superclass version of the method that calls a Factory Method to handle the instantiation 17

Factory Method Pattern Intent Define an interface for creating an object, but let subclasses decide which class to instantiate. Motivation Encapsulate the knowledge of which subclass to create to a factory method Applicability Class wants its subclasses to specify the objects it creates 18

Factory Method Pattern: Structure Product Creator FactoryMethod() AnOperation() product = FactoryMehod() ConcreteProduct ConcreteCreator FactoryMethod() return new ConcreteProduct() 19

Example BuilderTest 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 same builder = new XMLBuilder( orders ); // the same 20

Example BuilderTest: Class Diagram TestCase DomBuilderTest testaddaboveroot(): void XMLBuilderTest testaddaboveroot(): void... builder = new XMLBuilder("orders");...... builder = new XMLBuilder("orders");... OutputBuilder DOMBuilder XMLBuilder 21

Introduce Factory Method: Mechanics Extract Method on creation code Repeat in sibling subclasses Extract Superclass to create Creator Pull Up Method and Form Template Method of testaddaboveroot() Add abstract Factory Method Implement concrete factory method in subclasses 22

Extract Method on creation code 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 );... 23

Extract Superclass public abstract class AbstractBuilderTest extends TestCase { public class DOMBuilderTest extends AbstractBuilderTest {... public class XMLBuilderTest extends AbstractBuilderTest {... 24

Form Template Method and Pull Up Field public abstract class AbstractBuilderTest extends TestCase { protected OutputBuilder builder; protected abstract OutputBuilder createdbuilder (String rootname); 25

Result Refactoring BuilderTest TestCase AbstractBuilderTest builder : OutputBuilder createbuilder(rootname:string) : OutputBuilder testaddaboveroot() : void DomBuilderTest createbuilder(rootname:string): OutputBuilder FactoryMethod... builder = createbuilder("orders");... ConcreteCreator XMLBuilderTest createbuilder(rootname:string): OutputBuilder return new DOMBuilder(rootName); return new XMLBuilder(rootName); 26

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 27

Example 2: Simplicfication Factor out State

Factor out State Problem How to make a class extensible whose behavior depends on a complex evaluation of its state? Solution Eliminate complex conditional code over an object s state by applying the State Pattern 29

State Pattern Intent Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. Applicability An 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. 30

State Pattern: Structure Context Request() State Handle() state.handle() ConcreteStateA Handle() ConcreteStateB Handle() 31

Example SystemPermission public 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();... 32

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(); 33

Example SystemPermission: Class Diagram SystemPermission state : String REQUESTES : String CLAIMED : String GRANTED : String DENIED : String... claimedby(...) : void grantedby(...) : void deniedby(...) : vodi... if (! state.equals(requested) return; willbehandledby(admin); state = CLAIMED; 34

Test Case: SystemPermission public class TestSystemPermission { private SystemPermission permission; @Before public void setup() { permission = new SystemPermission(user, profile); @Test public void testgrantedby() { permission.grantedby(admin); assertequals("requested", permission.requested, permission.state()); assertequals("not granted", false, permission.isgranted()); permission.claimedby(admin); permission.grantedby(admin); assertequals("granted", permission.granted, permission.state()); assertequals("granted", true, permission.isgranted()); 35

Change: Adding two more states 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 37

Replace Type Code with Class 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; 38

Replace Type Code with Class (cont.) 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;...... 39

Extract Subclass for each Constant 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 {... 40

Move Method 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); 41

Move Method (cont.) 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; 42

Move State-Transition to Subclasses public class PermissionRequested extends PermissionState {... public void claimedby(systemadmin admin, SystemPermission permission) { permission.willbehandledby(admin); permission.setstate(claimed); 43

Move State-Transition to Subclasses (cont.) 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(); 44

Move State-Transition to Subclasses (cont.) 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) { 45

Result Refactoring SystemPermission permissionstate.claimedby(...); SystemPermission claimedby(...) : void grantedby(...) : void deniedby(...) : void permissionstate PermissionState name : String REQUESTES : PermissionState CLAIMED : PermissionState GRANTED : PermissionState DENIED : PermissionState... PermissionState(name : String) claimedby(...) : void grantedby(...) : void deniedby(...) : vodi PermissionClaimed grantedby(...) : void deniedby(...) : void... Permission... PermissionRequested claimedby(...) : void permission.willbehandledby(admin); permission.setstate(claimed); 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 47

Example 3: Generalization Replace Hard-Coded Notifications with Observer

Replace Notifications with Observer Problem Subclasses are hard-coded to notify a single instance of another class Solution Remove the subclass by making their superclass capable of notifying one or more instances of any class that implements an observer interface 49

Observer Pattern Intend Maintain a dependency between a central object (Subject) and multiple dependent objects (Observers) Motivation Decouple a subject from its observers Applicability When an instance must notify more than one receiver instance, E.g., when there are various views (Observers) on the same model instance (Subject) 50

Observer Pattern: Structure for (Observer o : observers) { o.update(); Subject attach(observer:observer) : void detach(observer:observer) : void notify() : void observers Observer update() : void ConcreteSubject state : State getstate() : State subject ConcreteObserver observerstate : State update() : void observerstate = subject.getstate(); 51

Example: TestResult 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 52

Example TestResult: Class Diagram TestResults textui.texttestresults ui.uitestresults Hard-Coded Notifications textui.testrunner ui.testrunner 53

Repl. Notific. with Observer: Mechanics Move custom behavior to receiver Extract Interface on a receiver to produce an observer interface Only 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 54

Move Custom Behavior to Receiver 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 ); 55

Extract Observer Interface 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) { 56

Make Receiver Implement the Interface package ui; public class TestRunner extends Frame implements TestListener {... package ui; public class UITestResult extends TestResult... protected TestListener runner; UITestResult(TestListener runner) { this.runner = runner; package textui; public class TextTestResult extends TestResult... protected TestListener runner; TextTestResult(TestListener runner) { this.runner = runner; 57

Pull Up Methods in Observers 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 { 58

Update Observers to Work with Subject 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); 59

Update Subject to Notify many Observers 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);... 60

Update Observer to Register with Subject package textui; public class TestRunner implements TestListener... protected TestResult createtestresult() { TestResult testresult = new TestResult(this); testresult.addobserver(this); return testresult; 61

Result: Refactoring TestResult TestResult TestListener textui.testrunner ui.testrunner 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 63

More Refactorings To Patterns A set of composite refactorings to refactor towards design patterns Joshua Kerievsky, Addison-Wesley, 2005 Some patterns online: http://www.informit.com/articles/article.aspx?p=1398607 64

Conclusions Refactoring to Patterns Encapsulates and simplifies logic Increases extensibility (interfaces, loose coupling) But, don t overdo it Only use a pattern when it (really) makes sense to use 65