Reader small image

You're reading from  TypeScript Design Patterns

Product typeBook
Published inAug 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781785280832
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Vilic Vane
Vilic Vane
author image
Vilic Vane

Vilic Vane is a JavaScript engineer with over 8 years of experience in web development. He started following the TypeScript project since it went public, and hes also a contributor of the project. He is now working at Ruff, a startup company building an IoT platform that runs JavaScript on embedded devices.
Read more about Vilic Vane

Right arrow

Template Method Pattern


When we are talking about subclassing or inheriting, the building is usually built from the bottom up. Subclasses inherit the basis and then provide more. However, it could be useful to reverse the structure sometimes as well.

Consider Strategy Pattern which defines the outline of a process and has interchangeable algorithms as strategies. If we apply this structure under the hierarchy of classes, we will have Template Method Pattern.

A template method is an abstract method (optionally with default implementation) and acts as a placeholder under the outline of a larger process. Subclasses override or implement related methods to modify or complete the behaviors. Imaging the skeleton of a TextReader, we are expecting its subclasses to handle text files from different storage media, detect different encodings and read all the text. We may consider a structure like this:

The TextReader in this example has a method readAllText that reads all text from a resource by two...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
TypeScript Design Patterns
Published in: Aug 2016Publisher: PacktISBN-13: 9781785280832

Author (1)

author image
Vilic Vane

Vilic Vane is a JavaScript engineer with over 8 years of experience in web development. He started following the TypeScript project since it went public, and hes also a contributor of the project. He is now working at Ruff, a startup company building an IoT platform that runs JavaScript on embedded devices.
Read more about Vilic Vane