This article assumes some familiarity with the Factory pattern. The factory concept is equally useful for creating products (ProductFactory) or for creating services (ServiceFactory). A good way to start is to look at what the client really needs from the factory.

Product Factory Example – Client code

 // Create an instance of the concrete factory.
 // This is the only concrete class that the client needs to know about.
 // Everything else needed by the client remains abstract
AbstractProductFactory absProdFactory = new ConcreteProductFactory();
AbstractProduct absProductA;
AbstractProduct absProductB;

absProductA = absProdFactory.CreateProductA();
absProductB = absProdFactory.CreateProductB();
string pA1 = absProductA.ProductProperty1();
string pA2 = absProductA.ProductProperty2();
string pB1 = absProductB.ProductProperty1();
string pB2 = absProductB.ProductProperty2();

 

Service Factory Example – Client code

static void Main(string[] args)
{
// Create an instance of the concrete factory – this is the only concrete class that the client needs to know about.
// Everything else needed by the client remains abstract
          AbstractFactory absFactory = new ConcreteFactory();
          AbstractService absServiceA;
          AbstractService absServiceB;

          absServiceA = absFactory.CreateServiceA();
          absServiceB = absFactory.CreateServiceB();

          string sA1 = absServiceA.serviceOperation1();
          string sA2 = absServiceA.serviceOperation2();

          string sB1 = absServiceB.serviceOperation1();
          string sB2 = absServiceB.serviceOperation2();

 }

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.