Abstract Factory Pattern in C#






2.40/5 (12 votes)
Abstract Factory Pattern in C#
Introduction
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:
- Data
- Stamp
- Control
- Common
- 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.
- 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
- Structural: This pattern deals with the Classes and Objects composition.
- 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.
- Create
interface ICar
with two declared methods (Attach
andDrive
) - Create
CarBase abstract
class which implementsICar
interface (Attach
andDrive
is implemented in this class) - Create
Ford
class inherited fromCarBase
. CreateMaruti
andHyundai
respectively:public Maruti() : base("MARUTI CAR") { }
The above code doesn’t have implementation, but it calls its parent’s class’ single parameter constructor.
- Create interface
ICarFactory
with a single method (CreateCar
). - Create
FordFactory
class havingICarFactory
implemented. CreateMarutiFactory
andHyundaiFactory
respectively. 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