keyboard_arrow_up

The Abstract Factory Pattern : A .NET Design Pattern






The theoretical production line design gives an approach to embody a group of individuals manufacturing plants that have a typical topic without indicating their solid classes. In ordinary use, the client programming makes a solid execution of the dynamic industrial facility and after that, uses the non specific interface of the processing plant to make the solid questions that are a part and parcel of the topic. The client doesn’t have the foggiest idea (or consideration) which solid phenomenon it gets from each of these interior industrial facilities, since it utilizes just the non specific interfaces of their products..


The plan of the unique industrial facility example is to leave the choice of the kind of article to make up to the conceptual being called.


The processing plant example is likely a standout amongst the other most utilized configurations which designs as a part of the C# and .NET environment. It can be seen all over from the ADO.NET database production line directly through to the era of various gaining sorts in ASP.NET.


The plant example is basically used to make an item sort to be overcome with every article sort being made by means of a solitary strategy call. At times, an additional parameter is passed into settle on the kind of item to make.


In the ADO.NET runtime, for instance, an association string is passed in. This association string is utilized to decide the hidden database connector to make, a case of which is then passed back. The returned object transmits a typical impression to other theoretical classes actualized in the same interface, and in this manner it can be trusted to give a 1-1 mapping of essential independents of which database innovation the fundamental library is utilizing.


The way to utilize a manufacturing plant design viably is to keep the customer and other outside components from making the class you need to utilize. This is regularly done by making the class constructor inside to the unique guardian and individuals from the conceptual namespace that utilizes it. By making the construction interior, no outside class can instantly form another duplicate of the class, and accordingly it must be given to the plant’s techniques in theory base to make and hand off a solid usage.


Utilization in C# -


Initially, this process starts with actual abstract factory.


abstract class AbstractFactory
{
public abstract AbstractProduct1 CreateProduct1();
public abstract AbstractProduct2 CreateProduct2();
}


Following below are the dynamic usage of the different classes -


abstract class AbstractProduct1
{
}


abstract class AbstractProduct2
{
}


class FirstProduct1 : AbstractProduct1
{
}


class FirstProduct2 : AbstractProduct2
{
}


class SecondProduct1 : AbstractProduct1
{
}


class SecondProduct2 : AbstractProduct2
{
}


It can be seen, there are two parent abstract classes which forms four final abstract classes.


class FirstProductConcreteFactory: AbstractFactory
{
public override AbstractProduct1 CreateProduct1()
{
return new FirstProduct1();
}


public override AbstractProduct2 CreateProduct2()
{
return new FirstProduct2();
}
}


class SecondProductConcreteFactory : AbstractFactory
{
public override AbstractProduct1 CreateProduct1()
{
return new SecondProduct1();
}


public override AbstractProduct2 CreateProduct2()
{
return new SecondProduct2();
}
}


Taken after, at last, but not the least, by the client that will, at last, utilize them.


class Client
{
private AbstractProduct1 _myFirstProduct;
private AbstractProduct2 _mySecondProduct;


public Client(AbstractFactory myFactory)
{
_myFirstProduct = myFactory.CreateProduct1();
_mySecondProduct = myFactory.CreateProduct2();
}
}


When you have a client prepared to utilize, you can make the utilization of the development chain by utilizing something like the accompanying:


AbstractFactory firstProductFactory = new FirstProductConcreteFactory();
Client firstProductClient = new Client(firstProductFactory);


AbstractFactory secondProductFactory =new SecondProductConcreteFactory();


Client secondProductClient = new Client(secondProductFactory);


For .NET training and placement reviews, please do visit CRB Tech Reviews.


More Related Articles:


.NET Framework 4.0 Tuples


A simplest way to reconstruct .Net Framework