Abstract Factory in C# – Equally useful for manufacturing operations (services) and products

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();

 }

Cloud Advisory Services | Security Advisory Services | Data Science Advisory and Research

Specializing in high volume web and cloud application architecture, Anuj Varma’s customer base includes Fortune 100 companies (dell.com, British Petroleum, Schlumberger).

All content on this site is original and owned by AdverSite Web Holdings, Inc. – the parent company of anujvarma.com. No part of it may be reproduced without EXPLICIT consent from the owner of the content.

Anuj Varma – who has written posts on Anuj Varma, Technology Architect.


Leave a Reply

Your email address will not be published. Required fields are marked *