Click here to Skip to main content
Click here to Skip to main content

Abstract Factory Pattern in C#

, 20 Apr 2010
Rate this:
Please Sign up or sign in to vote.
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.

  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.

Tips

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)

About the Author

RishiGanesh

India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMd. Humayun Rashed21-Feb-13 22:32 
GeneralMy vote of 1 Pinmemberbobfox21-Apr-10 14:47 
GeneralThere's just not enough here PinmvpPIEBALDconsult21-Apr-10 6:30 
GeneralMy vote of 1 PinmemberShurWint21-Apr-10 6:04 
GeneralMy vote of 1 PinmemberBigTuna21-Apr-10 5:50 
GeneralMy vote of 1 PinmemberVincent Ramp21-Apr-10 2:05 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 21 Apr 2010
Article Copyright 2010 by RishiGanesh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid