Design Pattern-Factory Pattern


Here i would like to Explain one of Gang of Four Design patterns Factory Pattern which is a solution to common problems that occurred in Software Designing which falls under the Creation Design patterns.

Design patterns are general reusable solutions to common problems that occurred in software designing. There are broadly 3 categories of design patterns, i.e., Creational, Behavioral and Structural.

Factory Design Pattern falls under the category of creational design pattern.It deals with the problem of creating objects (products) without specifying the exact class of object that will be created.

Implementation of Factory method




abstract class Factory
{
public abstract Product Getinstance(); //Factory Method Declaration
}


class FactoryforProcuct1 : Factory
{
public override Product Getinstance() //Factory Method Implementation
{
return new Product1();
}
}


class FactoryforProcuct2 : Factory
{
public override Product Getinstance() //Factory Method Implementation
{
return new Product2();
}
}


interface Product
{
void RetriveDetails();
}

class ProductA : Product
{
public void RetriveDetails()
{
//Implement Logic
}
}

class ProductB : Product
{
public void RetriveDetails()
{
//Implement Logic
}
}


protected void Buton1_click(object sender, EventArgs e)
{

Factory[] objFactories = new Factory[2];
objFactories[0] = new FactoryforProcuct1();
objFactories[1] = new FactoryforProcuct2();
foreach (Factory objFactory in objFactories)

{
Product objProduct = objFactory.Getinstance();
objProduct.RetriveDetails();
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: