Refactoring and Design Pattern

Best Practices

Danny August Ramaputra
Nov 7 · 5 min read
https://www.thejakartapost.com/life/2018/10/27/jakarta-fashion-week-cultural-heritage-at-the-forefront.html

FleetTracker’s backend service is built using Golang. We also used a framework to ease bootstrapping our prototype. The framework that we use is BeeGo, https://github.com/astaxie/beego. And with this combined, we have implemented some design patterns in our project to streamline the flow of the application.

Design Pattern?

Design pattern is a general solution to a common problem in software design. These are the paradigms/template that developers can implement into their codes, with the objective to solve these recurring problem.

Such patterns are used to speed up development, in the sense that it it a common solution that most developers can mutually understand the software’s workflow. This makes it common to use as a tool to communicate software interactions and ideas between developers, which are well understood.

Best practices and common solutions from developers globally have implicitly yield these design patterns to surface. It is improved over time, making them the best approach to use in solving the problem. Martin Fowler is a software engineer who have documented most of these design patterns into his book ”Patterns of Enterprise Application Architecture”.

Should We Use Them?

If we have a problem that design pattern covers, it is best to use them. They are proven to be the best practices and will save some time from designing a new approach. Otherwise, or if the implementation will conflict with existing software, it okay to not implement these design patterns. Design patterns are not to reshape our code for, they exist to offer best practice solutions.

Implemented Patterns

There are 4 + 1 common categories of design patterns:

  • Creational Patterns

We will explore how we implemented some of these into our software.

Singleton

Singleton is one of the creational design pattern. It is used when we want to have a single instance of an object throughout the lifetime of the application. We usually have them lazily instantiated and have a global access/pointer towards it.

In FleetTracker we have them for holding the components for the handler’s use. These components include a Database connection, Redis connection pool, Message Queue reciever, WebSocket upgrader, the Cronner entity, and the currently active WebSocket connections. These components are accessible to all of the handler functions, making it easy to manage these resources as a whole.

Adapter

Adapter is one of the structural design pattern. It is used when we want to create an interface that links a client’s interface to another interface. It is usually done by wrapping an existing class with another interface.

We use these adapters as a connector from our application to external libraries which does not come with an existing interface. We use there interface for easy maintenance, and for mocking in tests.

Chain of Responsibility

Chain of responsibility is a behavioral design pattern. It is used to allow a series of functions to process a data being passed through one another. This involves multiple handlers and a particular object being passed around.

FleetTracker implements this mostly for error handling. This allows errors to be passed upward to the callers and provide additional handling to the given error.

Object Pool

Object pool is a creational design pattern. This reduces load for handling an expensive to instantiate. We are pooling these instantiated object to allow the resources to be used/shared among the requiring clients.

We use this for handling Redis connections. To commence a Redis connection, there might be some blocking time to load. By using pooling, we are able to cycle around live connections around to the handler modules that need them, or create more if needed.

Asynchronous

Asynchronous pattern, or better known as the asynchronous method invocation, is a concurrency design pattern. In this pattern, functions are not polling for new events, but these methods are event driven, invoked when new messages arrives. And these functions do not block each other when called simultaneously. This is closely related to observer pattern in behavioral category.

We use this for parsing incoming messages from the MQ. Our application is multi-threaded, we use multiple threads to parse the high amount of incoming messages. Each of these threads are not polling for messages, but are triggered to execute a function when a new message enters the queue.

MVC

Model View Controller is a compound design pattern. A compound design pattern is the pattern that is a combination of multiple patterns, working together in a larger scope. This is a commonly used pattern for web applications.

Project File Structure

Model refers to the entities which hold data for transfer of repositories. View refers to the part that handles interfacing with the client. Controller refers to those functions which assembles models and views together, containing most of the software logic.

In FleetTracker, we implement this for our backend WebSocket server. We have controllers (not to be confused with “Controller” from MVC)as the functions that catches requests from the frontend application to be processed by the handlers. These handlers have access to the resources and are being called by either the controller, or event-driven via MQ messages, or scheduled by the Cron. And the data being passed around, including those coming in and out from DB, Redis and MQ, are defined in the datatransfers.

Refactoring

Refactoring is the process of restructuring existing software. As described in my other post about Clean Code, we do refactoring to clean up the smelly codes. We may also do refactoring when implementing design patterns to our existing code.

With the proper use of design patterns, the scope of refactoring that we need to do will be structured and organized, making development faster and more efficient.

That’s It!

That is all about design patterns that we have implemented in our project.

Thank you for reading!

HappyFresh Fleet Tracker

The Big Brother that Watches While You Work

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade