Creational Design Patterns in C# - Abstract Factory Pattern
Abstract Factory Pattern
Abstract Factory design pattern
(The code of the example given below can be found here at github.)
Lets consider an Agency called – CarInfo. CarInfo is an agency to provide consumers information about cars.
CarInfo want to develop a simple webpage where user selects car manufacturer, and then it shows the models of the select car company.
When they start implementing the classes for each company, it realizes that each company has their own set of car models. Also each company has different approach to determine whether a model is hybrid nor not.
So to make things easy – the CarInfo compnay’s software dept develops below two abstract classes –
carCompany - class for storing car manufacturer information. This class will store:
|
|
carModel each car model object that a company would provide should be based on this abstract class. This class would provide
|
|
The agency then sends these abstract classes to each car company and asks them to provide their own derivatives of these calles.
For example sake, lets say the Agency provides infomation of Toyota and Ford models.
CarInfo agency sends these above abstract classes to Toyota, and Ford and asks them to provide their own derivations of these classes.
While agency was waiting for responses from Toyota annd Ford, it actually started in its UI. On the UI they added two button links on the page – Toyota and Ford – and instruct user to choose one of these company buttons to find its list of models.
|
The Agency could code in advance of getting the actual implementation of the concrete classes because they were aware of the abstract class on which the concrete classes wouldbe based on.
Next day – Toyota and Ford provide the below concrete classes:
|
|
|
|
The agency then simply added these concrete classes from Ford and Toyota to their project class lib and ran their UI form..and voila ..it worked. Everything fell into place and worked neatly because all parties followed or implemented code as per the blue-print that the abstract classes provided.
This is in nutshell the Abstract Factory design pattern.
This youtube video explains Abstract factory pattern with another example of .net data provider classes.
Prev: Creational Design Patterns |
|
Next: Builder Pattern |