Click here to Skip to main content
12,822,888 members (48,544 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 20 Apr 2010

Abstract Factory Pattern in C#

, 20 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Abstract Factory Pattern in C#


We all know about data flow diagrams. It shows how the data flows between processes.


A process is a collection of methods or a single method. It is identified based on the complexity.

But when a method sends a data (as a parameter) to another method, then it is called coupling.

There are five different types of couplings:

  1. Data
  2. Stamp
  3. Control
  4. Common
  5. Content

These can be achieved by “divide and conquer” principle.

Identify the methods, setting the communication between the methods, what information should be passed as parameters, etc.

Now we can understand that, a method is a part of a big process. A method also silently tells that it is reusable and scalable.

Gang of Four is a four member team who studied these techniques closely and identified design patterns.

They have classified the design pattern into three groups: Creational, Structural and Behavioral.

  1. Creational: This pattern deals with instantiation of the classes. It is further divided into classes’ creation and objects creation patterns.
    • Class Creation uses inheritance concept of OOPS
    • Object creation uses Delegation of OOPS
  2. Structural: This pattern deals with the Classes and Objects composition.
  3. Behavioral: This pattern deals with the communication between objects.

Abstract Factory Pattern falls into Creational Design Pattern.

This pattern encapsulates the process of instantiating the family of classes.

Writing code like UI/Code behind, DB connected, fetch and display the data – everything is not completed.

How we write the code, how it is effectively understandable, easy to reuse, scalable for lateral changes, reducing the cost of compilation and deployment, ease of maintenance, meeting the cut end requirements, dynamic changes, etc. are achieved by the Architecture of the project. The project architecture is built on the foundation of the design patterns and every design pattern is built on the OOPs concepts.

We are going to see Abstract Factory Pattern in this article.

Let us take a Car manufacturing example.

Maruti, Ford and Hyundai are three different cars we plan to manufacture. For each of the cars, let us have CarBase abstract class which is inherited from ICar interface (Even we can aggregate these into a Class Library project).

Similarly we need to create Factory classes for the respective Car classes and all these factory classes share a common interface called ICarFactory. We create one common class called CarManufacture, having ICarFactory used for implementation. CarManufacture class’s constructor accepts instance of ICarFactory interface’s object.

By this, we hide, what should be created on what condition is completely hidden in assemblies.

  1. Create interface ICar with two declared methods (Attach and Drive)
  2. Create CarBase abstract class which implements ICar interface (Attach and Drive is implemented in this class)
  3. Create Ford class inherited from CarBase. Create Maruti and Hyundai respectively:
    public Maruti() : base("MARUTI CAR") { }

    The above code doesn’t have implementation, but it calls its parent’s class’ single parameter constructor.

  4. Create interface ICarFactory with a single method (CreateCar).
  5. Create FordFactory class having ICarFactory implemented. Create MarutiFactory and HyundaiFactory respectively.

  6. CarManufacture class is also built in inside the same assembly. But here we are extending the functionality to Sub Manufacture and in turn, it is called from the Main method.


ICarFactory mycar = new FordFactory();

The above statement instantiates FordFactory and the memory address created by them is stored in a variable mycar of ICarFactory interface.

The major advantage of this system is that it just hides the process of creating the instances of the family of classes.

About Myself

This is Rishi Ganesh A, working as a Project Leader in Mumbai, India. This is my first article.


  • 21st April, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Md. Humayun Rashed21-Feb-13 23:32
memberMd. Humayun Rashed21-Feb-13 23:32 
GeneralMy vote of 1 Pin
bobfox21-Apr-10 15:47
memberbobfox21-Apr-10 15:47 
GeneralThere's just not enough here Pin
PIEBALDconsult21-Apr-10 7:30
mvpPIEBALDconsult21-Apr-10 7:30 
GeneralMy vote of 1 Pin
ShurWint21-Apr-10 7:04
memberShurWint21-Apr-10 7:04 
GeneralMy vote of 1 Pin
BigTuna21-Apr-10 6:50
memberBigTuna21-Apr-10 6:50 
GeneralMy vote of 1 Pin
Vincent Ramp21-Apr-10 3:05
memberVincent Ramp21-Apr-10 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 21 Apr 2010
Article Copyright 2010 by RishiGanesh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid