Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#

Abstract Factory Pattern in C#

Rate me:
Please Sign up or sign in to vote.
2.40/5 (12 votes)
20 Apr 2010CPOL3 min read 33.6K   505   12   6
Abstract Factory Pattern in C#

Introduction

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

image002.gifimage001.gifimage003.gif

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.

Image 4Image 5

  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:
    C#
    public Maruti() : base("MARUTI CAR") { }

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

    Image 6

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

    Image 7

  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.

Tips

C#
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.

History

  • 21st April, 2010: Initial post

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1051082221-Feb-13 22:32
professionalMember 1051082221-Feb-13 22:32 
GeneralMy vote of 1 Pin
bobfox21-Apr-10 14:47
professionalbobfox21-Apr-10 14:47 
GeneralThere's just not enough here Pin
PIEBALDconsult21-Apr-10 6:30
mvePIEBALDconsult21-Apr-10 6:30 
GeneralMy vote of 1 Pin
Shurwint21-Apr-10 6:04
Shurwint21-Apr-10 6:04 
GeneralMy vote of 1 Pin
BigTuna21-Apr-10 5:50
BigTuna21-Apr-10 5:50 
GeneralMy vote of 1 Pin
Vincent Ramp21-Apr-10 2:05
Vincent Ramp21-Apr-10 2: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.