Saturday, June 15, 2013

Template design pattern using c#

Template design pattern is one of the popular design pattern in software project development. As the name suggest we can make template of any class. Then we can derive a new class from the template class.
And probably you guess the advantage. Yes, The common part of all class we can define in Template class and according to our need to can add extra salt(read feature) in derive class . And then our derive class will be very thin with all features. Let’s see below code to understand Template pattern.
Here I have created Employee Template class . And my ProcessEmployee is derive class. You can find that in my drive class I did not define any get set property ,still happily I am using them. Because my base class is containing them.  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

namespace BlogProject
{
    //Template Desingn pattern

    abstract public class Employee   // Template Class
    {
        public String Name { get; set; }
        public String Surname { get; set; }
        public int Eid { get; set; }
    }

    public class ProcessEmployee : Employee  // Derive Class from Template class
    {
        public void AddEmployee(Employee e)
        {
            Console.WriteLine(e.Name + e.Surname + e.Eid);
            //You can write your add code to add data in Database
        }

        public void EditEmployee(Employee e)
        {
            Console.WriteLine(e.Name + e.Surname + e.Eid);

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ProcessEmployee objEmp = new ProcessEmployee();
            objEmp.Name = "Sourav";
            objEmp.Surname ="Kayal";
            objEmp.Eid =1001;
            objEmp.AddEmployee(objEmp);
            //objEmp.EditEmployee(objEmp);

            Console.ReadLine();
        }
    }

}



No comments:

Post a Comment