Template Method Pattern

 

Comments

 

Use of abstract and virtual access modifiers for overriding so derived classes can have their own implementations.

 

Code

 

 

1.	using System;
2.	using System.Data;
3.	using System.Data.OleDb;
4.	 
5.	namespace DoFactory.GangOfFour.Template.RealWorld
6.	{
7.	  /// 
8.	  /// MainApp startup class for Real-World 
9.	  /// Template Design Pattern.
10.	  /// 
11.	  class MainApp
12.	  {
13.	    /// 
14.	    /// Entry point into console application.
15.	    /// 
16.	    static void Main()
17.	    {
18.	      DataAccessObject daoCategories = new Categories();
19.	      daoCategories.Run();
20.	 
21.	      DataAccessObject daoProducts = new Products();
22.	      daoProducts.Run();
23.	 
24.	      // Wait for user
25.	      Console.ReadKey();
26.	    }
27.	  }
28.	 
29.	  /// 
30.	  /// The 'AbstractClass' abstract class
31.	  /// 
32.	  abstract class DataAccessObject
33.	  {
34.	    protected string connectionString;
35.	    protected DataSet dataSet;
36.	 
37.	    public virtual void Connect()
38.	    {
39.	      // Make sure mdb is available to app
40.	      connectionString =
41.	        "provider=Microsoft.JET.OLEDB.4.0; " +
42.	        "data source=..\\..\\..\\db1.mdb";
43.	    }
44.	 
45.	    public abstract void Select();
46.	    public abstract void Process();
47.	 
48.	    public virtual void Disconnect()
49.	    {
50.	      connectionString = "";
51.	    }
52.	 
53.	    // The 'Template Method' 
54.	    public void Run()
55.	    {
56.	      Connect();
57.	      Select();
58.	      Process();
59.	      Disconnect();
60.	    }
61.	  }
62.	 
63.	  /// 
64.	  /// A 'ConcreteClass' class
65.	  /// 
66.	  class Categories : DataAccessObject
67.	  {
68.	    public override void Select()
69.	    {
70.	      string sql = "select CategoryName from Categories";
71.	      OleDbDataAdapter dataAdapter = new OleDbDataAdapter(
72.	        sql, connectionString);
73.	 
74.	      dataSet = new DataSet();
75.	      dataAdapter.Fill(dataSet, "Categories");
76.	    }
77.	 
78.	    public override void Process()
79.	    {
80.	      Console.WriteLine("Categories ---- ");
81.	 
82.	      DataTable dataTable = dataSet.Tables["Categories"];
83.	      foreach (DataRow row in dataTable.Rows)
84.	      {
85.	        Console.WriteLine(row["CategoryName"]);
86.	      }
87.	      Console.WriteLine();
88.	    }
89.	  }
90.	 
91.	  /// 
92.	  /// A 'ConcreteClass' class
93.	  /// 
94.	  class Products : DataAccessObject
95.	  {
96.	    public override void Select()
97.	    {
98.	      string sql = "select ProductName from Products";
99.	      OleDbDataAdapter dataAdapter = new OleDbDataAdapter(
100.	        sql, connectionString);
101.	 
102.	      dataSet = new DataSet();
103.	      dataAdapter.Fill(dataSet, "Products");
104.	    }
105.	 
106.	    public override void Process()
107.	    {
108.	      Console.WriteLine("Products ---- ");
109.	      DataTable dataTable = dataSet.Tables["Products"];
110.	      foreach (DataRow row in dataTable.Rows)
111.	      {
112.	        Console.WriteLine(row["ProductName"]);
113.	      }
114.	      Console.WriteLine();
115.	    }
116.	  }
117.	}