Python Design Pattern- Creational | Part 1

aps08
4 min readMar 6, 2022

--

Python — One of the most famous language in the industry, and why not, it’s easy to learn, small in size, works on different platform and requires a minimum setup to work on.

Now, in this article we will be deep diving into python and it’s design pattern, so that you can write better and clean code for your clients and projects. I will be covering — (1) What are Design Patterns ? (2) Design Pattern Types (3) Creational Design Pattern Types — in simple words as much as possible.

Keywords — Design Pattern, Python, Creational, Behavioral, Structural

What are design patterns ?

A design pattern is a general repeatable solution to a commonly occurring problem in software design, it’s not a fully developed code which is ready to deploy, but a template to which developers can follow when solving a problem which can be used in many different places.

Design patterns improve the speed of development process and increase the reusability of code, also reusing the design pattern helps us remove the subtle issue that can cause major issues and improve readability for architect who are familiar with the design.

Design pattern types

Now that we know what are design patterns, let’s learn their types. Design patterns are of three types — Creational design pattern, Structural design pattern and Behavioral design pattern. In this article, I will be discussing only creational design patterns and next two will be covered in next articles.

As the name implies, creational design patterns are more concerned about how objects are created and these are very useful for lowering our dependency on other classes and controlling it’s interaction. These are used when the decision must be made during run time. These are further divided into sub categories which we will see in next section.

Creational design patterns types

Let’s see each of the creational design pattern one by one by example:

Factory method : It is used when you are not sure about the objects you will be needing eventually in your system or when the system need to decide which object to call on runtime.

Factory Method

In the above example the object to be called is define during run time, by passing the object name in get_pet() function.

Abstract factory method: This comes from factory method, it is useful when the user expects to receive a family of related objects, but doesn’t have to know which family until runtime.

Abstract Factory Method

In the above example, the family object is passed to the petstore in order to get the desired output.

Singleton : It’s used when you want to allow only one instance to be created for the class, and other classes shall use the data from that instance(borg). In simple word, this just a global variable in an object-oriented way.

Singleton

In the above example, the same variable( _shared_data) is shared between different Objects.

Builder method : The builder design pattern allows the step-by-step creation of complex objects using the correct sequence of actions and excessive number of constructor.

Builder Method

In the above example, each step is performed step-by-step by using constructor to call different classes.

Prototype pattern : It clones object according to prototypical instance. It is useful when creating many identical objects individually. It can also be customized as per the requirement.

Prototype Pattern

In the above example, the object is cloned and return to another variable.

Object pool : It uses a pool of initialized objects that are ready to be used rather than creating a new object all the time. The main idea of an Object Pool is to reuse the object from the pool which are expensive to create in the middle of execution.

Object Pool

In the above example, 10 objects are created a the start and they can be used when needed.

Thank you for reading this article. 😃😃😃 Of course, this information is based on my knowledge. If you like this article give me a clap and feel free to connect with me on Twitter, GitHub and LinkedIn.

My old articles:

  1. Working with git for noobs [Technical]
  2. Website Vs Web Application [Technical]
  3. Lake of Dead [Non-Technical]

--

--

aps08
aps08

Written by aps08

Keep learning, keep growing | 3.5k views

No responses yet