Presentation is loading. Please wait.

Presentation is loading. Please wait.

TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.

Similar presentations


Presentation on theme: "TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem."— Presentation transcript:

1 TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH

2 WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code but a description or template for how to solve a problem that can be used in many different situations

3 DIFFERENT PATTERNS BY TYPE CREATIONALSTRUCTURALBEHAVIORAL ABSTRACT FACTORY ADAPTERCHAIN OF RESPONSIBILITY BUILDERBRIDGECOMMAND FACTORY METHODCOMPOSITEINTERPRETER PROTOTYPEDECORATORITERATOR SINGELTONFAÇADEMEDIATOR FLYWEIGHTMEMENTO PROXYOBSERVER STATE STRATEGY TEMPLATE METHOD VISITOR

4 DIFFERENT PATTERNS BY TYPE CREATIONALSTRUCTURALBEHAVIORAL ABSTRACT FACTORY ADAPTER CHAIN OF RESPONSIBILITY BUILDERBRIDGECOMMAND FACTORYCOMPOSITE INTERPRETER PROTOTYPEDECORATORITERATOR SINGELTONFAÇADE MEDIATOR FLYWEIGHTMEMENTO PROXYOBSERVER STATE STRATEGY TEMPLATE METHOD VISITOR OF THE 23 PATTERNS WE HAVE COVERED 8 IN CLASS

5 W HAT IS T EMPLATE M ETHOD P ATTERN ?? Behavioral design pattern : Design patterns that identify common communication patterns between objects and realize these communication patterns Defines the program skeleton of an algorithm in a method, called template method Template method defers some steps in subclasses It lets one redefine certain steps of an algorithm without changing the algorithm's structure. The template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in a method, called template method, which defers some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure

6 L ETS SEE WHAT IT ACTUALLY MEANS We’re going to create a Game abstract class defining operations with a template method set to be final so that it cannot be overridden. Cricket and Football are concrete classes extend Game and override its methods.

7 S TEP 1 C REATE AN ABSTRACT CLASS WITH A TEMPLATE METHOD BEING FINAL. public abstract class Game { abstract void initialize(); abstract void startPlay(); abstract void endPlay(); //template method public final void play() { //initialize the game initialize(); //start game startPlay(); //end game endPlay(); }

8 STEP 2 C REATE CONCRETE CLASSES EXTENDING THE ABOVE CLASS. 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!"); }

9 STEP 3 SIMILAR STEP FOR FOOTBALL CLASS 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!"); }

10 STEP 4 USE THE GAME’S TEMPLATE METHOD PLAY() TO DEMONSTRATE A DEFINED WAY OF PLAYING GAME. public class TemplatePatternDemo { public static void main(String[] args) { Game game = new Cricket(); game.play(); System.out.println(); game = new Football(); game.play(); }

11 STEP 5 OUTPUT 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!

12 The Template Method pattern should be used: – To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary. – When refactoring is performed and common behavior is identified among classes. A abstract base class containing all the common code (in the template method) should be created to avoid code duplication.

13 THANK YOU…


Download ppt "TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem."

Similar presentations


Ads by Google