Decorator Pattern And It's Importance

Decorator Pattern And It's Importance

Ever wondered how while playing Mario Mr. Mario becomes Super Mario after having a mushroom? Ever thought about how would you code such a requirement in a project or game?

Here's my thought on various ways how to design such a requirement on code:-

1. Design a complex chain of inheritance with all the combinations possible for Mr Mario and keep them handy while compiling the code.

2. Develop a separate class for each of the possible Mario possible in the game.

3. Use the wonderful decorator pattern to achieve the above requirement.

The problem with the first and the second solution is that it adds to the complexity of the codebase and is very difficult to maintain. It violates almost all the SOLID principles and thus is not recommended at all for any real-life project.

So what's the solution? Well, this is where the decorator pattern comes to our rescue. So without any further due let's get into it.


Decorator pattern significance and definition

Definition:- Decorator pattern attaches additional responsibilities to an Object dynamically at runtime. Decorators provide a flexible alternative to subclassing for extending the functionality.

Significance of Decorator pattern:-

  • Runtime enhancement:- Decorator pattern is used to enhance the behaviour of the code at runtime instead of compile time.This gives us the ability to modify, upgrade, and degrade our brother Mr. Mario while the code is compiled and running. making it much more flexible for our servers to run and reducing time complexity.
  • The decorator pattern works very closely with the Open/Closed principle one of the key principles of SOLID principals. And ensures that our code follows the Open/Closed principle.Open/Closed principal: The code should be open for extension and closed for modification. well, this is just a formal definition but in reality, what it means is that if we define a class for a requirement and then later in SDLC (Software development life cycle) some other requirements come. The code of this class should not be modified rather it should be extended for new use.
  • Q. Why follow the Open / Closed principle?

Ans:- Too much time is spent in companies and firms to make a piece of code work in the expected way. So it should not be touched or modified rather it should be extended for new use instead.

  • The decorator pattern belongs to the Structural design pattern family of the design patterns family.
  • Multiple Decorators:- Multiple new decorators could be wrapped around an object. Enabling us to incorporate a large number of features without modification of existing code.
  • Same Supertype:- To understand let take a example from our daily life a normal football and a FIFA football both can be used interchangeably to play football similarly The object and the decorated object both have the same supertype and can be used interchangeably in the codebase.

Well, so much for the various advantages of our beautiful decorator pattern now it's time to understand various core decorator components and implement decorator patterns in Python.


Components Involved and Implementation in Python

  • Component Interface:- This can be thought of as what the skeleton is to the human body. In technical terms, this interface needs to be implemented by both base objects like in our case MR. Mario and the decorator objects in common

Component Interface in Python


  • Concrete Component:- What Mr Mario is there in our case is that a concrete component basically any object which inherits the component interface and implements its methods in accordance is the concrete component

Concrete Component in Python

  • Decorator Interface:- The heart of our pattern it inherits the Component interface takes an instance of the object and defines the structure which needs to be followed by all the decorators

Decorator Interface in Python

.

  • Concrete Decorator:- The objects which transform our Mr Mario into Super Mario. These objects add additional functionality to concrete components and they inherit their base structure from the Decorator interface. Multiple concrete decorators can be placed around the same concrete component.

Concrete Decorator and the main function code in Python

For the ER diagram please refer to the diagram below:-

ER diagram of the Decorator pattern

Advantages and Disadvantages of Decorator Pattern

Advantages:-

Well, mostly what's mentioned in the significance section above but just to have a quick recap. Listing them again below:-

  1. Open/Closed Principal:- The decorator pattern lets us implement or extend the behaviour of our objects without doing new subclassing.
  2. Runtime extension:- Runtime addition and removal of responsibilities and effects is made possible from the decorator pattern.
  3. Multiple layers support:- Multiple decorators can form layers around a component to apply multiple roles and responsibilities to the same component.
  4. Single Responsibility Principal:- By dividing the code into several small components and decorators we make sure that each one implements and handles a single responsibility.

Disadvantages:-

Well as it is said no one is perfect except Lord Krishna so is applicable to our bro Decorator pattern.

So here are some of the disadvantages which I feel are brought by using it in our codebase.

  1. Specific Removal of Decorator:- It's really hard to remove a specific decorator from a series of decorators applied to our component. Removing specific element from a stack is complex and has higher time complexity.
  2. Behavior Order Free:- It's complex to implement a decorator pattern in code which is free from the order of decorators applied on the component.
  3. Initial implementation might look complex and difficult to understand.


Things to keep in mind while using and implementing the Decorator Pattern

Some crucial things should always be kept in mind while interacting and using decorator patterns in our code. Which includes.

  1. Just as in our example of MR Mario, we should be sure that our code can be fragmented into layers of small code of the same supertype which is crucial for decorator pattern implementation.
  2. Both the component and Decorator should have the same base class interface and should be compatible to be used interchangeably.
  3. Do a Proper analysis of the code to find and debug the common code places for the base class creation.

Well, so this was a deep dive inside our beloved decorator pattern. Make sure to read other articles of mine.


Design patterns are crucial for maintaining clean and efficient code, especially in game development. It's fascinating to see how the decorator pattern can simplify complex requirements like transforming Mr. Mario into Super Mario. Looking forward to reading your article and learning more about its implementation in Python!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了