Saturday, May 30, 2020

Glimmer Calculator

Glimmer Calculator is a desktop GUI sample application developed with Glimmer test-first following TDD (Test Driven Development) and leveraging data-binding and the MVP (Model View Presenter) architectural pattern. It additionally employs the Facade and Command Design Patterns.

















It is packaged as both a Mac App DMG file (to launch as an icon) and a Ruby gem (to launch as a terminal command).




























The most interesting thing about the project is most certainly the Command Pattern that was stumbled upon through test-driven-design (aka TDD / test driven development).

The way it works is every calculator button press represents a command. In fact, the Presenter class simply has a "press" method taking the label of the button pressed as an argument and a "result" method to show the numeric result on the calculator screen.

Otherwise, all presses are routed to commands whereby each command knows its text keywords.

For example, if you would like a Number command to be invoked, you press any number from 0-9. The Number command is then summoned to handle it and execute the outcome of pressing a number (e.g. appending 1 to 2 to make number 21).

Here is the Number command code. Very simple. Simply specifies its keywords on top and then handles execution by updating number1, number2, and operation variables to be used later.


Here is another example of a command, the Equals command. Also, very simple. It specifies its keywords on top, allowing both the '=' button and "\r" carriage return keyboard key (ENTER) to trigger it. In its execution, it simply uses the operation attribute set from earlier to calculate number1 and number2, be it addition, subtraction, multiplication, or division.
Operations are a special subtype of Command since they not only involve execution to display a result (usually they don't change the calculator screen), but also calculation later on when the Equals command invokes them. Here is the Add command.
As you can see, the application is very well factored and designed thanks to TDD (test-driven-development) facilitated by Glimmer's data-binding and the use of the MVP pattern (Model-View-Presenter)

Last but not least, Glimmer Calculator is built as a Glimmer Custom Shell Gem (reusable window) using the `glimmer scaffold:custom_shell_gem[name]` Scaffolding command. In other words, it behaves as both a standalone application and a reusable window to be included in other applications that need a calculator. As such, it may be launched as an executable (e.g. Mac App) and a terminal command (`calculator` command from installing the `glimmer-cs-calculator` Ruby gem).

I hope you've enjoyed learning about the new Glimmer Calculator sample desktop GUI app. What will you build with Glimmer?

Happy Glimmering!

No comments: