{"id":1690,"date":"2023-04-24T15:14:10","date_gmt":"2023-04-24T08:14:10","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1690"},"modified":"2023-05-13T14:39:28","modified_gmt":"2023-05-13T07:39:28","slug":"csharp-template-method-pattern","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-template-method-pattern\/","title":{"rendered":"C# Template Method Pattern"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Template Method pattern and how to implement it in C#.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# Template Method Pattern<\/h2>\n\n\n\n<p>The Template Method pattern defines a skeleton of an algorithm in a superclass and allows subclasses to redefine specific steps of the algorithm without changing its structure.<\/p>\n\n\n\n<p>The following UML diagram illustrates the Template Method pattern:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2023\/04\/CSharp-Template-Method-Pattern.svg\" alt=\"C# Template Method Pattern\" class=\"wp-image-1694\"\/><\/figure>\n<\/div>\n\n\n<p>Typically, the Template Method consists of an <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-abstract-class\/\">abstract class<\/a> that defines the structure of the algorithm and concrete subclasses that implement the specific steps of the algorithm. <\/p>\n\n\n\n<p>The abstract class provides a set of template methods that the subclasses can override to provide their own implementation.<\/p>\n\n\n\n<p>In practice, you use the Template Method pattern when you have an algorithm that you need to implement in a similar way across multiple classes but with some slight variations. <\/p>\n\n\n\n<p>Instead of duplicating the same code across different classes, you can define a standard template in a superclass, and then override some methods in the subclasses to customize the corresponding algorithm steps.<\/p>\n\n\n\n<p>The Template Method pattern reduces code duplication, increases code reuse, and makes it easier to maintain and modify the algorithm in the future.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C# Template Method pattern example<\/h2>\n\n\n\n<p>The following program demonstrates how to use the Template Method pattern to define a generic algorithm for reading data from a file, transforming it, and storing it in a database while allowing subclasses to override specific steps to handle different file formats and database types:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">TemplateMethodPattern<\/span>;\n\n\n<span class=\"hljs-comment\">\/\/ Abstract class that defines the template method<\/span>\n<span class=\"hljs-comment\">\/\/ and its steps<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ETL<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">ImportData<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Step 1: read data from file<\/span>\n        <span class=\"hljs-keyword\">var<\/span> data = Extract(filename);\n\n        <span class=\"hljs-comment\">\/\/ Step 2: transform data<\/span>\n        <span class=\"hljs-keyword\">var<\/span> transformedData = Transform(data);\n\n        <span class=\"hljs-comment\">\/\/ Step 3: store data in database<\/span>\n        Load(transformedData);\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Extract<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Transform<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Default implementation of transformation<\/span>\n        <span class=\"hljs-comment\">\/\/ that does nothing<\/span>\n        <span class=\"hljs-keyword\">return<\/span> data;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Load<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>;\n}\n\n<span class=\"hljs-comment\">\/\/ Concrete subclass that handles<\/span>\n<span class=\"hljs-comment\">\/\/ CSV files and SQL Server database<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CSVToSQLServer<\/span> : <span class=\"hljs-title\">ETL<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Extract<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Read CSV file and return data as an array of strings<\/span>\n        <span class=\"hljs-keyword\">return<\/span> File.ReadAllLines(filename);\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Load<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Store data in SQL Server database<\/span>\n        <span class=\"hljs-comment\">\/\/ Implementation omitted for brevity<\/span>\n        Console.WriteLine(<span class=\"hljs-string\">\"Store Data in SQL Server\"<\/span>);\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ Concrete subclass that handles<\/span>\n<span class=\"hljs-comment\">\/\/ Excel files and MySQL database<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ExcelToMySQL<\/span> : <span class=\"hljs-title\">ETL<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Extract<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Read Excel file and return data as an array of strings<\/span>\n        <span class=\"hljs-comment\">\/\/ Implementation omitted for brevity<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] { <span class=\"hljs-string\">\"1,2,3\"<\/span>, <span class=\"hljs-string\">\"4,5,6\"<\/span> };\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Transform<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Transform Excel data to CSV format<\/span>\n        <span class=\"hljs-comment\">\/\/ Implementation omitted for brevity<\/span>\n        Console.WriteLine(<span class=\"hljs-string\">\"Excel data transformed to CSV format\"<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> data;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Load<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Store data in MySQL database<\/span>\n        <span class=\"hljs-comment\">\/\/ Implementation omitted for brevity<\/span>\n        Console.WriteLine(<span class=\"hljs-string\">\"Data stored in MySQL\"<\/span>);\n    }\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Import CSV data into SQL Server<\/span>\n        <span class=\"hljs-keyword\">var<\/span> csvSqlServerImport = <span class=\"hljs-keyword\">new<\/span> CSVToSQLServer();\n        csvSqlServerImport.ImportData(<span class=\"hljs-string\">\"data.csv\"<\/span>);\n\n        <span class=\"hljs-comment\">\/\/ Import Excel data into MySQL<\/span>\n        <span class=\"hljs-keyword\">var<\/span> excelMySqlImport = <span class=\"hljs-keyword\">new<\/span> ExcelToMySQL();\n        excelMySqlImport.ImportData(<span class=\"hljs-string\">\"data.xlsx\"<\/span>);\n\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define an abstract class called <code>ETL<\/code> (Extract, Transform, Load) that contains the template method called <code>ImportData<\/code>. <\/p>\n\n\n\n<p>The <code>ImportData<\/code> method defines the steps to import data from a file, transform the data, and load it into a database.<\/p>\n\n\n\n<p>The <code>ImportData<\/code> method calls three abstract methods, <code>Extract<\/code>, <code>Transform<\/code>, and <code>Load<\/code>, which represent the specific steps of the ETL process:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>Extract<\/code> method is responsible for reading data from a file.<\/li>\n\n\n\n<li>The <code>Transform<\/code> method transforms the data. <\/li>\n\n\n\n<li>The <code>Load<\/code> method saves the transformed data in a database.<\/li>\n<\/ul>\n\n\n\n<p>We mark the <code>Extract<\/code> and <code>Load<\/code> methods as <code><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-abstract-class\/\">abstract<\/a><\/code> so that the subclasses need to implement them. We also mark the <code>Transform<\/code> method as <code>virtual<\/code> and provide a default implementation. The subclasses can override the <code>Transform<\/code> method if necessary:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Abstract class that defines the template method<\/span>\n<span class=\"hljs-comment\">\/\/ and its steps<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ETL<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">ImportData<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Step 1: read data from file<\/span>\n        <span class=\"hljs-keyword\">var<\/span> data = Extract(filename);\n\n        <span class=\"hljs-comment\">\/\/ Step 2: transform data<\/span>\n        <span class=\"hljs-keyword\">var<\/span> transformedData = Transform(data);\n\n        <span class=\"hljs-comment\">\/\/ Step 3: store data in database<\/span>\n        Load(transformedData);\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Extract<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Transform<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Default implementation of transformation<\/span>\n        <span class=\"hljs-comment\">\/\/ that does nothing<\/span>\n        <span class=\"hljs-keyword\">return<\/span> data;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Load<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define the <code><code><code>CSV<\/code>ToSQLServer<\/code><\/code> class that inherits from the <code>ETL<\/code> class. The <code><code><code>CSV<\/code>ToSQLServer<\/code><\/code> implements the <code>Extract()<\/code> method to read data from a <code>CSV<\/code> file and the <code>Load()<\/code> method to store the data in a SQL Server database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Concrete subclass that handles<\/span>\n<span class=\"hljs-comment\">\/\/ CSV files and SQL Server database<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CSVToSQLServer<\/span> : <span class=\"hljs-title\">ETL<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Extract<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Read CSV file and return data as an array of strings<\/span>\n        <span class=\"hljs-keyword\">return<\/span> File.ReadAllLines(filename);\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Load<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Store data in SQL Server database<\/span>\n        <span class=\"hljs-comment\">\/\/ Implementation omitted for brevity<\/span>\n        Console.WriteLine(<span class=\"hljs-string\">\"Store Data in SQL Server\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, define the <code>ExcelToMySQL<\/code> class that extends the <code>ETL<\/code> class. The <code>CSVToSQLServer<\/code> class implements the <code>Extract()<\/code> method to read data from an Excel file and the <code>Load()<\/code> method to store the data in a MySQL Server database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Concrete subclass that handles<\/span>\n<span class=\"hljs-comment\">\/\/ Excel files and MySQL database<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ExcelToMySQL<\/span> : <span class=\"hljs-title\">ETL<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Extract<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Read Excel file and return data as an array of strings<\/span>\n        <span class=\"hljs-comment\">\/\/ Implementation omitted for brevity<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] { <span class=\"hljs-string\">\"1,2,3\"<\/span>, <span class=\"hljs-string\">\"4,5,6\"<\/span> };\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] <span class=\"hljs-title\">Transform<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Transform Excel data to CSV format<\/span>\n        <span class=\"hljs-comment\">\/\/ Implementation omitted for brevity<\/span>\n        Console.WriteLine(<span class=\"hljs-string\">\"Excel data transformed to CSV format\"<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> data;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Load<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] data<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Store data in MySQL database<\/span>\n        <span class=\"hljs-comment\">\/\/ Implementation omitted for brevity<\/span>\n        Console.WriteLine(<span class=\"hljs-string\">\"Data stored in MySQL\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, the <code>Main()<\/code> method of the program uses the <code><code>CSV<\/code>ToSQLServer<\/code> and <code>ExcelToMySQL<\/code> classes to import data from CSV and Excel files and save the data to a SQL Server and MySQL, respectively.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ Import CSV data into SQL Server<\/span>\n        <span class=\"hljs-keyword\">var<\/span> csvSqlServerImport = <span class=\"hljs-keyword\">new<\/span> CSVToSQLServer();\n        csvSqlServerImport.ImportData(<span class=\"hljs-string\">\"data.csv\"<\/span>);\n\n        <span class=\"hljs-comment\">\/\/ Import Excel data into MySQL<\/span>\n        <span class=\"hljs-keyword\">var<\/span> excelMySqlImport = <span class=\"hljs-keyword\">new<\/span> ExcelToMySQL();\n        excelMySqlImport.ImportData(<span class=\"hljs-string\">\"data.xlsx\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Template Method pattern to define the skeleton of an algorithm in a superclass and allow subclasses to override specific steps of the algorithm without changing its structure.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1690\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-template-method-pattern\/\"\n\t\t\t\tdata-post-title=\"C# Template Method Pattern\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1690\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-template-method-pattern\/\"\n\t\t\t\tdata-post-title=\"C# Template Method Pattern\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Template Method pattern and how to implement it in C#.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1441,"menu_order":26,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1690","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=1690"}],"version-history":[{"count":4,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1690\/revisions"}],"predecessor-version":[{"id":1814,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1690\/revisions\/1814"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1441"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=1690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}