Abstract design pattern in python

It is a type of creational design pattern method, which is also called the super factory method. It can produce related object classes as many as you want without specifying any particular class. It creates another factory which is why it is called a factory of factories. It is one of the simplest object creation methods and is also considered the best way.it is used when the client wants to create related dependent classes without specifying a concrete class

Problem statement

It is simple. Suppose there is a website of java point where it contains information about all present writers. It contains their name, educational background, age, location, specialization, and area of interest. If you want to know about any writer or all the writers, all you need to do is visit the website and look into the provided information, and you will be able to know everything you need to know about the writer. But there is a developer behind the scene who makes the website and information organized so that you can figure everything out just by looking once into the screen. There are unique classes for all the information like name, educational background, age, location, etc. Hence, as we all know, the company will expand its human resource, and more and more writers will join, and this will happen regularly so that developers will do? Will they add all the information inside the class manually?. Manual class instantiation is not considered good practice for a developer, making the code complex. So, how would we deal with this because we need to add information without manual instantiation?

Answer statement(solution by adding factory method):

We will replace the construction call by calling it through the factory method. The terminology of object creation will remain the same, but it will be called within the factory method. This makes object creation flexible because now we can create as many objects as we want without worrying about the instantiation of objects. We will make a factory which will produce a factory. It simply means creating one object class that can produce another related object class.

Code without using factory method:

Python code:

# Code for object creation in python
# without using factory method
#  Abstract factory method
class writer1:
  def name(self):
    return "Sarita"


  def experience(self):
    return "1 year"


class writer2:
  def name(self):
    return "Sweta"


  def experience(self):
    return "2 year"


class writer3:
 def name(self):
    return "Vikash"


 def experience(self):
    return "1 year"
class writer3:
  def name(self):
    return "Aditya"


  def experience(self):
    return "1 year"
class writer4:
  def name(self):
    return "shreya"


  def experience(self):
    return "1 year"
class writer5:
  def name(self):
    return "abhishek"


  def experience(self):
    return "1 year"


# main method
if __name__ == "__main__":


  writer1 = writer1() 
  writer2 = writer2() 
  writer3 = writer3() 


  print(f'The Name of the writer is {writer1.name()} and his experience is {writer1.experience()}')
  print(fThe 'Name of the writer is {writer2.name()} and his experience is {writer2.experience()}')
  print(f'The Name of the writer is {writer3.name()} and his experience is {writer3.experience()}')

  Output:

The name of the writer is Sarita, and his experience is 1 year
The name of the writer is Sweta, and his experience is 1 year
The name of the writer is Aditya, and his experience is 1 year

Explanation:

Here we have made classes for each writer. Just look at the code for writer 1-

class writer1:
  def name(self):
    return "sarita"
def experience(self):
    return "1 years"

We have defined names and experiences and given them their respective values. Whenever we call these functions, this will return a value. Now the question arises if we want to add new writers, then I have to take another class and add these values manually, so let's see how we can do this by the abstract factory method.

Abstract design pattern in python

This diagram indicates the instantiation of objects which is explained previously.

Python code of the previous example using an abstract design pattern

Exmaple.py

# Python Code 
# the abstract factory design pattern method


import random
class writer_At_jtp:
  def __init__(self, writers_factory = None):
    self.writers_factory = writers_factory
def show_writers(self):
  writers = self.writers_factory()


    print(f'We have a writer named {writers.name()}')
    print(f'his/her experience  is {writers.experience()}')
class writer1:
  def name(self):
    return "sarita"
def experience(self):
    return "1 years"


class writer2:
  def name(self):
    return "sweta"


  def experience(self):
    return "1 years"


class writer3:
 def name(self):
    return "vikash"
def experience(self):
    return "1 years"
class writer3:
  def name(self):
    return "aditya"
def experience(self):
    return "1 years"
class writer4:
  def name(self):
    return "shreya"
  def experience(self):
    return "1 years"
class writer5:
  def name(self):
    return "abhishek"
  def experience(self):
    return "1 years"
def random_writer():
 return random.choice([writer1,writer1,writer2,writer3,writer4])()
if __name__ == "__main__":
  writers = writer_At_jtp(random_writer)
  for i in range(5):
    writers.show_writers()

Output:

We have a writer named Sarita
his/her experience is 1 years
We have a writer named Sarita
his/her experience is 1 years
We have a writer named Aditya
his/her experience is 1 years
We have a writer named Aditya
his/her experience is 1 years
We have a writer named Sarita
his/her experience is 1 years

 Explanation :

Here we have defined the factory class at the top of the code, and all the rest classes are wrapped inside it, so whenever we want to add any class, we can add it, but we do not need to call it manually, so our problem is solved. Just look at the code below-

if __name__ == "__main__":
writers = writer_At_jtp(random_writer)
for i in range(5):
    writers.show_writers()

we have not written any specific writer class here, but in the output, we get all of them through a random call using the random function. Creating objects is also simplified. Hence our problem is solved.

This was a very simple example, so you might think that you will follow the conventional method but think about the scenario, there are a thousand lines of code with two hundred classes, so will the conventional method help you? The answer is no, So using the factory method is good practice.

Advantages of abstract factory method:

  • You can create and add different objects without changing the existing code.
  • Objects of classes can be controlled.
  • It improves the consistency of the construction time of objects.
  • Swapping g of the component family becomes easy.

The disadvantage of the abstract factory method:

  • The future use of applications cannot be predicted precisely.
  • We should be concerned about the interfaces because they affect all the factories.
  • One major disadvantage is that if you want to add a product of a different type that is not related to the rest of the objects and their classes, this can be problematic. Suppose I want to add a director, so even if I will add that class for rendering, it is not possible to relate it to existing code. You need to write separate code for that.

So this was all about the abstract design pattern method. We will discuss different methods in the upcoming parts.



ADVERTISEMENT
ADVERTISEMENT