Python Design Pattern- Structural | Part 2

aps08
4 min readMar 26, 2022

Welcome back! This is part 2 of Python Design Pattern Series, if you still haven’t read the part 1, I encourage you to give it a try as I have covered what is design pattern and it’s types in my part 1 of this series.

In this article I will be covering (1) What are structural design pattern (2) It’s types with simple examples. I am not gonna bore you with a lot of theory, so I will give a short summary for each topic and some code so that you can understand it quickly in an easy way. In case you want to read more about it, please follow this link.

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

What are structural design pattern

This design pattern is about organizing different classes and objects in order to create a new larger structure and increase the functionality, efficiency and flexibility.

It is usefully when you are creating a layered code which requires regular maintenance, especially when working with external libraries, legacy code or numerous objects.

Types

Decorator — It’s a structural pattern that allows the users to add new features to existing objects. The idea is to create a wrapper which conforms to the same interface as the class we’re wrapping, but overrides its methods.

Decorator Example

In the above example, hello_world() is a normal function, make_blink() is a function which is used to add functionality to the hello_world() function without making any changes into it. @ make_blink is a decorator which is used to call the make_blink() function, as a argument it also takes the hello_world() function. @ wraps is a decorator for decorator() function where the passed argument is used.

Proxy— Proxy becomes important, when making a highly intensive resource objects. It is used to postpone object creation unless absolutely necessary.

Proxy Example

In the above example, producer is a intensive object, and the proxy class makes sure that it is created after particular amount of time, or when absolutely necessary.

Adapter — The adapter converts the interface of the class to another one, which the client is expecting. It converts incompatible interface to compatible interface.

Adapter Example

In the above example we have English and Korean classes, and both inherits the Speaker class, which contains only one abstract method called type(). Coming to the Adapter class, which contains the constructor for saving object and function value in a key called speak, and __getattr__() to get the values for a respective object. In the last line of the code, inside the print statement, we have used the key (“speak”), which we had defined during adapter object initialization to get the desired output.

Composite — The composite design pattern maintain a tree data structure to represent part-whole relationship. Here, we want to make recursive data structure, so that element of the tree can have its own sub-elements.

Composite Example

In the above example, the Child and Composite class inherits the component class, and they have component_function() common. In the the loop statement of Composite’s class component_function(), for each object the component_function() of Child class is called.

Bridge — When working with multiple large classes, it may be possible that classes violate the single responsibility principle, in this case bridges helps to untangle an unnecessarily complicated class hierarchy.

Bridge Example

In the above example, DrawAPIOne and DrawAPITwo are two classes to draw a circle. The Circle class constructor takes co-ordinates and one of the previous class objects as input. The draw() uses the class object passed in constructor to call the methods of these classes.

Other then these 5 structural pattern, there are Facade and Flyweight, you can read about them on the link given in the starting of the article. 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 in case of any doubts.

My old articles:

  1. Python Design Pattern- Creational | Part 1[Technical]
  2. Working with git for noobs [Technical]
  3. Website Vs Web Application [Technical]

--

--