Javatpoint Logo
Javatpoint Logo

Template Pattern

A Template Pattern says that "just define the skeleton of a function in an operation, deferring some steps to its subclasses".


Benefits:

  • It is very common technique for reusing the code.This is only the main benefit of it.

Usage:

  • It is used when the common behavior among sub-classes should be moved to a single common class by avoiding the duplication.

UML for Template Pattern:

Template Pattern

Implementation of Template Pattern:

Step 1:

Create a Game abstract class.

Step 2:

Create a Chess class that will extend Game abstract class for giving the definition to its method.

Step 3:

Create a Soccer class that will extend Game abstract class for giving the definition to its method.

Step 4:

Create a TemplatePatternDemo class.

Output:

Template Pattern
Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA