Now a days, design pattern are treated as a complex architecture patterns where it is thought that it is accessible and understandable to only experienced developers or designers. NOT TRUE. If you understand the basic concepts of any Object Oriented Programming, you can understand and implement any of the design patterns.
Lets start with Creational Pattern | Abstract Factory Pattern.
Objective of this pattern is to provide only a way to create related objects. Let’s take a closer look.
Implementation
Lets assume that you own an Alienware Gaming laptop modelling house. You provide the best gaming laptop and offer two models M14 and M17.
My 2 favourite locations are Paris and Amsterdam. So lets assume that we provide the products assembled at those locations only.
In the above picture, left, we have the factory where we have two products M14 and M17 which are delivered in the two locations (Paris,Amsterdam). This is just provide the interface. The actual configuration and delivery process is hidden here. This will be there in the Actual Product and Assembling Implementations only.
You can specify any specific process for any product in any location without the need to changing anything in abstract layer.
Lets start with working example. As I already explained the scenario above, I have created a sample project for this pattern which you can download using the link at the bottom.
The project hierarchy is as below:
Now, lets look at the first class which is “AbstrackAlienwareM14”. this class is an abstract class for AlienwareM14 models which can be increasing in future as we all know you, me and lot others like Alienware.
The basic methods for M14 are “getConfigurations” and “preloadedApps”. The implementation of such methods will be in actual concrete implementations.
Now this is the actual concrete implementation of M14 Alienware model. This implements the two operations required for M14. I have put some logic based on Delivery location about the specification as I have experienced the same.
This is a similar implementation for M14 as above.
This is the abstract class for M17 which can be used for other upcoming new models of M17 Alienware class.
Now, lets get started with the usage of these all aliens we have created. I am created a very basic Factory “AlienFactory” which will just let you order Alienware 14 or 17 models for real! “Just Kidding”.
This is an implementation of the AlienFactory based on location. As in our example we have two locations I have created below two concrete implementations of the Factory – Paris and Amsterdam.
Finally the complete pattern design is in place now. Now the actual client side implementation of using the pattern that we have created.
Below is the code to order M14 or M17 in both the locations.
We have observed that the definition interface is kept separate from the actual implementation of those interfaces. In this context how the laptops gets delivered in various locations is separate from the product configuration and delivery.
This is very basic pattern and is used most often in creating libraries and helper objects. If implemented efficiently, this pattern can bring lot of code re-usability to any application.
Hope this helped you understand the pattern better.
For your reference, I have implemented the above example using JAVA. Have a look HERE.
[…] is the second pattern that I am going to discuss after Abstract Factory. Now, if you have read the description of this pattern, you will feel like it is similar to […]