Skip to content

Latest commit

 

History

History
58 lines (34 loc) · 3.45 KB

File metadata and controls

58 lines (34 loc) · 3.45 KB

Template Method

Scenario

Multiglom Media, vendor of archiving solutions, has decided to support encoded (encrypted) Storage of field values (String).

Therefore they want to create a family of decoders to support different string encoding algorithms.

Requirements Overview

The purpose of the String decoder is encrypting and decrypting Strings, supporting several algorithms.

Main Features

  • Encode given String.
  • Decode given encoded String.
  • Check validity of encoded String

Quality Goals

Table 1. Quality Goals

No. Quality Motivation
1 Performance The performance impact shall be minimal, for certain scenarios it shall be possible to define a dummy decoder not impacting processing speed at all.
2 Extendability It shall be easy to add further decoders.
3 Testability The design shall encourage testing decoupled from the rest of the application.

Choice of Pattern

In this scenario we want to apply the Template Method Pattern to define the skeleton of an algorithm in an operation, deferring some steps to subclasses (GoF).

Test

In the defined scenario we assume that the String Codec has some general code. Thus there is an abstract base class AbstractTemplateMethodStringCodec with this common logic. The encryption and decryption shall happen in concrete subclasses, encode, decode as well as checkValid are Template Methods. The ExampleTemplateMethodStringCodec is currently the only concrete implementation.

You can see this depicted below on the left side.

Test

There is a more interesting example of Template Method in the util-package. You can see the AbstractConsoleServer with its subclasses above on the right side. I use external processes in some pattern examples to make them a little more interesting. The AbstractConsoleServer only provides basic functionality and declares Template Methods for the remaining logic. The same is true for the AbstractThreadedSocketServer which adds the magic of threading. Finally, the EchoServer is a concrete subclass that implements the remaining Template Methods to be functional.

Try it out!

Open TemplateMethodTest.java to start playing with this pattern. By setting the log-level for this pattern to DEBUG in logback.xml you can watch the pattern working step by step. Be aware that I have redirected the output of the EchoServer (running in the background) to the log output for better visualization.

Remarks

  • Template method is an excellent way to realize abstraction not appearing "over-engineered". The classes get smaller with better focus.
  • Template method can potentially impede testing as it uses inheritance: You implicitly run the construction process of the base class. So, it is a good idea to avoid any heavy-weight actions in the constructor chain.
  • The MuhaiGenerator implements Template Method to allow subclasses to change details of the MUHAI-creation process without having to re-implement the whole generator.

References

  • (GoF) Gamma, E., Helm, R., Johnson, R., Vlissides, J.: Design Patterns – Elements of Reusable Object-Oriented Software. Addison-Wesley (1995).