Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Template Pattern

Template Pattern

In the Template Pattern, an abstract class publicly defines the way/template for executing its methods. Its subclasses can override method implementations as needed, but the call will be made in the way defined in the abstract class. This type of design pattern belongs to the behavioral patterns.

Introduction

Intent

Defines the skeleton of an algorithm in a parent class and allows subclasses to redefine certain specific steps of the algorithm without changing its structure.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

Disadvantages

Usage recommendation

Notes

Main roles included

- Defines template methods and some abstract methods or concrete methods. - Inherits from the abstract parent class and implements abstract methods. - Defined in the abstract parent class and can be overridden by subclasses to affect the behavior of the template method. - Use the abstract parent class and concrete subclasses without worrying about the details of template methods.

Create a class that implements

We will create an abstract class that defines operations, where the Game template method is set to final so that it cannot be overridden.Cricket and Football are concrete classes that extend Game the abstract class and override its methods.

TemplatePatternDemo, and our demo class uses the Game to demonstrate the use of the Template Pattern.

Step 1

Create an abstract class whose template method is set to final.

Game.java

public

abstract

class

Game

{

abstract

void

initialize

(

)

;

abstract

void

startPlay

(

)

;

abstract

void

endPlay

(

)

;

//

Template

public

final

void

play

(

)

{

//

Initialize the game

initialize

(

)

;

//

Start the game

startPlay

(

)

;

//

End the game

endPlay

(

)

;

}

}

Step 2

Create the entity classes that extend the above class.

Cricket.java

public

class

Cricket

extends

Game

{

@

Override

void

endPlay

(

)

{

System

.

out

.

println

(

"

Cricket Game Finished!

"

)

;

}

@

Override

void

initialize

(

)

{

System

.

out

.

println

(

"

Cricket Game Initialized! Start playing.

"

)

;

}

@

Override

void

startPlay

(

)

{

System

.

out

.

println

(

"

Cricket Game Started. Enjoy the game!

"

)

;

}

}

Football.java

public

class

Football

extends

Game

{

@

Override

void

endPlay

(

)

{

System

.

out

.

println

(

"

Football Game Finished!

"

)

;

}

@

Override

void

initialize

(

)

{

System

.

out

.

println

(

"

Football Game Initialized! Start playing.

"

)

;

}

@

Override

void

startPlay

(

)

{

System

.

out

.

println

(

"

Football Game Started. Enjoy the game!

"

)

;

}

}

Step 3

Use Game the template method play() to demonstrate how the game is defined.

TemplatePatternDemo.java

public

class

TemplatePatternDemo

{

public

static

void

main

(

String

[

]

args

)

{

Game

game

=

new

Cricket

(

)

;

game

.

play

(

)

;

System

.

out

.

println

(

)

;

game

=

new

Football

(

)

;

game

.

play

(

)

;

}

}

Step 4

Run the program, output result:



Cricket Game Initialized! Start playing.

Cricket Game Started. Enjoy the game!

Cricket Game Finished!



Football Game Initialized! Start playing.

Football Game Started. Enjoy the game!

Football Game Finished!