Saturday, November 13, 2010

..........Abstract Factory Pattern[CD]

Abstract Factory Pattern : Term “Factory” refer to the location in code where object has been created.Here object is created without knowing its class. Means object is created by a method of class not by the class’s constructor. So basically Abstract Factory Pattern is used wherever subclasses given the privilege of instantiating a method that can create an object.

The benefit of this is if in the future the implementation of the object that implements the interface changes, the consuming client is not affected as long as the interface remains the same.

An Abstract Factory class is a class which exist to create instances of another class.

If you want to create an instance of class at runtime (Means class should be selected at runtime).

1) Create an AbstractFactory class for each class you wish to create.
2) In each AbstractFactory class there should have a polymorphic “created instance” method, conforming to a method signature, used to create instances of the corresponding classes.


Suppose an Abstact class wants to hide its subclass name and instantiation.

abstract class BaseClass
{
static BaseClass GetInstance()
{

return new ChildClass();
}
}

class ChildClass extends BaseClass
{

}

void main()
{

BaseClass objBase = BaseClass.GetInstance();

objBase.Method():
}

No comments:

Followers

Link