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
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.
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
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 senior 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 f...
This author has published 3 articles on DotNetSlackers. View other articles or the complete profile here.
|
Other articles in this category
Introduction to StructureMap
Have you heard of StructureMap, generally know what it’s for, and want to know how to get started qu...
The Command Pattern
In this article I will provide a quick refresher on what the command pattern is used for, how it wor...
DI Patterns: Constructor Injection
In this article, an excerpt from the book "Dependency Injection in .NET", we will take a detailed lo...
TypeMock’s Arrange-Act-Assert
Brian Mains discusses how to implement the Arrange-Act-Assert pattern in TypeMock.
Key Process Patterns
This article, based on chapter 2 of Specification by Example, presents effective patterns for softwa...
You might also be interested in the following related blog posts
Mobile Design Pattern Gallery: UI Patterns for Mobile Applications
read more
6 common use of Template Design pattern: - Design pattern series
read more
Pattern focus: Decorator pattern
read more
Oredev Wrap-Up
read more
MPI Project Template for VS2010
read more
Using T4 Templates for Simple DTOs
read more
12 ASP.NET MVC Best Practices
read more
Visual Studio 2010 Extension Manager
read more
Choose your preferred data layout with RadListView for ASP.NET AJAX
read more
VS10 Beta 2 From an ASP.NET MVC Perspective
read more
|
|
Please login to rate or to leave a comment.