Tuesday, October 7, 2008

Implementing Factory Pattern using Reflection and Singleton Pattern

Today I want to demonstrate how to implement Factory pattern using Reflection...

Important point that should be considered whenever you write code
1) Keep the coupling between components as low as possible --

what this means is when you write components, you should always try to keep the code independent of the other components. It should be testable by itself. It should be reusable without the need of other components.

One of the standard technique that lot of people use to reduce the coupling is by using the Factory Pattern.

Generally use the "new" keyword to instantiate a class, but my doing so we make our code dependent on that object.
Factory pattern separates the creation and initialization of object.

To demonstrate this,
Lets see the class diagram


PS: Please click on the image to see the image contents.

Computer is an abstract base class and different computers like (BrandXComputer or ConcreteComputer) inherit from this interface. Computer as an abstract property called Mhz. Different derived classed will override this property.

Similarly, there is an Abstract class called ComputerFactory, it has an abstract method called "GetFactory". Every class you create will have a corresponding concretefactory class which will return an instance of the corresponding Concrete Computer class. Each concrete factory has a Singleton implementation i.e only one instance of these classes are created. This make sense here as we dont want to instantiate the Factory class everyone.. which will be a wastage of memory.

Alright, till now what we have done is standard factory implementation. It gets interesting when you actually get an instance of the factory. The way this is implemented is through Reflection.

All the concrete factories that you need are kept in the App.Config Files...

PS: Please click on the image to see the image contents.

In the config file bascially we keep all the entries of the concrete factory along with their types i.e class names.

There is one more class which is very important in this whole setup and that class is -- ComputerFactories.

Computer factories has one Static method GetFactory which will take the factoryname (i.e key from the app.config file) , from that it will get the type of the factory and using FieldInfo class it will get that singleton instance we created. Reflection is a very powerful and it has been extensively used in the .Net Base Class Library.

As you can see we, the client code doesnt need to know anything about the Computer Object instantiation or initialization. The code is not tightly coupled.

To download the SourceCode -- Click Here

Happy Programming!!!
Yash

1 comments:

Anonymous said...

Free Online Casino tyuueooru
Casino Games
You should also check out the software that these online casino websites are using.
[url=http://www.nhgaa.org/]Casino Game Online[/url]
This is why online casino seems to be a common interest of many nowadays.
http://www.nhgaa.org/ - Casino Bonus
So, it is vital to be able to find a right online casino website for you if you don't want your online gambling experience to be annoying and regretting.