Introduction
Programmers can easily get lost in the details when creating large and complicated applications. As bugs start to show up, the developer ends up creating specialized solutions to each particular problem. This is a bad practice because the developer will spend more time creating specialized solution and adding code than it took the initial system. Using design patterns will help you design your application following proven best practices. In this article, we will discuss the fundamental design pattern: Template Pattern
Template Pattern
The Template pattern belongs to the behavioral patterns and its one of the most widely used and useful pattern. It defines the program skeleton of the algorithm. The algorithm itself is made abstract, and the method in the subclasses will override the abstract methods in the base class to provide concrete behavior.
Suppose you need to fetch data from a database and do 2 types of analysis on that data. We used to create two main classes to reach our goal. Using this Pattern, you create only one base class which in this case fetch the data from the database and provide a general abstract method. Then you create two subclasses to implement the two different behaviors. In the first subclass for example you override the abstract method to do one type of analysis, and in the second subclass you override the abstract method to do the other type of analysis.
Template Pattern UML
Figure1 provides a simple template pattern example.
In the above UML diagram, we can see that an abstract class is defined with one abstract method which includes two other methods and we have two concrete classes (ConcreteClassA, ConcreteClassB). The only thing we do in these two subclasses only is to override the two abstract methods in the abstract class to implement our varying behavior.
Template Pattern Example using System;
using System.Data;
using System.Data.SqlClient;
namespace Template_Pattern
{
/// <summary>
/// Summary description for DataAccess.
/// </summary>
public abstract class
DataAccess
{
protected string connectionString;
protected DataSet ds;
public DataAccess()
{
//
// TODO: Add constructor logic here
//
}
public virtual void
Connect()
{
connectionString = "Data Source=(local);Initial Catalog=master;
Trusted_Connection=yes";
}
public abstract void
GetData();
public void Run()
{
Connect();
GetData();
}
}
}
In the above example, we created an abstract class with one abstract method called from the Run() method. The GetData() function must be overridden in the concrete class to implement the unique behavior. The Connect() method is used to specify the Connection String to used to connect to the database.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace Template_Pattern
{
/// <summary>
/// Summary description for Customers.
/// </summary>
public class Customers: DataAccess
{
public Customers()
{
//
// TODO: Add constructor logic here
//
}
public override void
GetData()
{
string Select= "Select * FROM Customers";
SqlDataAdapter da = new SqlDataAdapter(Select,connectionString);
ds = new DataSet();
da.Fill(ds,"Customers");
foreach(DataRow dr in ds.Tables[0].Rows)
{
HttpContext.Current.Response.Write(dr[0]+ "<br/>");
}
base.Connect ();
}
}
}
In the above listing, we created a class inherited from the DataAccess class. In this class we have overridden the GetData() function to implement its unique behavior which in this example fill the dataset with the data retrieved from our database and output just the first Column Values.
Template Pattern Example Output 1 Haissam Abdul Malak Lebanon
2 Bilal Haidar Lebanon
3 Scott Bryan USA
4 Ryan Washington USA
5 Bill Gates USA
6 David Beckham GB
The above listing is our example output; the subclass (Customers.cs) has implemented its unique behavior. In this case, we just printed out all the values in the datatable filled in the dataset.
Conclusion
After completing this article, you will be in the right track for mastering the OOP (Object Oriented Programming) concept which indeed will help you design your application in a best way. It is important to say that choosing which design pattern to implement in your application needs a lot of experience. Hope this article was informative, and I will try my best to cover as much as I can in the future more design patterns.
About Haissam Abdul Malak
 |
Haissam Abdul Malak works as software developer in CCC. He has been developing web application using ASP.NET technology over the last 3 years. He achieved the Microsoft Certified Application Developer [MCAD] and been certified since 2006.
He is a regular contributor on the ASP.NET official forums (...
View complete profile
|
Please login to rate or to leave a comment.