{"id":44793,"date":"2015-09-30T06:41:32","date_gmt":"2015-09-30T03:41:32","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=44793"},"modified":"2023-12-07T13:06:39","modified_gmt":"2023-12-07T11:06:39","slug":"template-design-pattern","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html","title":{"rendered":"Template Design Pattern Example"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">Java Design Patterns<\/a>.<\/em><\/p>\n<p>In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of them. Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">here<\/a>!F<\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#intro\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#whatis\">2. What is the Template Design Pattern<\/a><\/dt>\n<dt><a href=\"#impl\">3. Implementing the Template Design Pattern<\/a><\/dt>\n<dt><a href=\"#hook\">4. Introducing a hook inside the template<\/a><\/dt>\n<dt><a href=\"#when\">5. When to use the Template Design Pattern<\/a><\/dt>\n<dt><a href=\"#jdk\">6. Template Pattern in JDK<\/a><\/dt>\n<dt><a href=\"#download\">7. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"intro\"><\/a>1. Introduction<\/h2>\n<p>The Template Design Pattern is a behavior pattern and, as the name suggests, it provides a template or a structure of an algorithm which is used by users. A user provides its own implementation without changing the algorithm&#8217;s structure.<\/p>\n<p>It is easier to understand this pattern with the help of a problem. We will understand the scenario in this section and will implement the solution using the Template pattern in a later section.<\/p>\n<p>Have you ever connected to a relation database using your Java application? Let\u2019s recall some important steps which are required to connect and insert data into the database. First, we need a driver according to the database we want to connect with. Then, we pass some credentials to the database, then, we prepare a statement, set data into the insert statement and insert it using the insert command. Later, we close all the connections, and optionally destroy all the connection objects.<\/p>\n<p>You need to write all these steps regardless of any vendor\u2019s relational database. Consider a problem where you need to insert some data into the different databases. You need to fetch some data from a CSV file and have to insert it into a MySQL database. Some data comes from a text file and which should be insert into an Oracle database. The only difference is the driver and the data, the rest of the steps should be the same, as JDBC provides a common set of interfaces to communicate to any vendor\u2019s specific relation database.<\/p>\n<p>We can create a template, which will perform some steps for the client, and we will leave some steps to let the client to implement them in its own specific way. Optionally, a client can override the default behavior of some already defined steps.<\/p>\n<p>Now, before implementing the code, let\u2019s know more about the Template Design Pattern.<\/p>\n<h2><a name=\"whatis\"><\/a>2. What is the Template Design Pattern<\/h2>\n<p>The Template Pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses to redefine certain steps of an algorithm without changing the algorithm&#8217;s structure.<\/p>\n<p>The Template Method pattern can be used in situations when there is an algorithm, some steps of which could be implemented in multiple different ways. In such scenarios, the Template Method pattern suggests keeping the outline of the algorithm in a separate method referred to as a template method inside a class, which may be referred to as a template class, leaving out the specific implementations of the variant portions (steps that can be implemented in multiple different ways) of the algorithm to different subclasses of this class.<\/p>\n<p>The Template class does not necessarily have to leave the implementation to subclasses in its entirety. Instead, as part of providing the outline of the algorithm, the Template class can also provide some amount of implementation that can be considered as invariant across different implementations. It can even provide default implementation for the variant parts, if appropriate. Only specific details will be implemented inside different subclasses. This type of implementation eliminates the need for duplicate code, which means a minimum amount of code to be written.<\/p>\n<p><figure id=\"attachment_7241\" aria-describedby=\"caption-attachment-7241\" style=\"width: 176px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/TemplatePatternclass_diagram_13.jpg\"><img decoding=\"async\" class=\"wp-image-7241 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/TemplatePatternclass_diagram_13.jpg\" alt=\"Figure 1 - Class Diagram\" width=\"176\" height=\"244\" \/><\/a><figcaption id=\"caption-attachment-7241\" class=\"wp-caption-text\">Figure 1<\/figcaption><\/figure><\/p>\n<p><strong>AbstractClass<\/strong><\/p>\n<ul>\n<li>Defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.<\/li>\n<li>Implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in <code>AbstractClass<\/code> or those of other objects.<br \/>\n<code>ConcreteClass<\/code><\/li>\n<li>Implements the primitive operations to carry.<\/li>\n<\/ul>\n<h2><a name=\"impl\"><\/a>3. Implementing the Template Design Pattern<\/h2>\n<p>Below we can see the connection template class used to provide a template for clients to connect and communicate with the various databases.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.templatepattern;\n\npublic abstract class ConnectionTemplate {\n\n\tpublic final void run() {\n\t\tsetDBDriver();\n\t\tsetCredentials();\n\t\tconnect();\n\t\tprepareStatement();\n\t\tsetData();\n\t\tinsert();\n\t\tclose();\n\t\tdestroy();\n\t}\n\n\tpublic abstract void setDBDriver();\n\n\tpublic abstract void setCredentials();\n\n\tpublic void connect() {\n\t\tSystem.out.println(\"Setting connection...\");\n\t}\n\n\tpublic void prepareStatement() {\n\t\tSystem.out.println(\"Preparing insert statement...\");\n\t}\n\n\tpublic abstract void setData();\n\n\tpublic void insert() {\n\t\tSystem.out.println(\"Inserting data...\");\n\t}\n\n\tpublic void close() {\n\t\tSystem.out.println(\"Closing connections...\");\n\t}\n\n\tpublic void destroy() {\n\t\tSystem.out.println(\"Destroying connection objects...\");\n\t}\n}<\/pre>\n<p>The abstract class provides steps to connect, communicate and later to close the connections. All these steps are required in order to get the work done. The class provides default implementation to some common steps and leaves the specific steps as abstract which force the client to provide an implementation to them.<\/p>\n<p>The <code>setDBDriver<\/code> method should be implementing by the user to provide database specific drivers. The credentials could be different for different databases; therefore, <code>setCredentials<\/code> is also left as abstract to let the user to implement it.<\/p>\n<p>Similarly, connecting to the database using JDBC API and preparing a statement is common. But, data would be specific so user will provide it, and rest of other steps like running an insert statement, closing connections and destroying object are similar to any database, therefore their implementations are kept common inside the template.<\/p>\n<p>The key method of the above class is the <code>run<\/code> method. The <code>run<\/code> method is used to run these steps in order. The method is set as final because the steps should be kept safe and should not be change by any user.<\/p>\n<p>The below two classes extend the template class and provide specific implementation to some methods.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.templatepattern;\n\npublic class MySqLCSVCon extends ConnectionTemplate {\n\n\t@Override\n\tpublic void setDBDriver() {\n\t\tSystem.out.println(\"Setting MySQL DB drivers...\");\n\t}\n\n\t@Override\n\tpublic void setCredentials() {\n\t\tSystem.out.println(\"Setting credentials for MySQL DB...\");\n\t}\n\n\t@Override\n\tpublic void setData() {\n\t\tSystem.out.println(\"Setting up data from csv file....\");\n\t}\n}<\/pre>\n<p>The above class is used to connect to a MySQL database and provides data by reading a CSV file.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.templatepattern;\n\npublic class OracleTxtCon extends ConnectionTemplate {\n\n\t@Override\n\tpublic void setDBDriver() {\n\t\tSystem.out.println(\"Setting Oracle DB drivers...\");\n\t}\n\n\t@Override\n\tpublic void setCredentials() {\n\t\tSystem.out.println(\"Setting credentials for Oracle DB...\");\n\t}\n\n\t@Override\n\tpublic void setData() {\n\t\tSystem.out.println(\"Setting up data from txt file....\");\n\t}\n}<\/pre>\n<p>The above class is used to connect to an Oracle database and provides data by reading a text file.<br \/>\n[ulp id=&#8217;7eyRwVlMDyxg6Ptw&#8217;]<br \/>\n&nbsp;<br \/>\nNow, let\u2019s test the code.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.templatepattern;\n\npublic class TestTemplatePattern {\n\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"For MYSQL....\");\n\t\tConnectionTemplate template = new MySqLCSVCon();\n\t\ttemplate.run();\n\t\tSystem.out.println(\"For Oracle...\");\n\t\ttemplate = new OracleTxtCon();\n\t\ttemplate.run();\n\t}\n}<\/pre>\n<p>The above code will result to the following output:<\/p>\n<pre class=\"brush:bash\">For MYSQL....\nSetting MySQL DB drivers...\nSetting credentials for MySQL DB...\nSetting connection...\nPreparing insert statement...\nSetting up data from csv file....\nInserting data...\nClosing connections...\nDestroying connection objects... \n\nFor Oracle...\nSetting Oracle DB drivers...\nSetting credentials for Oracle DB...\nSetting connection...\nPreparing insert statement...\nSetting up data from txt file....\nInserting data...\nClosing connections...\nDestroying connection objects...<\/pre>\n<p>The above output clearly shows how the template pattern works to connect and communicate with the different databases using the similar way. The pattern keeps the common code under one class and promotes code reusability. It sets a framework and controls it for the users and allows the users to extends the template in order to provide their specific implementation to some of the steps.<\/p>\n<p>Now, if we enhance the above example by adding a logging mechanism. But some of the users of the code do not want to add this facility, to implement this we can use a hook. A hook is a simple method inside a template class with a default behavior; this behavior can be used to alter some optional steps. A user should implement this method which can hook inside the template class to alter the optional steps of the algorithm.<\/p>\n<h2><a name=\"hook\"><\/a>4. Introducing a hook inside the template<\/h2>\n<p>Let\u2019s enhance the above example with a hook.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.templatepattern;\n\nimport java.util.Date;\n\npublic abstract class ConnectionTemplate {\n\n\tprivate boolean isLoggingEnable = true;\n\n\tpublic ConnectionTemplate() {\n\t\tisLoggingEnable = disableLogging();\n\t}\n\n\tpublic final void run() {\n\t\tsetDBDriver();\n\t\tlogging(\"Drivers set [\" + new Date() + \"]\");\n\t\tsetCredentials();\n\t\tlogging(\"Credentails set [\" + new Date() + \"]\");\n\t\tconnect();\n\t\tlogging(\"Conencted\");\n\t\tprepareStatement();\n\t\tlogging(\"Statement prepared [\" + new Date() + \"]\");\n\t\tsetData();\n\t\tlogging(\"Data set [\" + new Date() + \"]\");\n\t\tinsert();\n\t\tlogging(\"Inserted [\" + new Date() + \"]\");\n\t\tclose();\n\t\tlogging(\"Conenctions closed [\" + new Date() + \"]\");\n\t\tdestroy();\n\t\tlogging(\"Object destoryed [\" + new Date() + \"]\");\n\t}\n\n\tpublic abstract void setDBDriver();\n\n\tpublic abstract void setCredentials();\n\n\tpublic void connect() {\n\t\tSystem.out.println(\"Setting connection...\");\n\t}\n\n\tpublic void prepareStatement() {\n\t\tSystem.out.println(\"Preparing insert statement...\");\n\t}\n\n\tpublic abstract void setData();\n\n\tpublic void insert() {\n\t\tSystem.out.println(\"Inserting data...\");\n\t}\n\n\tpublic void close() {\n\t\tSystem.out.println(\"Closing connections...\");\n\t}\n\n\tpublic void destroy() {\n\t\tSystem.out.println(\"Destroying connection objects...\");\n\t}\n\n\tpublic boolean disableLogging() {\n\t\treturn true;\n\t}\n\n\tprivate void logging(String msg) {\n\t\tif (isLoggingEnable) {\n\t\t\tSystem.out.println(\"Logging....: \" + msg);\n\t\t}\n\t}\n}<\/pre>\n<p>We introduced two new methods inside the above template class. The <code>disableLogging<\/code> is the hook which returns a <code>boolean<\/code>. By default, the boolean <code>isLoggingEnable<\/code>, which enables the logging, is true. A user can override this method if logging should be disabled for his code. The other is a private method used to log the messages.<\/p>\n<p>The below class implements the hook method and returns false, something that switches off the logging mechanism for this specific work.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.templatepattern;\n\npublic class MySqLCSVCon extends ConnectionTemplate {\n\n\t@Override\n\tpublic void setDBDriver() {\n\t\tSystem.out.println(\"Setting MySQL DB drivers...\");\n\t}\n\n\t@Override\n\tpublic void setCredentials() {\n\t\tSystem.out.println(\"Setting credentials for MySQL DB...\");\n\t}\n\n\t@Override\n\tpublic void setData() {\n\t\tSystem.out.println(\"Setting up data from csv file....\");\n\t}\n\n\t@Override\n\tpublic boolean disableLogging() {\n\t\treturn false;\n\t}\n}<\/pre>\n<p>Let\u2019s test this code.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.templatepattern;\n\npublic class TestTemplatePattern {\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"For MYSQL....\");\n\t\tConnectionTemplate template = new MySqLCSVCon();\n\t\ttemplate.run();\n\t\tSystem.out.println(\"For Oracle...\");\n\t\ttemplate = new OracleTxtCon();\n\t\ttemplate.run();\n\t}\n}<\/pre>\n<p>The above class will result to the following output:<\/p>\n<pre class=\"brush:bash\">For MYSQL....\nSetting MySQL DB drivers...\nSetting credentials for MySQL DB...\nSetting connection...\nPreparing insert statement...\nSetting up data from csv file....\nInserting data...\nClosing connections...\nDestroying connection objects... \n\nFor Oracle...\nSetting Oracle DB drivers...\nLogging....: Drivers set [Sat Nov 08 23:53:47 IST 2014]\nSetting credentials for Oracle DB...\nLogging....: Credentails set [Sat Nov 08 23:53:47 IST 2014]\nSetting connection...\nLogging....: Conencted\nPreparing insert statement...\nLogging....: Statement prepared [Sat Nov 08 23:53:47 IST 2014]\nSetting up data from txt file....\nLogging....: Data set [Sat Nov 08 23:53:47 IST 2014]\nInserting data...\nLogging....: Inserted [Sat Nov 08 23:53:47 IST 2014]\nClosing connections...\nLogging....: Conenctions closed [Sat Nov 08 23:53:47 IST 2014]\nDestroying connection objects...\nLogging....: Object destoryed [Sat Nov 08 23:53:47 IST 2014]\n<\/pre>\n<p>You can clearly see in the output, that logging is off for the MySQL implementation, whereas, on for the Oracle implementation.<\/p>\n<h2><a name=\"when\"><\/a>5. When to use the Template Design Pattern<\/h2>\n<p>The Template Method pattern should be used in the following cases:<\/p>\n<ul>\n<li>To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.<\/li>\n<li>When common behavior among subclasses should be factored and localized in a common class to avoid code duplication. You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations.<\/li>\n<li>To control subclasses extensions. You can define a template method that calls &#8220;hook&#8221; operations (see Consequences) at specific points, thereby permitting extensions only at those points.<\/li>\n<\/ul>\n<h2><a name=\"jdk\"><\/a>6. Template Pattern in JDK<\/h2>\n<ul>\n<li><code>java.util.Collections#sort()<\/code><\/li>\n<li><code>java.io.InputStream#skip()<\/code><\/li>\n<li><code>java.io.InputStream#read()<\/code><\/li>\n<li><code>java.util.AbstractList#indexOf()<\/code><\/li>\n<\/ul>\n<h2><a name=\"download\"><\/a>7. Download the Source Code<\/h2>\n<p>This was a lesson on the Template Design Pattern. You may download the source code here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/TemplatePattern-Project.zip\"><strong>TemplatePattern-Project<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of &hellip;<\/p>\n","protected":false},"author":967,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[145],"class_list":["post-44793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-design-patterns"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Template Design Pattern Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Template Design Pattern Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-30T03:41:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-07T11:06:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rohit Joshi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit Joshi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html\"},\"author\":{\"name\":\"Rohit Joshi\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\"},\"headline\":\"Template Design Pattern Example\",\"datePublished\":\"2015-09-30T03:41:32+00:00\",\"dateModified\":\"2023-12-07T11:06:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html\"},\"wordCount\":1376,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Design Patterns\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html\",\"name\":\"Template Design Pattern Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-30T03:41:32+00:00\",\"dateModified\":\"2023-12-07T11:06:39+00:00\",\"description\":\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/template-design-pattern.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Template Design Pattern Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\",\"name\":\"Rohit Joshi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"caption\":\"Rohit Joshi\"},\"description\":\"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/rohit-joshi\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Template Design Pattern Example - Java Code Geeks","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html","og_locale":"en_US","og_type":"article","og_title":"Template Design Pattern Example - Java Code Geeks","og_description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-30T03:41:32+00:00","article_modified_time":"2023-12-07T11:06:39+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Rohit Joshi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Rohit Joshi","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html"},"author":{"name":"Rohit Joshi","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20"},"headline":"Template Design Pattern Example","datePublished":"2015-09-30T03:41:32+00:00","dateModified":"2023-12-07T11:06:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html"},"wordCount":1376,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Design Patterns"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html","name":"Template Design Pattern Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-30T03:41:32+00:00","dateModified":"2023-12-07T11:06:39+00:00","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/template-design-pattern.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Template Design Pattern Example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20","name":"Rohit Joshi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","caption":"Rohit Joshi"},"description":"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/rohit-joshi"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/967"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44793"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44793\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}