Docsity

Refactoring Techniques: Extract, Replace, Move Method & Overdraft Charge Calculation, Study notes of Computer Science

Partial preview of the text

Download Refactoring Techniques: Extract, Replace, Move Method & Overdraft Charge Calculation and more Study notes Computer Science in PDF only on Docsity!

18 - Flyweight Pattern & Refactoring

(with thanks to Frank Tip)

CMPSCI 220 (291A)

Programming Methodology

Spring 2009

Flyweight Pattern

 Use when one instance of a class can serve as

many “virtual instances”

 Reduces number of object instances, saves space

 At runtime

 In stable storage (database, file system, etc.)

 “Centralizes state for many ‘virtual’ objects”

 HFDP means this one way

 Alternative interpretation probably more common

 “Used when a class has many instances, and they

can all be controlled identically” - HFDP

 Again, a broader interpretation probably better

More Typical Flyweight Example

Tree

xCoord

yCoord

age

//large amount

//of data common

//to all (or some)

//trees

Tree

xCoord

yCoord

age

myInfo

TreeInfo

//large amount

//of data common

//to all (or some)

//trees

• One single,

immutable TreeInfo object

  • Each Tree instance maintains its own copy of common data - Each Tree instance points to one shared copy of common data

What is Refactoring?

 Process of applying transformations

(refactorings) to a program

 Goal is improving its design while:

 Keeping program readable, understandable, and

maintainable

 By eliminating small problems early, avoid

big trouble later

Reasons for Refactoring

 Change to requirements necessitates different

design

 Desire to make design more flexible

 Design patterns are often target for refactoring

 Programmer sloppiness

 “Cut and paste” introduction of a new method

 Programmers don’t come up with right design

the first time

 Sometimes intentionally: “agile development”

methods, like XP, call for continuous refactoring

Refactoring:

Extract Method

 Refactoring for extracting a code fragment

into a method

 Goals:

 Enable reuse

 Avoid cut-and-paste programming

 Make longer methods shorter and more

readable

Extract Method: How?

 Scan extracted code for variables local in

scope to source method -- or let Eclipse!

 Only used in target method: declare in target

method

 Used in source method and read but not

assigned in target method: pass in as parameters

 Used in source method and assigned in target

method and new value used in source method:

pass in as parameters and return changed value

 At most one variable can be modified

Example: Extract Method

void printOwing(){ Enumeration e = _orders.elements(); double outstanding = 0.0; // print banner System.out.println("*************************"); System.out.println("***** Customer Owes *****"); System.out.println("*************************"); // calculate outstanding while (e.hasMoreElements()){ Order each = (Order)e.nextElement(); outstanding += each.getAmount(); } // print details System.out.println("name:" + _name); System.out.println("amount" + outstanding); }

Example: Extract Method (3)

void printOwing(){ Enumeration e = _orders.elements(); double outstanding = 0.0;

printBanner();

// calculate outstanding while (e.hasMoreElements()){ Order each = (Order)e.nextElement(); outstanding += each.getAmount(); } printDetails(outstanding);

public void printDetails(double outstanding) { System.out.println("name:" + _name); System.out.println("amount" + outstanding); } public void printBanner() {... }

Example: Extract Method (4)

void printOwing(){ Enumeration e = _orders.elements(); double outstanding = 0.0;

printBanner();

outstanding = getOutstanding(e, outstanding); printDetails(outstanding); } public double getOutstanding(Enumeration e, double outstanding) { while (e.hasMoreElements()){ Order each = (Order)e.nextElement(); outstanding += each.getAmount(); } return outstanding;

public void printDetails(double outstanding) {... } public void printBanner() {... }

Example: Extract Method (6)

void printOwing(){

printBanner();

double outstanding = getOutstanding(); printDetails(outstanding); } public double getOutstanding() { Enumeration e = _orders.elements(); double result = 0.0; while (e.hasMoreElements()){ Order each = (Order)e.nextElement(); result += each.getAmount(); } return result;

public void printDetails(double outstanding) {... } public void printBanner() {... }

Variant -- Computed Previous Value

void printOwing(double previousAmount) { Enumeration e = _orders.elements(); double outstanding = previousAmount * 1.2;

printBanner();

// calculate outstanding while (e.hasMoreElements()){ Order each = (Order)e.nextElement(); outstanding += each.getAmount(); } printDetails(outstanding); } public void printDetails(double outstanding) {... } public void printBanner() {... }

Extract Method: Considerations

 If the extracted code changes multiple local

variables:

 Pick different code to extract

 Aim for multiple methods, each with one return value

 Try applying Replace Temp with Query

 In essence, recomputing the value each time

 Not always (directly) possible

 Performance issues

 Or use Replace Method with Method Object

 Heavy weight -- turns method into its own object

Problem Case

void printOwing(){ Enumeration e = _orders.elements(); double outstanding = 0.0; int count = 0;

printBanner();

// calculate outstanding while (e.hasMoreElements()){ Order each = (Order)e.nextElement(); outstanding += each.getAmount(); count = count + 1; } printDetails(outstanding, count);

public void printDetails(double outstanding, int count) {... } public void printBanner() {... }