Build your professional network on facebook via our app Go to app
 
 
 
Functional Area : Architecture
Activity:  1 comments  169 views  last activity : 07 06 2010 20:18:04 +0000
 Refer 12
Share
 
 
 

Many times we come across a situation where we want to have a some way to create instance of related classes. Using Factory Method pattern we can achieve this.

Main Parts of this pattern are,

  1. Product(Interface - IController)
  2. Concrete Product(MainController, LoginController etc.. implementing IController i.e. Product)
  3. Creator(IControllerCreatorHelper)
  4. ConcreteCreator(CControllerInstanceCreator)

 

While Implementing MVC pattern for one of my project, i came across a situation where i required generalized mechanism to create a singleton instance of a every Controller class.

For this i started creating a hierarchy for Controller classes where every Controller class has to implement IController interface.

public interface IMainController:IController

{

___________

___________

}

public class MainController: IMainController
{

public static IMainController Instance
{
get
{

return(IMainController)CControllerInstanceCreator<MainController>.Instance;

}
}

}

internal class CControllerCreatorHelper:IControllerCreatorHelper
{

#region IControllerCreatorHelper Members

public IMainController CreateMainControllerInstance()
{
return MainController.Instance;
}

#endregion

}

public class CControllerInstanceCreator where T : IController, new()
{
private static T m_oController;
private static object syncRoot = new Object();

public static IController Instance
{
get
{
if (m_oController == null)
{
lock (syncRoot)
{
m_oController = new T();
}
}
return m_oController;
}

}
}

 

By reading the code you can observe CControllerCreatorHelper class contains a method for every controller to return the singleton instance. CControllerInstanceCreator class actully returns a singleton instance of any type implementing IController interface.

 

For more information on Factory method pattern plz. visit

http://www.dofactory.com/Patterns/PatternFactory.aspx

 
1 comments on "Implementing Factory Method Pattern"
  Commented by  Dipak Mawale, Senior Executive, Harbinger Knowledge Products    | 06 11 2008 14:02:22 +0000
Thanks :)
Add your comment on "Implementing Factory Method Pattern"

Rate:
Submit
Part of Randstad, the world’s second largest HR services company
Part of Randstad, the world’s second largest HR services company
Viewers also viewed
As everyone knows about the importance of Lean thinking in the Construction process as customers...
 
12 referals 10 votes, 2940 views
Mallick Software Solutions provides end-to-end web design and development solutions that enhance...
 
18 referals 3 arguments, 351 views
Obviously there is never a winner in “language X is better/faster/more scalable than language...
 
3 referals 33 arguments, 53197 views
more...  
Recent Knowledge (87)
I did my Schooling from a Boarding(St.Amtul's Public School - Nainital) Masters in International...
 
968 referals 2 comments, 88 views
Recovery of its due has been a hectic exercise for the Banks in the absence of a special...
 
1 referals 1 comments, 0 views
We know that Ecommerce is the most happening trend of the day. After the emergence of ecommrce,...
109 referals 1 comments, 53 views
more...  
More From Author
Many times we come across a situation where we want to have a some way to create instance of related classes. Using Factory Method pattern we can achieve this. Main Parts of this pattern are, Product(Interface - IController) Concrete...
This article promotes the practical approach using generics collections. Working across the project while reviewing the code i came to know the beauty of using generics. I will guide you through how to replace the collections with generic...
ok.... cool.... But did you get the exact reason for session time out?... Again You can try out calling webservice method asynchronously if possible. Tkcr Welcome
more...