Step 3

First cut, empty class:

<pre>

package calc;

public class OOCalc {

}

</pre>

Next step

[ March 13, 2002: Message edited by: Junilu Lacar ]

*Practice only makes habit, only perfect practice makes perfect.
So, practice mindfully. Practice doing the right things and doing things right. *— Junilu

[How to Ask Questions] [How to Answer Questions]

<pre>

public class OOCalc {

private String result;

public String getResult() {

return result;

}

}

</pre>

Continue...

[ March 13, 2002: Message edited by: Junilu Lacar ]

*Practice only makes habit, only perfect practice makes perfect.
So, practice mindfully. Practice doing the right things and doing things right. *— Junilu

[How to Ask Questions] [How to Answer Questions]

<pre>

public class OOCalc {

private String result = "0";

public String getResult() {

return result;

}

}

</pre>

Continue...

[ March 13, 2002: Message edited by: Junilu Lacar ]

*Practice only makes habit, only perfect practice makes perfect.
So, practice mindfully. Practice doing the right things and doing things right. *— Junilu

[How to Ask Questions] [How to Answer Questions]

<pre>

public class OOCalc {

private double

public double

return Double.NaN

}

}

</pre>

Continue...

<pre>

public class OOCalc {

private double result;

public double getResult() {

return result

}

}

</pre>

Continue...

[ March 13, 2002: Message edited by: Junilu Lacar ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

<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 ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

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

public static final String ADD = "+";

...

public double getResult() {

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

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 ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

<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 ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

<pre>

// Operation.java

package calc;

public class Operation {

public static final String ADD = "+";

}

// OOCalc.java

package calc;

public class OOCalc ...

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(

</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 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 ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

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 Operation

</pre>

[ March 29, 2002: Message edited by: Junilu Lacar ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

<pre>

public class OOCalc {

...

private Operation _op;

private String operation;

...

public double getResult() {

if (_op

// delete addition calculation

return _op.perform(operandStack)

} 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!

<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 ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

<pre>//OOCalc.java

package calc;

import java.util.Stack;

public class OOCalc {

private Stack operandStack = new Stack();

private Operand _op;

public double getResult() {

// guard condition

if (_op == null)

return 0.0;

return _op.perform(operandStack);

// no need for this anymore:

}

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 ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

**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 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 ]

So, practice mindfully. Practice doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

It is sorta covered in the JavaRanch Style Guide. |