factory pattern c# Archives - Anuj Varma https://www.anujvarma.com/tag/factory-pattern-c/ Production Grade Technical Solutions | Data Encryption and Public Cloud Expert Sun, 24 Nov 2013 08:15:06 +0000 en-US hourly 1 https://wordpress.org/?v=7.1.5 https://www.anujvarma.com/wp-content/uploads/anujtech.png factory pattern c# Archives - Anuj Varma https://www.anujvarma.com/tag/factory-pattern-c/ 32 32 Abstract Factory in C# – Equally useful for manufacturing operations (services) and products https://www.anujvarma.com/abstract-factory-equally-useful-for-manufacturing-operations-services-and-products/ https://www.anujvarma.com/abstract-factory-equally-useful-for-manufacturing-operations-services-and-products/#respond Wed, 21 Sep 2011 22:13:26 +0000 http://www.anujvarma.com/abstract-factory-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…

The post Abstract Factory in C# – Equally useful for manufacturing operations (services) and products appeared first on Anuj Varma.

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

 }

The post Abstract Factory in C# – Equally useful for manufacturing operations (services) and products appeared first on Anuj Varma.

]]>
https://www.anujvarma.com/abstract-factory-equally-useful-for-manufacturing-operations-services-and-products/feed/ 0