PPL2021 — Design Pattern & Refactoring — Why Pattern Could ImproveYour Development Cycle
“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 :
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.
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.
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
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 :
As you can see in the highlighted code, the logic of the code will be executed when Django receive a POST request.
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.
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.
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.
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.
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 :
As you can see, the red part of the code is the old implementation, while the green part is the refactor implementation.
Design Pattern - Overview
Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns…
Design Patterns and Refactoring
In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software…
What is a pattern? - Django Design Patterns and Best Practices - Second Edition
What is common between the words blueprint, scaffolding, and maintenance? These software development terms have been…
Slide Design Pattern RPL Semester Gasal 2020