Port of schuchert.wikispaces.com


Rpn_Calculator.WithRefactoringAndPatterns

Rpn_Calculator.WithRefactoringAndPatterns

Describe the calculator (unlimited stack BTW, but a stack of 4 is a nice option) Ask them what they’d like to see first Get an idea of what they want for a system boundary

Then I start demonstrating TDD in the raw. I don’t describe the steps, just demo it. I have them follow me and keep up. I do this for at least:

It is important to have operators with different arity, (binary, unary, for example)

Next, I have a discussion about the “thickness” of the API. All along, I’m asking them what the interface should be and then coding it in a test. Typically you’ll have:

Note, they need tests for things like:

As you make this work, you’ll notice feature envy in handling the stack.

Fix that problem.

Notice how the API is getting wide? Violation of Open/Closed. How can we fix? I guide them to:

Long Method/SRP violation, execute method is both selecting what to do and doing it. Problem, as you add more features, you’ll keep adding more and more code. Discuss Strategy Pattern. Implement Plus as strategy (do not use an interface YET)

You have strategies. You have duplication. You have too much responsibility in calculate(“”) method.

Make all strategies implement interface (extract interface). Change long method… calculate method:

OK, there’s still duplication in plus and minus. We can introduce the template method pattern. The abstract BinaryOperator class:

Now, demonstrate OCP: create a factory that uses words like plus instead of +. Plug it in to

This is a good stopping point, but you can do much more.

I have them implement two functions:


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.