Template Design Pattern for ComputerNavigator - A Practical Approach





1.00/5 (3 votes)
This article explains a practical approach to employ the template design pattern using an example of a ComputerNavigator module.
Introduction
Template design pattern as defined in www.dofactory.com 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!
Background
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 overrideGetWMIQuery()
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.
Conclusion
There are some more design patterns waiting for you.