Wednesday, August 27, 2008

Builder Pattern in Real Life Scenario

Builder Desing Pattern:
I implemented Builder pattern for scenario pertaining to hetrogeneous authentication.
Below code outlines how to use builder pattern using an Interface. Same can be implemented using Abstract Class also. However using Abstract class has many significant advantages viz, The class can be partially implemented and inherited - best scenario when the design is in conceptual stage / not base lined...


The key components of the builder patterns are as follows:

1)Director > Selects/Provides the object to be represented, implemented by class Director.
2)Builder > Implemented by Interface "IAuthenticationBuilder"
3)Concrete Class > Class that contains actual implementation. Eg/- NT_Authentication, DB_Authentication....so on



namespace nBuilderPattern
{
//Builder
interface IAuthenticationBuilder
{
bool Authenticate();
}

//Concrete Implementation Class : LDAP Authentication
class LDAP_Authentication:IAuthenticationBuilder
{
public bool Authenticate()
{
//implement Code for LDAP Authentication
return true;
}
}

//Concrete Implementation Class : DB Authentication
class DB_Authentication : IAuthenticationBuilder
{
public bool Authenticate()
{
//implement Code for DB Authentication
return true;
}
}

//Concrete Implementation Class : Windows NT Authentication
class NT_Authentication : IAuthenticationBuilder
{
public bool Authenticate()
{
//implement Code for NT Authentication
return true;
}
}


//Director
class Director
{
public bool Construct(IAuthenticationBuilder oAB)
{
return oAB.Authenticate();
}
}


//Main
public class main
{
public static bool main1(int nSelection)
{
Director objDirector = new Director();
IAuthenticationBuilder iAB;
switch (nSelection)
{
case 1:
iAB = new LDAP_Authentication();
break;
case 2:
iAB = new DB_Authentication();
break;
case 3:
iAB = new NT_Authentication();
break;
default:
iAB = new NT_Authentication();
break;
}
return objDirector.Construct(iAB);
}
}

}

Another scenarion which come to my mind at this point of time, where we could use Builder pattern is Soft Drink Vending machine.
Following can be corelated;
1. Director - Waiter, who takes in you order and selects the soft drink.
2. Builder - Vending Machine(interface).
3. Concrete Class - Coke Class, Sprit Class, Pepsi Class, Diet Coke class, Miranda Class.

Friday, August 22, 2008

Unable to Run TaskManager and Regedit in Win XP

Today i had a strange symptoms with my Win XP OS. The symptoms were as below.
1. Unable to Run TaskManager, MS Config, RegEdit.Whenever you run these programs, the window would flash for a second and disappear and prompt with a dialog box stating "Task Manager is disabled by the Administrator".
2. Accessing the drives was a issue. Overall the system was very slow.
3. unable to bring up local Computer Policy (GPEdit.msc).
4. Virus scanning was consuming lot of system memory.

Since the taskmanager was not showing up, it was very difficult to find out if there were any illegal process running.

The first thing was to bring up the task manager. I googled and found few website that gave satisfactory answers. One of them was "http://www.dougknox.com/xp/utils/xp_emerutils.htm".This website provides with a emergency utility called "xp_emegencyutil.exe". This utility copies correct version of MSConfig, Regedit, Taskmanger programs to a folder, which can then be copied to c:\windows\system32 directory. Best use when you dont have a copy of windows XP CD/DVD.


Then i tried running the following command from the Start > Run window.
"REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /t REG_DWORD /d 0 /f"

Immediately the task manager was enabled but yet ran into same problem.I was sure there was either a virus / trojan that had disabled or renamed or infact deleted the regedit.exe and taskmanager.exe. So checked for the file in c:\windows\system32 and learnt that these files were renamed and missing. I copied these files back to c:\windows\system32\ and re-ran the following commands

  1. "REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /t REG_DWORD /d 0 /f"

  2. "REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableRegistryTools /t REG_DWORD /d 0 /f"

Hoorray!!! I was able to get the TaskManager, MSConfig and RegEdit programs up and running.

On running taskmanager i saw 5-7 instance of "ssvichosst" program running. I checked in the net and found that this was a Dangeous Spyware which had infected Autorun.in and many important files.Quickly i went to startup's in the MSConfig utility and disabled this exe. My system is better and working fine..!!

About ssvichosst : Click here to know about ssvichosst.exe

Important URL's:
1. http://windowsxp.mvps.org/tweakuirest.htm
2. http://windowsxp.mvps.org/ToolsQuit.htm

Thursday, August 21, 2008

Singleton Design Pattern

This is a Creational Design Pattern. There are many ways to implement this pattern. But two simple approach is as below:
Approach - 1: Check the instance count and instantiate the class in the implementation class.

//Parent Class
class clsDirector
{
  //declare static variable to track the instance count
  public static int nCount=0;

  //make the constructor public.
  //This is method will be invoked first.
  public Director()
  {
    if(nCount==0)
    {
    nCount++;
    }
  }
}

//Implementation Class
Class clsImplementation
{
  //Check for the instance count. If 0 then instantiate.
  if(clsDirector.nCount==0)
  {
    clsDirector objDirector=new Director();
  }
}

Approach 2 - Just return the object. Instance check and instantiation will be done in the parent class.

//Parent Class
class clsDirector
{
  public static int nCount;
  //Create a static object of class type clsDirector
  private static clsDirector objDirector;

  //Constructor is made private
  private clsDirector
  {
  }

  //this method returns the instance of the class "clsDirector"
  public static clsDirector GetInstance()
  {
    if(nCount==0)
    {
    //create instance when nCount=0
    objDirector=new clsDirector();
      nCount++;
    }
    //return the object
    return objDirector;
  }
}

class clsImplementation
{
  //Cannot use "New" -> clsDirector obj =new clsDirector();
  clsDirector objDirector=clsDirector.GetInstance();
}

Tuesday, August 5, 2008