Abstract Factory method pattern falls under Creational Pattern.
It is used to create a set of related objects, or dependent objects.
Internally, Abstract Factory use Factory design pattern for creating objects.
If you create an abstract factory Design pattern then it will be an example if
Simple factory & factory Patterns. The implementation of abstract factory
is little bit complex while comparing the implementation of another patterns. This
pattern is called Factory of Factories
Components of Abstract Factory
This pattern is used for several scenarios in an application. Ie your application using different databases & Different DB’s for different requirements. This scenario is one of the best example for Implementation of Abstract Factory Design Patten. Similarly we can use this pattern to implement the logic for export different type data to different format.
Example
AbstarctProducts
Create Call
Components of Abstract Factory
Client : the
client maintains a reference to an abstract Factory class, which all Factories
must implement. The abstract Factory is instantiated with a concrete
factory.
AbstractFactory: the factory is capable of producing multiple types. The objects which can be created still have a parent class or interface that the client knows about, but the key point is there is more than one type of parent.
ConcreteFactory: the concrete factory creates the concrete objects.
ConcreteProduct, AbstractProduct: the concrete objects are returned to the client. Again, the client doesn’t really know what the type of the objects are, just that are a children of the parents.
Usage AbstractFactory: the factory is capable of producing multiple types. The objects which can be created still have a parent class or interface that the client knows about, but the key point is there is more than one type of parent.
ConcreteFactory: the concrete factory creates the concrete objects.
ConcreteProduct, AbstractProduct: the concrete objects are returned to the client. Again, the client doesn’t really know what the type of the objects are, just that are a children of the parents.
This pattern is used for several scenarios in an application. Ie your application using different databases & Different DB’s for different requirements. This scenario is one of the best example for Implementation of Abstract Factory Design Patten. Similarly we can use this pattern to implement the logic for export different type data to different format.
Example
AbstarctProducts
public interface
ISUV
{
string
Name();
}
public interface
IHatchBack
{
string
Name();
}
AbstarctFactory
public interface
ICarFactory
{
ISUV
GetSUV(string suv);
IHatchBack
GetHatchBack(string hatchBack);
}
Products
public class
SportsSuv : ISUV
{
public string Name()
{
return
"SportsSUV";
}
}
public class
RegularSuv : ISUV
{
public string Name()
{
return
"RegularSUV";
}
}
public class
Sedan : IHatchBack
{
public string Name()
{
return
"Sedan";
}
}
public class
hatchback : IHatchBack
{
public string Name()
{
return
"Hatchback";
}
}
ConcreteProducts
public class
HondaFactry : ICarFactory
{
public ISUV GetSUV(string
suv)
{
switch
(suv)
{
case
"SportsSUV":
{
return new SportsSuv();
}
case
"RegularSUV":
{
return new RegularSuv();
}
default:
{
return null;
}
}
}
public IHatchBack GetHatchBack(string
hatchBack)
{
switch
(hatchBack)
{
case
"sedan":
{
return new Sedan();
}
case
"hatchback":
{
return new hatchback();
}
default:
{
return null;
}
}
}
}
public class
MaruthiFactory : ICarFactory
{
public ISUV GetSUV(string
suv)
{
switch
(suv)
{
case
"SportsSUV":
{
return new SportsSuv();
}
case
"RegularSUV":
{
return new RegularSuv();
}
default:
{
return null;
}
}
}
public IHatchBack GetHatchBack(string
hatchBack)
{
switch
(hatchBack)
{
case
"sedan":
{
return new Sedan();
}
case
"hatchback":
{
return new hatchback();
}
default:
{
return null;
}
}
}
}
Client
public class
Client
{
ISUV
suv;
IHatchBack
hutchback;
public
Client()
{ }
public
Client(ICarFactory factory, string type)
{
suv = factory.GetSUV(type);
hutchback =
factory.GetHatchBack(type);
}
public void GetSuv()
{
Console.WriteLine(suv.Name());
}
public void GetHutchBack()
{
Console.WriteLine(hutchback.Name());
}
}
static void Main(string[]
args)
{
ICarFactory
carr= new HondaFactry();
Client
client = new Client(carr,
"sedan");
client.GetHutchBack();
client = new
Client(carr, "SportsSUV");
client.GetSuv();
Console.ReadLine();
}
No comments:
Post a Comment