Last active
May 5, 2018 03:16
-
-
Save asarnaout/5ed3b1ff33b436361bd4c1bf26a442f5 to your computer and use it in GitHub Desktop.
Template Method Design Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TemplateMethod | |
{ | |
/* | |
* The template method Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets | |
* subclasses redefine certain steps of an algorithm without changing the algorithm's structure. | |
* | |
* The Template Method pattern is used when two or more implementations of a similar algorithm exist. In the real world templates are | |
* used all the time: for architectural plans, and throughout the engineering domain. A template plan may be defined which is then built | |
* on with further variations. For example, a basic house plan can have many variations such as adding an extensions or using a different | |
* heating system. | |
* | |
* Template Method may not be an obvious choice in the beginning, but the usual sign that you should use the pattern is when you find that | |
* you have two almost identical classes working on some logic. At that stage, you should consider the power of the template method | |
* pattern to clean up your code. | |
*/ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BaseOrm orm; | |
if(DateTime.Now.DayOfWeek == DayOfWeek.Monday) | |
{ | |
orm = new NotCoolOrm(); | |
} | |
else | |
{ | |
orm = new MyCoolOrm(); | |
} | |
var result = orm.Query(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TemplateMethod | |
{ | |
public abstract class BaseOrm | |
{ | |
public IEnumerable Query() | |
{ | |
ParseQuery(); | |
ProjectData(); | |
MapToPoco(); | |
return Enumerable.Empty<object>(); | |
} | |
protected abstract void ParseQuery(); | |
protected abstract void ProjectData(); | |
protected abstract void MapToPoco(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TemplateMethod | |
{ | |
public class MyCoolOrm : BaseOrm | |
{ | |
public MyCoolOrm() | |
{ | |
} | |
protected override void MapToPoco() | |
{ | |
Console.WriteLine("The Cool ORM is now Mapping the query results to POCO classes"); | |
} | |
protected override void ParseQuery() | |
{ | |
Console.WriteLine("The Cool ORM is now Parsing the Query"); | |
} | |
protected override void ProjectData() | |
{ | |
Console.WriteLine("The Cool ORM is now retrieving the data"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TemplateMethod | |
{ | |
public class NotCoolOrm : BaseOrm | |
{ | |
public NotCoolOrm() | |
{ | |
} | |
protected override void MapToPoco() | |
{ | |
Console.WriteLine("The uncool ORM is mapping the projected data to POCO classes, would probably take forever..."); | |
} | |
protected override void ParseQuery() | |
{ | |
Console.WriteLine("The uncool ORM is parsing the query right now"); | |
} | |
protected override void ProjectData() | |
{ | |
Console.WriteLine("The uncool ORM is projecting the data right now"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment