“Provide an interface for creating families of related or dependent objects without specifying their concrete classes”
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).
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.
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”
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”
public TestApp(SecurityFactory securityFactory ) { AuthorizationManager authorizationManager = securityFactory.getAuthorizationManager(); if(authorizationManager.isLoggedIn()){ // allow user to access resource }else{ // notify unauthorized user } }
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.
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.
Comments
Post a Comment