{"id":2473,"date":"2021-06-05T14:59:47","date_gmt":"2021-06-05T14:59:47","guid":{"rendered":"https:\/\/system.camp\/?p=2473"},"modified":"2021-06-05T15:00:58","modified_gmt":"2021-06-05T15:00:58","slug":"template-design-pattern","status":"publish","type":"post","link":"https:\/\/system.camp\/java\/template-design-pattern\/","title":{"rendered":"Template Design Pattern"},"content":{"rendered":"\n

When do we need the template design pattern?<\/h3>\n\n\n\n

The template design pattern is useful when different components share some functionality and adding a change to one would need duplicate changes in the other component.<\/p>\n\n\n\n

How do we implement the template design pattern?<\/h3>\n\n\n\n

The template design pattern is a behavioural design patter. In this, we have an abstract class that exposes templates in order of the subclasses to execute it. The subclasses can choose the override the existing implementation but the invocation needs to follow the same contract. <\/p>\n\n\n\n

Below is an example of the abstract class and it’s subclasses.<\/p>\n\n\n\n

public abstract class Game {\n   abstract void initialize();\n   abstract void startPlay();\n   abstract void endPlay();\n\n   \/\/template method\n   public final void play(){\n      initialize();\n      startPlay();\n      endPlay();\n   }\n}\n<\/code><\/pre>\n\n\n\n
public class TableTennis extends Game {\n\n   @Override\n   void endPlay() {\n      System.out.println(\"Table tennis game is finished!\");\n   }\n\n   @Override\n   void initialize() {\n      System.out.println(\"Initializing table tennis!\");\n   }\n\n   @Override\n   void startPlay() {\n      System.out.println(\"Start playing table tennis!\");\n   }\n}\n<\/code><\/pre>\n\n\n\n
public class Cricket extends Game {\n\n   @Override\n   void endPlay() {\n      System.out.println(\"Cricket game finished!\");\n   }\n\n   @Override\n   void initialize() {\n      System.out.println(\"Cricket Game Initialized!\");\n   }\n\n   @Override\n   void startPlay() {\n      System.out.println(\"Cricket Game Started!\");\n   }\n}\n<\/code><\/pre>\n\n\n\n

Do try out the example! Also, do note that the Factory pattern is a specialisation of template pattern \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"

When do we need the template design pattern? The template design pattern is useful when different components share some functionality and adding a change to one would need duplicate changes in the other component. How do we implement the template design pattern? The template design…<\/p>\n","protected":false},"author":2,"featured_media":2475,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[40,39],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/2473"}],"collection":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/comments?post=2473"}],"version-history":[{"count":1,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/2473\/revisions"}],"predecessor-version":[{"id":2474,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/2473\/revisions\/2474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media\/2475"}],"wp:attachment":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media?parent=2473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/categories?post=2473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/tags?post=2473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}