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.
Wednesday, August 27, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment