Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 20
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.
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.
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.
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.
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.
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.
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.
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.
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.
Patterns in Software Engineering – Lecture 11 Simplifying Conditional Expressions:
p y g 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.
Patterns in Software Engineering – Lecture 11 Simplifying Conditional Expressions:
p y g 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
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
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.
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.
Patterns in Software Engineering – Lecture 11 Making Method Calls Simpler:
g 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.
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.
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.
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.
Patterns in Software Engineering – Lecture 11 R fR eference Fowler, M., Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999.