Skip to main content

Abstract Factory design pattern analysis

Abstract Factory


What is the Abstract Factory design pattern? 

The intent or definition of Abstract Factory is


Provide an interface for creating families of related or dependent objects without specifying their concrete classes


It's a bit of a complicated definition, so we'll need to break it down into meaningful chunks to understand it.


1.  "Provide an interface

When we discuss the Abstract Factory, we are referring to interfaces rather than abstract classes. Essentially, this design pattern provides us with an interface, similar to those found in Java, C#, or Typescript (abstract class).  


Why interface?  Whenever we talk about any design pattern, we focus on how we will use it rather than how we will implement it.

Because a concrete class creates tight coupling, whereas an interface isolates it from the rest of the system, an interface is always the better choice. We can get different solutions at runtime without having to update our code.


For example


interface SecurityFactory {

   AuthenticationManager getAuthenticationManager();

   AuthorizationManager getAuthorizationManager();

}

Here we have an abstract factory which defines the behaviour of creating objects related to application security, like authentication and authorization. 

Here we can have different authentication schemes like Basic, Bearer, or Digest, and authorizations like simple or JWT. 

Our client code is unconcerned about which implementation we use, it just uses Abstract Factory interfaces to perform security-related actions.



2. “for creating families of related or dependent objects

The second part states that the Abstract Factory, aka Interface, creates objects.
Interfaces contain methods, and each method should return an object.


AuthenticationManager getAuthenticationManager();

AuthorizationManager getAuthorizationManager();

When we call these methods, we get instances of security objects that are related to a context. Here, the context is security.



3. “without specifying their concrete classes

The last part of the definition states that the return type of each abstract method is an interface, not a concrete class.
  
It makes sense because the Abstract Factory is an interface in and of itself, therefore the return type of each of its methods should also be an interface. 
Clients can now obtain many types of objects from the Abstract Factory without having to make any changes on their end.
  
For example, when I use SecurityFactory like this,


public TestApp(SecurityFactory securityFactory ) {

  AuthorizationManager authorizationManager = securityFactory.getAuthorizationManager();

  if(authorizationManager.isLoggedIn()){
    // allow user to access resource
  }else{
    // notify unauthorized user
  }
}

Here, the object authorizationManager can represent any kind of authorization model, like basic authorization, JWT, or session based authorization.
 
To have different authorization behaviors, we don't need to change anything on the client side. Instead, we add a different securityFactory dependency at runtime.
 
Another example would be as follow.


void AuthenticateUser(String username, String password){

   AuthenticationManager authManager = securityFactory.getAuthenticationManager();

   if (authManager.authenticate(username, password)  == true){
      // User is authenticated
   }else {
     // Invalid User
   }
}    

The authentication model in this situation could range from basic authentication to custom authentication.


This implementation will not impact the client code, it only knows about the interface, not its implementation.


The concrete implementations of SecurityFactory will be the only thing that changes inside your application, this can be done using dependency injection.



Benefits:

Every application requires the construction of a number of objects, each with its own set of creation procedures.

To instantiate objects, some developers prefer new keywords, while others prefer dependency injection, such as singleton.
The first technique leads to tight coupling, which is difficult to verify, and the second way is difficult to maintain and can lead to a verbose constructor definition.


When we group all related objects of an application together, we get only a few abstract factories that are easy to manage and maintain throughout the entire application.
 
AbstractFactory is easy to test. We can create mock objects and test application behaviour easily.
 
AbstractFactory follows the Open/Close principle. We can change the behaviour of an application without updating any client code. 



Example

Security Abstract Factory link
Security Abstract Factory Implementation link


Conclusion:

It is a creational pattern to create a set of related objects. Rather than creating objects with new keywords, which is difficult to maintain and verify, we should encapsulate them within creational patterns. The Abstract Factory is a great pattern to create such related items.
Please let me know what you think of this pattern and how you've implemented it in your app.

Comments

Subscribe for latest tutorial updates

* indicates required

Popular posts from this blog

AJAX - How To Retrieve JSON Response from PHP script

Hello Friends, This is my first tutorial post. Today I'll explain you how to retrieve JSON response via Ajax call. First we build a PHP script to send response in JSON format, then we will gather this response from an Ajax call through jQuery script. Let’s first create a PHP script  Create a PHP script, I’ll name it as “json_response.php” And add following code in it json_response.php <?php //set header for JSON response header("Content-Type: application/json"); //render array in JSON format echo json_encode(array('status' => 200, 'message' => 'Response from PHP script in JSON format.')); This is a basic example to send JSON response. PHP script will send response with status code and message. Now create a HTML file to fetch JSON response and add it in header tag. Create a HTML file, I’ll name it as “json_response.html” And add following code. json_response.html <html>           ...

JSON Tutorials : Getting Started

JSON is widely accepted text formatted structured data. JSON stands for " JavaScript Object Notation ". In general JSON can represent 1. Object of database record. 2. Object to represent a list of HTML elements. 3. Result of search query. 4. Response of an Ajax call. Here you can see JSON is used in many different areas and for many different scenarios. This means it has simple data structure. most of programming languages adopt it and it can flow easily from one connection to another. You can find JSON office definition here JSON Official Site . JSON is represented by two structural types, which includes two primitive types. Structural types Array : A sequential list of primitive data types between square brackets [ ] Object : Collection of key, value pair stored inside curly braces { }, where value would be primitive data type Primitive types : There are two primitive types key and value. " key " should be string and " value (data type)...

Twig Tutorials: Install and Configure

In this tutorial, we'll look at how to install Twig within your PHP project and thereafter configure it so that we can create and use a Twig template inside our PHP web application. Later on, we'll see a simple Twig template example that displays a "Welcome to Twig template" message. Let's take it one step at a time. Install The Twig template can be installed using Composer, Git, or PEAR. In this tutorial, we will use Composer to install Twig. To do so, we'll need to make a "composer.json" file. { "require" : { "twig/twig" : "1.*" } } From the console, run the following command . $ php composer.phar install This command will install Twig library. Configuration To use the Twig template, we should first configure it. To achieve this, we will write an index.php file which will configure the Twig autoloader and generate the Twig environment. We will render the Twig temp...