Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Software Engineering Refactoring Patterns: Lecture 12 Study Guide by Sharif University, Slides of Software Engineering

An overview of refactoring patterns in software engineering as discussed in lecture 12 at sharif university of technology. Topics include dealing with generalization through techniques like pull up constructor body, extract subclass/superclass, extract interface, collapse hierarchy, form template method, replace inheritance with delegation, replace delegation with inheritance, tease apart inheritance, and convert procedural design to objects. Fowler's 'refactoring: improving the design of existing code' is recommended for further study.

Typology: Slides

2011/2012

Uploaded on 02/19/2012

hester
hester 🇮🇷

4.5

(13)

85 documents

1 / 17

Toggle sidebar

Related documents


Partial preview of the text

Download Software Engineering Refactoring Patterns: Lecture 12 Study Guide by Sharif University and more Slides Software Engineering in PDF only on Docsity!

Patterns in Software Engineering Lecturer: Raman Ramsin^ Lecture 12^ Refactoring Patterns

Part 3

1

Sharif University of Technology

Part

3

Patterns in Software Engineering – Lecture 12 Dealing with Generalization:

Pull Up Constructor Body

Dealing

with Generalization:

Pull

Up Constructor Body

„^

Pull Up Constructor Body^ ^

You have constructors on subclasses with mostly identical bodies. Create a superclass constructor; call this from the subclass methods.

2

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Dealing with Generalization:

Extract Subclass/Superclass

Dealing

with Generalization:

Extract

Subclass/Superclass

„^

Extract Subclass^

A^

class has features that are used only in some instances.

y

^ Create a subclass for that subset of features. Extract Superclass

„^

Extract

Superclass

^ You have two classes with similar features. ^ C e te

pe

l

nd mo e the

ommon fe t

e

to the

^ Create a superclass and move the common features to thesuperclass.

3

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Dealing with Generalization:

Extract Interface

Dealing

with Generalization:

Extract

Interface

„^

Extract Interface^ ^

Several clients use the same subset of a class's interface, or two classes havepart of their interfaces in common. Extract the subset into an interface.

4

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Dealing with Generalization:

Collapse Hierarchy

Dealing

with Generalization:

Collapse

Hierarchy

„^

Collapse Hierarchy „^

Collapse

Hierarchy

^ A superclass and subclass are not very different. ^ Merge them together.

5

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Dealing with Generalization:

Form Template Method

Dealing

with Generalization:

Form

Template Method

„^

Form Template Method^

You have two methods in subclasses that perform similarsteps in the same order, yet the steps are different.

p^

, y

p

^ Get the steps into methods with the same signature, so that ^ Get the steps into methods with the same signature, so thatthe original methods become the same. Then you can pullthem up.

6

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Dealing with Generalization:

Form Template Method

Dealing

with Generalization:

Form

Template Method

7

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Dealing with Generalization:

Replace Inheritance with Delegation

g^

p^

g

„^

Replace Inheritance with Delegation p^

g

^ A subclass uses only part of a superclass’s interface or does not want to inheritdata. ^ Create a field for the superclass

adjust methods to delegate to the superclass

^ Create a field for the superclass, adjust methods to delegate to the superclass,and remove the subclassing.

8

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Dealing with Generalization:

Replace Delegation with Inheritance

g^

p^

g

„^

Replace Delegation with Inheritance „^

Replace

Delegation with Inheritance

^ You're using delegation and are often writing many simple delegations for theentire interface. ^ M k

th

d l

ti^

l

b l^

f th

d l

t

^ Make the delegating class a subclass of the delegate.

9

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Big Refactorings:

Tease Apart Inheritance

Big

Refactorings:

Tease

Apart Inheritance

„^

Tease Apart InheritanceTease

Apart Inheritance

^ You have an inheritance hierarchy that is doing two jobs at ^ You

have an inheritance hierarchy that is doing two jobs at

once.

Create two hierarchies and use delegation to invoke onefrom the other.

10

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Big Refactorings:

Tease Apart Inheritance

Big

Refactorings:

Tease

Apart Inheritance 11

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Big Refactorings:

Convert Procedural Design to Objects

Big

Refactorings:

Convert

Procedural Design to Objects

„^

Convert Procedural Design to Objects „^

Convert

Procedural Design to Objects

^ Yo

ha e code

itten in a p oced

al st le

^ Yo

u have code written in a procedural style.

T^

th^

d t

d^

i t

bj^

t^

b^

k^

th^

b h

i

^ T

urn the data records into objects, break up the behavior,and move the behavior to the objects.

12

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Big Refactorings:

Convert Procedural Design to Objects

Big

Refactorings:

Convert

Procedural Design to Objects 13

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Big Refactorings:

Separate Domain from Presentation

Big

Refactorings:

Separate

Domain from Presentation

„^

Separate Domain from Presentation „^

Separate

Domain from Presentation

^ You have GUI classes that contain domain logic. ^ Separate the domain logic into separate domain classes.

14

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Big Refactorings:

Extract Hierarchy

Big

Refactorings:

Extract

Hierarchy

„^

Extract Hierarchy^

You have a class that is doing too much work, at least inpa t th o gh man

conditional statements

part through many conditional statements.C^

t^

hi^

h^

f^

l^

i^

hi h

h^

b l

^ C

reate a hierarchy of classes in which each subclassrepresents a special case.

15

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 Big Refactorings:

Extract Hierarchy

Big

Refactorings:

Extract

Hierarchy 16

Sharif University of Technology

Patterns in Software Engineering – Lecture 12 R fReference „ Fowler, M.,

Refactoring: Improving the Design of Existing Code,

Addison-Wesley, 1999.

17

Sharif University of Technology