Click here to Skip to main content
12,884,771 members (28,785 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 12 Nov 2013

Template Design Pattern for ComputerNavigator - A Practical Approach

, 12 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains a practical approach to employ the template design pattern using an example of a ComputerNavigator module.


Template design pattern as defined in is “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”.  I love to visit this website often to get the resolution of any queries regarding design patterns. Nice stuff!


I usually spend the weekend on my laptop to innovate. I like to give simple solutions to the complex problems. I would certainly NOT like to become the Software Architect of any company as they give complex solutions to the simple problems!! (kidding, no offence meant). Last weekend I came across a very good idea to develop my own code to navigate computer system. System includes Services, Devices, Eventlogs (.evt files), Shares, etc for a particular machine. I am new to WMI stuff. The explanation on how WMI works is out of the scope of this article. I have developed the code to navigate the internals of the local computer only (due to stringent deadlines, as we usually have in software delivery center!!), however the code can be tweaked a bit to tune it to navigate the internals of remote computer also.

Using the code

The code includes five classes viz. SystemBase, Devices, Services, Eventlogs, and Shares. SystemBase is the base class for rest of the four classes. It contains the code which is common for rest of the classes. This code includes usage of ManagementObjectSearcher, ManagementObjectCollection, ObjectQuery (please refer the code attached with this article). Please refer the code attached for more details. 

The important methods of SystemBase are:  

  • public ManagementObjectCollection GetManagementObjectCollection(); -> This method contains the common code to connect to the computer using WMI classes.
  • public virtual void GetData(ref DataTable dtData); -> This method gets the data by iterating over ManagementObjectCollection.
  • public virtual void CreateDataSource(ref DataTable dtDataSource); -> This method creates the schema of the database to bind to datagrid on WinForm. This is not the good way of creating the schema of datatable as we have better ways to do this. (e.g. create typed dataset, create datatable using XML file, etc). I am writing the code over the weekend hence I am feeling lazy to use any of the above two!
  • public virtual ObjectQuery GetWMIQuery(); -> This method creates the WMI query to get the particular type of the data from the computer. This method contains the default implementation which is inturn overriden by the child classes. E.g. The Services class will override GetWMIQuery() to initialize the WMI query that returns Services related data from the computer. This is point where our template design pattern works!
public ManagementObjectCollection GetManagementObjectCollection()
    ObjectQuery objQuery = GetWMIQuery();
    objMgmtObjSearcher = new ManagementObjectSearcher(objManagementScope, objQuery);
    objMgmtObjCollection = objMgmtObjSearcher.Get();

    return objMgmtObjCollection;

public virtual ObjectQuery GetWMIQuery()
   return null;

The child classes viz. Services, EventLogs, Devices, Shares overrides GetWMIQuery() method to provide their own implementation. Please spare a few moments to look at the code as it will be more appropriate. Please make sure that you modify the code to put your computer name in Form1.cs. objScope = new ManagementScope("//<<Computername>>/root/cimv2");

// Devices.cs
public override System.Management.ObjectQuery GetWMIQuery()
   return new ObjectQuery("select * from Win32_SystemDriver");

// EventLogs.cs
public override System.Management.ObjectQuery GetWMIQuery()
   return new ObjectQuery("select * from Win32_NTEventlogFile");

So on, so forth...

Points of Interest

Unlike OOPS concepts, the design patterns are not the rules that your code should follow. These are only the customizable solutions which somebody else have written for us.


There are some more design patterns waiting for you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
He is a Bachelor of Engineering (Computer science) graduate with the total work experience of around 5 years on microsoft technologies primarily on C#.NET. Currently he is engaged in exploring new technologies like nHibernet, DDD, TDD, jQuery and ASP.NET MVC. His previous experience includes the development of the leading community website, C++/COM+ with active directory (He loved playing with AD), MS-CRM, .NET remoting, webservices and ASP.NET. He holds the MCP certification for 70-316.

Along with Software Development, he likes to play harmonica (Mouthorgan) and likes to spend the weekend researching on new coding techniques published on

You may also be interested in...


Comments and Discussions

QuestionNot enough for an article, may be better as a tip ... Pin
mekDroid27-Feb-15 5:32
membermekDroid27-Feb-15 5:32 
GeneralMy vote of 1 Pin
Henry Minute20-Aug-09 14:32
memberHenry Minute20-Aug-09 14:32 
GeneralMy vote of 1 Pin
Acoustic19-Aug-09 6:45
memberAcoustic19-Aug-09 6:45 
QuestionRe: My vote of 1 Pin
Pratik.Patel19-Aug-09 7:13
memberPratik.Patel19-Aug-09 7:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 12 Nov 2013
Article Copyright 2013 by Pratik.Patel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid