28 Apr 2020

Patterns


One can say that programming is like building with legos, taking smaller components and build something out of it. After following a good number of lego building instructions, you might notice some have repeat design features. Design patterns in programming are similar to that. Design patterns might not be the all-purpose solution for everything, but they are often the re-usable solutions we need for most of our problems.

Prototype

Prototyping creates a parent class that can’t be implemented on its own, the “unimplemented” methods must be implemented within its child class. This is often used in object-oriented programming for writing less and more understandable code.

In the below example, I’ve created DelayedTask parent class with 2 abstract methods for implementation. The 2 methods to be implemented are actions to be taken on task start and termination, the parent class is responsible for time calculation and general methods. This is used for discord bot reminders and temporary ban features.

Observer

Sometimes in programming, updating one thing will not be enough, multiple objects will need to be updated. If the subject is updated, then call all observer methods within the program.

While I’ve not implemented an observer design pattern before, I’ve used the observer methods provided by Discord.py library. If bot detects the specified event update, all observers under the subject will be called. In this case, all those event updates will be converted into embed and sent into the appropriate log channel.

While design patterns are like powerful cheat tools, it’s important to know each has its advantages and disadvantages. Knowing when and where to deploy these solutions are crucial.