CS计算机代考程序代写 algorithm TemplatePattern

TemplatePattern

COMP2511

Template Pattern

Prepared by
Dr. Ashesh Mahidadia

Template Pattern: Motivation and Intent

• “Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.

Template Method lets subclasses redefine certain steps of an algorithm

without changing the algorithm’s structure.” [GoF]

• A template Method defines the skeleton (structure) of a behavior (by implementing the

invariant parts).

• A template Method calls primitive operations, that could be implemented by sub classes

OR has default implementations in an abstract super class.

• Subclasses can redefine only certain parts of a behavior without changing the other parts

or the structure of the behavior.

COMP2511: Template Pattern 2

Template Pattern: Motivation and Intent

v Subclasses do not control the behavior of a parent class,

a parent class calls the operations of a subclass and not the other way around.

v Inversion of control:

v when using a library (reusable classes), we call the code we want to reuse.

v when using a framework (like Template Pattern), we write subclasses and

implement the variant code the framework calls.

v Template pattern implement the common (invariant) parts of a behavior once “and

leave it up to subclasses to implement the behavior that can vary.”[GoF, p326]

v Invariant behavior is in one class (localized)

COMP2511: Template Pattern 3

Template Pattern: Structure

• Abstract class defines a templateMethod() to

implement an invariant structure (behaviour)

• templateMethod() calls methods defined in the

abstract class (abstract or concrete) – like primitive1,

primitive2, etc.

• Default behaviour can be implemented in the

abstract class by offering concrete methods

• Importantly, sub classes can implement primitive

methods for variant behaviour

COMP2511: Template Pattern 4

Template Pattern: Structure

v “To reuse an abstract class effectively, subclass writers must understand which

operations are designed for overriding.” [GoF, p328]

v Primitive operations : operations that have default implementations or must be

implemented by sub classes.

v Final operations: concrete operations that cannot be overridden by sub classes.

v Hook operations: concrete operations that do nothing by default and can be redefined

by subclasses if necessary. This gives subclasses the ability to “hook into” the algorithm

at various points, if they wish; a subclass is also free to ignore the hook. (see the

example)

COMP2511: Template Pattern 5

Template Pattern: Example

COMP2511: Template Pattern 6

Template Pattern: Example

• From https://refactoring.guru/design-patterns/template-method
COMP2511: Template Pattern 7

Template Pattern: Example

COMP2511: Template Pattern 8

Template method

Step 1 Step 2

Step 3

Hook

Step 4

Abstract methods

Default method

Default methods

Template Pattern: Example

COMP2511: Template Pattern 9

Hook

Step 2

Part of Step 1

Template Pattern: Example

COMP2511: Template Pattern 10

Template Pattern: Example

From the Head First Design Book
COMP2511: Template Pattern 11

Template Pattern:
Example

COMP2511: Template Pattern 12
From the Head First Design Book

Template Pattern: Example

COMP2511: Template Pattern 13From the Head First Design Book

Template Pattern:
Example (hook)

COMP2511: Template Pattern 14From the Head First Design Book

Template Pattern:
Example (hook)

COMP2511: Template Pattern 15
From the Head First Design Book

Template Vs Strategy Patterns

• Template Method works at the class level, so it’s static.

• Strategy works on the object level, letting you switch behaviors at runtime.

• Template Method is based on inheritance: it lets you alter parts of an algorithm

by extending those parts in subclasses.

• Strategy is based on composition: you can alter parts of the object’s behavior by

supplying it with different strategies that correspond to that behavior at runtime.

COMP2511: Template Pattern 16