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

Refactoring Patterns in Software Engineering: Lecture 11, Slides of Software Engineering

The refactoring patterns for organizing data and simplifying conditional expressions in software engineering, as presented in lecture 11 of the patterns in software engineering course at sharif university of technology. The patterns include self encapsulate field, replace data value with object, replace array with object, duplicate observed data, encapsulate field, encapsulate collection, replace subclass with fields, decompose conditional, consolidate conditional expression, replace nested conditional with guards, and replace conditional with polymorphism. The document also mentions the introduce null object, separate query from modifier, parameterize method, replace parameter with explicit methods, preserve whole object, and replace parameter with method patterns, as well as the reference book 'refactoring: improving the design of existing code' by martin fowler.

Typology: Slides

2011/2012

Uploaded on 02/19/2012

hester
hester 🇮🇷

4.5

(13)

85 documents

1 / 20

Toggle sidebar

Related documents


Partial preview of the text

Download Refactoring Patterns in Software Engineering: Lecture 11 and more Slides Software Engineering in PDF only on Docsity!

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

Part 2

1

Sharif University of Technology

Part

2

Patterns in Software Engineering – Lecture 11 Organizing Data: Self Encapsulate Field Organizing Data: Self Encapsulate Field „ Self Encapsulate Field

You are accessing a field directly, but the coupling to the field is becomingawkward.

Create getting and setting methods for the field and use only those to accessthe field.

2

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Organizing Data: Replace Data Value with Object Organizing Data: Replace Data Value with Object „ Replace Data Value with Object „ Replace Data Value with Object

You have a data item that needs additional data or behavior.

Turn the data item into an object.

3

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Organizing Data: Replace Array with Object Organizing Data: Replace Array with Object „ Replace Array with Object „ Replace Array with Object

You have an array in which certain elements mean different things.

Replace the array with an object that has a field for each element.

4

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Organizing Data: Duplicate Observed Data Organizing Data: Duplicate Observed Data „ Duplicate Observed Data

You have domain data available only in a GUI control, and domain methods need

You have domain data available only in a GUI control, and domain methods need access.

Copy the data to a domain object. Set up an observer to synchronize the two piecesof data.

5

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Organizing Data: Encapsulate Field Organizing Data: Encapsulate Field „ Encapsulate Field

There is a public field.

Make it private and provide accessors

Make it private and provide accessors.

6

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Organizing Data: Encapsulate Collection Organizing Data: Encapsulate Collection „ Encapsulate Collection

A method returns a collection.

Make it return a read-only view and provide add/remove methods

Make it return a read-only view and provide add/remove methods.

7

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Organizing Data: Replace Subclass with Fields Organizing Data: Replace Subclass with Fields „ Replace Subclass with Fields

You have subclasses that vary only in methods that return constant data

You have subclasses that vary only in methods that return constant data.

Change the methods to superclass fields and eliminate the subclasses.

8

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Simplifying Conditional Expressions: Decompose Conditional p y g p p „ Decompose Conditional „ Decompose Conditional

You have a complicated conditional (if-then-else) statement.

Extract methods from the condition, then part, and else parts.

9

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Simplifying Conditional Expressions: Consolidate Conditional Expression p y g p p „ Consolidate Conditional Expression „ Consolidate Conditional Expression

You have a sequence of conditional tests with the same result.

Combine them into a single conditional expression and extract it.

10

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Simplifying Conditional Expressions:

Replace Nested Conditional with Guards

p y g p

p

„ Replace Nested Conditional with Guard Clauses

A method has conditional behavior that does not make clear the normal path of p execution.

Use guard clauses for all the special cases.

11

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Simplifying Conditional Expressions:

Replace Conditional with Polymorphism

p y g p

p

y

p

„ Replace Conditional with Polymorphism

You have a conditional that chooses different behavior depending on the type of an p g yp object.

Move each leg of the conditional to an overriding method in a subclass. Make theoriginal method abstract. g

12

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Simplifying Conditional Expressions: Introduce Null Object p y g p j I d N ll Obj „ I ntroduce Null Object

You have repeated checks for a null value.

Replace the null value with a null object. p j

13

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Making Method Calls Simpler: Separate Query from Modifier g p p Q y „ Separate Query from Modifier

You have a method that returns a value but also changes the state of an object.

Create two methods one for the query and one for the modification

Create two methods, one for the query and one for the modification.

14

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Making Method Calls Simpler: Parameterize Method g p „ Parameterize Method

Several methods do similar things but with different values contained in themethod body.method body.

Create one method that uses a parameter for the different values.

15

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Making Method Calls Simpler:

Replace Parameter with Explicit Methods

g p

p

p

„

Replace Parameter with Explicit Methods

You have a method that runs different code depending on the values of an p g enumerated parameter.

Create a separate method for each value of the parameter.

16

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Making Method Calls Simpler: Preserve Whole Object g p j „ Preserve Whole Object „ Preserve Whole Object

You are getting several values from an object and passing these values asparameters in a method call.

S d th h l bj t i t d

Send the whole object instead.

17

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Making Method Calls Simpler: Replace Parameter with Method g p p „ Replace Parameter with Method „ Replace Parameter with Method

An object invokes a method, then passes the result as a parameter for amethod. The receiver can also invoke this method.

Remove the parameter and let the receiver invoke the method

Remove the parameter and let the receiver invoke the method.

18

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 Making Method Calls Simpler: Introduce Parameter Object g p j „ Introduce Parameter Object „ Introduce Parameter Object

You have a group of parameters that naturally go together.

Replace them with an object.

19

Sharif University of Technology

Patterns in Software Engineering – Lecture 11 R fR eference „ Fowler, M., Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999.

20

Sharif University of Technology