PPL2021 — Design Pattern & Refactoring — Why Pattern Could ImproveYour Development Cycle

Fredy Pasaud
6 min readMay 22, 2021
Photo by Silvio Kundt on Unsplash

“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” [Christopher Alexander, A pattern language, 1997.]

Have you ever realise that pattern is embedded in our daily life. For example, your sleep pattern, you slept at 10.00 PM and woke up at 05.00 AM. You feel that pattern work for you because you have to go to work at 07.00 AM, and you arrived at home at 06.00 PM and need to do some relaxing and other things. And also, your daily pattern could be adapted by other people with the same schedule and responsibilities as you. In this article, we will discuss how design pattern could be applied to your code. Applying tested and proven pattern could shorten your development time and also guaranteed that solution will always work.

What is Design Pattern?

According to Gamma, Object-Oriented design is hard, designing a reusable Object-Oriented design is even harder. Every time you found an obstacle in your code, it will be harder to design a new solution rather than using an existing solution that has been proven to work before.

In short, Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.

Benefits of Design Pattern

According to Pittsburg University, the benefit of Design Pattern is:

  • Patterns inspire a solution for the developer.
  • Patterns will capture expert knowledge and design tradeoffs to make this knowledge widely available.
  • Patterns will enhance understanding of software by documenting the architecture of a system.
  • Design pattern enables large-scale reuse of software architectures.

Categories of Design Pattern

According to reference book about design pattern there is 23 design pattern which can be categorised into three categories, there are :

Creational Patterns

The creational pattern provides a way to create an object while hiding away the logic behind it. This gives the program more flexibility in deciding which objects need to be created for a given use case. Example of a creational pattern is Factory method, abstract factory, builder, prototype, and singleton.

Structural Patterns

Like the name suggest structural pattern concern about class and object composition. The concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities. An example of structural patterns is Adapter, Bridge, Composite, Decorator, Facade, Flyweight, and Proxy.

Behavioural Patterns

Address problems associated with the assignment of responsibility between objects and how communication is affected between objects. An example of behavioural patterns is a chain of responsibility, command, iterator, mediator, moment, observer, state, strategy, template method and visitor.

Design Pattern in Django

HttpRequest

Django uses HttpRequest to encapsulates a request in an object. This implementation follows the command pattern. In general, Django knows the basic five HttpRequest. There are GET, POST, DELETE, PUT, and PATCH.

Here are my implementation of Django HTTP Request in my PPL project :

HttpRequest Implementation in Django

As you can see in the highlighted code, the logic of the code will be executed when Django receive a POST request.

Signals

In general, Django also implements Observer Pattern. It means when one object changes state, all its listeners are notified and updated automatically. For example, every time a buyer changes their address in the code below, the listener (in this case, form and object models) will be changed accordingly.

Signals implementation in Django

Class-based generic views

Django also implements templated pattern in its algorithm. For example, you can subclass an AbstractUser class without change the algorithm structure. So it means you can adjust user capabilities according to your needs without making a new authentication model from scratch.

Template implementation in Django

Model-Template-View Architecture

In general, Django adopts MVC architecture with some modified part. Rather than using view as the template and controller. Django is using views as the controller and template loader for the interface (usually in the shape of HTML). Django is keeping the model architecture for their database.

Django Models for Database
Django Views for Controller
Django Templates for view

In short, Django views will call the models for any logical data structure. After the data is formatted and read by the views, the views' logic will call the corresponding HTML template to present its data. This cycle will happen continuously every time user makes a new request to the system. For better illustration, see the picture below.

Django MTV Implementation

There are still many Pattern that was implemented in Django. I could not cover all that pattern because this article will get so long that you probably won’t read it.

Refactor in Django

Now that we know what the Django pattern is. I will discuss shortly about refactoring and its importance when implementing your code. The basic idea of refactoring is you can’t always write your code perfectly for the first time. In your first implementation, you may write unused variables, random variables naming, importing unused package, and so many more. Because as we know that Django uses abstraction, listener, template patterns. We can always make sure that we can change our implementation later without worrying our new implementation will break our initial code. An example of refactoring in my project can be seen below :

Refactoring in My Project

As you can see, the red part of the code is the old implementation, while the green part is the refactor implementation.

--

--

Fredy Pasaud

Under-graduated Students Majoring in Computer Science