• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Listing >> OOCalculator >> OOCalc

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this thread for discussion.
Step 3
First cut, empty class:
<pre>
package calc;

public class OOCalc {
}
</pre>
Next step
[ March 13, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 5
<pre>
public class OOCalc {
private String result;

public String getResult() {
return result;
}

}
</pre>
Continue...
[ March 13, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 6 - Initialize result
<pre>
public class OOCalc {
private String result = "0";

public String getResult() {
return result;
}
}
</pre>
Continue...
[ March 13, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 7a - Refactor to double
<pre>
public class OOCalc {
private doubleString result = "0";

public doubleString getResult() {
return Double.NaNresult;
}
}
</pre>
Continue...
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 7b - Refactor: add body to stub
<pre>
public class OOCalc {
private double result;

public double getResult() {
return resultDouble.NaN;
}
}
</pre>
Continue...
[ March 13, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 8 - Stubs for testAddition()
<pre>
package calc;

public class OOCalc {
private double result;
public double getResult() {
return result;
}

public void in(String operator) {
}

public void in(double operand) {
}

}
</pre>
-------
And implementing:
<pre>
package calc;

import java.util.Stack;

public class OOCalc {
private double result;
private Stack operandStack = new Stack();
private String operator;


public double getResult() {
if ("+".equals(operator)) {
double op1 = ((Double)operandStack.pop()).doubleValue();
double op2 = ((Double)operandStack.pop()).doubleValue();
result = op1 + op2;
}


return result;
}

public void in(String operator) {
this.operator = operator;
}

public void in(double operand) {
operandStack.push(new Double(operand));
}
}
</pre>
Continue...
[ March 24, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the interest of brevity (and keeping my sanity... UBB), I'll just post the snippets of changed code from now on. I'll refresh the complete listing at appropriate intervals which should be fewer and farther apart.
Step 9 - Refactoring
1. Rename field "operator" to "operation" to make the code more gramatically correct in describing what it does.
2. Apply the Extract Method refactoring to the addition operation. We will call the new method performOperation.
<pre>
class OOCalc {
...
private String operatoroperation;
public static final String ADD = "+";
...
public double getResult() {
if ("+".equals(operator)) {
double op1 = ((Double)operandStack.pop()).doubleValue();
double op2 = ((Double)operandStack.pop()).doubleValue();
result = (op1 + op2);
}
return result;


if (operation != null)
return performOperation();
else
return 0.0;

}

public void in(String operatoroperation) {
this.operation = operation;
}

private double performOperation() {
if (operation == ADD) {
double op1 = ((Double)operandStack.pop()).doubleValue();
double op2 = ((Double)operandStack.pop()).doubleValue();
return (op1 + op2);
}
return 0.0;
}

}
</pre>
3. Refactor OOCalcTest
Continue...
[ March 26, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 10 - SUB and MUL implemented
<pre>
public class OOCalc ...
public static final String ADD = "+";
public static final String SUB = "-";
public static final String MUL = "*";

...
public double getResult() {
if (operation == ADD) {
...
return (op1 + op2);
} else if (operation == SUB) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op2 - op1);
} else if (operation == MUL) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op1 * op2);
} else {
return 0.0;
}

}
// deleted performOperation()
</pre>
Notice I've backpedalled a bit and moved the calculations back to getResult() and deleted the performOperation. I decided to do this because I wasn't getting a whole lot from the previous refactoring. Now I can see the duplication and I have a better view of where I actually want to go.
Continue...
[ March 29, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 10 (continued) - extract constants to another class. Let's use Ilja's class name instead: Operation
<pre>
// Operation.java

package calc;
public class Operation {
public static final String ADD = "+";
}


// OOCalc.java
package calc;
public class OOCalc ...
public static final String ADD = "+";
public static final String SUB = "-";
public static final String MUL = "*";
...
public double getResult() {
if (operation == Operation.ADD) {
...
</pre>
Then update OOCalcTest.testAddition:
<pre>
calc.in(OOCalcOperation.ADD);
</pre>
Compile and test. Still green bar.
After following the same steps to move SUB and MUL we have this:
<pre>
// Operation.java
package calc;
public class Operation {
public static final String ADD = "+";

public static final String SUB = "-";
public static final String MUL = "*";
}


// OOCalc.java
package calc;
public class OOCalc ...
public static final String SUB = "-";
public static final String MUL = "*";
...
public double getResult() {
if (operation == Operation.ADD) {
...
} else if (operation == Operation.SUB) {
...
} else if (operation == Operation.MUL) {
...
</pre>
[ March 29, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Next I see that Operation is more of a Strategy, with ADD, SUB, and MUL being concrete strategies. I'll head the refactoring that way then.
I'll make all Operations recognize the perform() message. Looking at the duplication pattern in OOCalc again, it looks like perform() will need to take a Stack as a parameter. So I make Operation an abstract class and add perform as an abstract method:
<pre>
// Operation.java
package calc;
import java.util.Stack;
public abstract class Operation {
...
public abstract double perform(Stack operandStack);
}
</pre>

Then create the concrete subclass and copy the addition calculation logic from OOCalc.getResults().
<pre>
// Addition.java
package calc;
import java.util.Stack;
public class Addition extends Operation {
public double perform(Stack operandStack) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op1 + op2);
}
}
</pre>
So now I can change the declaration for Operation.ADD to be:
<pre>
public static final OperationString ADD = new Addition()"+";
</pre>
[ March 29, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now to make some adjustments to OOCalc. After changing the type of ADD to Operation, I need to overload OOCalc.in() to accept an Operation. I'll keep it in a new private member field called _op (now adopting the convention that Martin Fowler uses in his book on "Refactoring").
<pre>
public class OOCalc {
...
private Operation _op;
private String operation;
...
public double getResult() {
if (_operation == Operation.ADD) {
// delete addition calculation
return _op.perform(operandStack)(op1 + op2);
} else
// separating this old stuff for now
if (operation == Operation.SUB) {
...
}
public void in(Operation op) {
_op = op;
}

</pre>
No need to change OOCalcTest since our changes don't affect the line
<pre>calc.in(Operation.ADD);</pre>
Compile and test. Still Green bar. Whew!
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following the steps for addition, we do the same for subtraction and multiplication. After that we have the following code:
<pre>
//Operation.java
package calc;
import java.util.Stack;
public abstract class Operation {
public static final Operation ADD = new Addition();
public static final Operation SUB = new Subtraction();
public static final Operation MUL = new Multiplication();

public abstract double perform(Stack operandStack);
}

//Addition.java
package calc;
import java.util.Stack;
public class Addition extends Operation {
public double perform(Stack operandStack) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op1 + op2);
}
}

//Subtraction.java
package calc;
import java.util.Stack;
public class Subtraction extends Operation {
public double perform(Stack operandStack) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op2 - op1);
}
}

//Multiplication.java
package calc;
import java.util.Stack;
public class Multiplication extends Operation {
public double perform(Stack operandStack) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op1 * op2);
}
}

//OOCalc.java
package calc;
import java.util.Stack;
public class OOCalc {
private Stack operandStack = new Stack();
private Operand _op;
private String operation;

public double getResult() {
if (_op == Operation.ADD) {
return _op.perform(operandStack);
} else if (_op == Operation.SUB) {
return _op.perform(operandStack);
} else if (_op == Operation.MUL) {
return _op.perform(operandStack);
} else
return 0.0;

}

public void in(String operation) {
this.operation = operation;
}

public void in(double operand) {
operandStack.push(new Double(operand));
}

public void in(Operation op) {
_op = op;
}
}
</pre>
[ March 29, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost done... We can now eliminate the remnants of the old String operation and arrive at the polymorphic behavior that we so wanted to have in the beginning. In addition, we get to add the old "guard condition" that we also wanted.
<pre>//OOCalc.java
package calc;
import java.util.Stack;
public class OOCalc {
private Stack operandStack = new Stack();
private Operand _op;
private String operation;

public double getResult() {
// guard condition
if (_op == null)
return 0.0;

return _op.perform(operandStack);


// no need for this anymore:
if (_op == Operation.ADD) {
return _op.perform(operandStack);
} else if (_op == Operation.SUB) {
return _op.perform(operandStack);
} else if (_op == Operation.MUL) {
return _op.perform(operandStack);
} else
return 0.0;

}

public void in(String operation) {
this.operation = operation;
}

public void in(double operand) {
operandStack.push(new Double(operand));
}

public void in(Operation op) {
_op = op;
}
}
</pre>
[ March 29, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing to take care of. OK, so we've got OOCalc calling Operations polymorphically now. We're still violating the OCP in Operation because if we need to add an operation, we need to create a subclass and modify Operation. It would be nice if we could eliminate that duplicate work. Well, we can and here's how:
Notice that we don't really need multiple instances of concrete Operations. One instance of each will do. So, I'll turn all the concrete Operations into Singletons. I'll add a private constructor and a static getInstance() method to each concrete subclass. After doing this, I can eliminate the constants defined in Operation.
How does this affect my other code? Well, only the client code, as represented by OOCalcTest, will be affected. So instead of invoking the in(Operation) method with Operation.ADD we'll just invoke it with Addition.getInstance(), Subtraction.getInstance(), and Multiplication.getInstance(). A little more typing here maybe but look what happens: all we have to do now if we add a new operation is to create a new subclass of Operation! Here are the changes and the implementation of the Division operation:
<pre>
//Operation.java
package calc;
import java.util.Stack;
public abstract class Operation {
public static final Operation ADD = new Addition();
public static final Operation SUB = new Subtraction();
public static final Operation MUL = new Multiplication();

public abstract double perform(Stack operandStack);
}

//Addition.java
package calc;
import java.util.Stack;
public class Addition extends Operation {
private static Addition _instance;

public static Operation getInstance() {
if (_instance == null)
_instance = new Addition();
return _instance;
}

private Addition() {
super();
}


public double perform(Stack operandStack) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op1 + op2);
}
}

//Subtraction.java
package calc;
import java.util.Stack;
public class Subtraction extends Operation {
private static Subtraction _instance;

public static Operation getInstance() {
if (_instance == null)
_instance = new Subtraction();
return _instance;
}

private Subtraction() {
super();
}


public double perform(Stack operandStack) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op2 - op1);
}
}

//Multiplication.java
package calc;
import java.util.Stack;
public class Multiplication extends Operation {
private static Multiplication _instance;

public static Operation getInstance() {
if (_instance == null)
_instance = new Multiplication();
return _instance;
}

private Multiplication() {
super();
}


public double perform(Stack operandStack) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op1 * op2);
}
}

//Division.java

package calc;
import java.util.Stack;
public class Division extends Operation {
private static Division _instance;

public static Operation getInstance() {
if (_instance == null)
_instance = new Division();
return _instance;
}

private Division() {
super();
}

public double perform(Stack operandStack) {
double op1 = ((Double)(operandStack.pop())).doubleValue();
double op2 = ((Double)(operandStack.pop())).doubleValue();
return (op2 / op1);
}
}


//OOCalc.java
package calc;
import java.util.Stack;
public class OOCalc {
private Stack operandStack = new Stack();
private Operation _op;

public double getResult() {
if (_op == null)
return 0.0;

return _op.perform(operandStack):
}

public void in(double operand) {
operandStack.push(new Double(operand));
}

public void in(Operation op) {
_op = op;
}
}
</pre>
Then make the appropriate changes to OOCalcTest
Compile and test. Green bar
[ March 29, 2002: Message edited by: Junilu Lacar ]
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic