Refactoring and Design Patterns

Nardiéna Althafia Pratama
HappyFresh Fleet Tracker
3 min readNov 22, 2019
https://www.udemy.com/course/react-design-patterns-for-beginners/

What is Refactoring?

Refactoring is the process of improving source code without adding any new functions, which will transform the messy code into clean code and simple design.

Refactoring is done in three cases: when adding a new feature; when fixing a bug; and during a code review.

Design Patterns

Design patterns are general repeatable solutions to a commonly occurring in software design. These patterns aren’t transformed directly into code, rather they are templates for how to solve a problem that can be used in many situations in the project.

Design patterns help speed up the development process, especially in areas of maintenance and code reuse.

I will be talking about how we use some of the design patterns in our project, FleetTracker.

MVC

Model-View-Controller Pattern is a compound design pattern that is used to separate the application’s concerns.

Project Structure

Model represents an object that carries the data. View represents the visualization of the data that the model contains. The Controller controls the data flow into the model object and updates the view whenever the data changes.

In our project, the data that is used is defined in the models and datatransfers. The controllers (equivalent to views) are the ones that catch requests from the front-end side and process them in the handlers. The handlers (equivalent to the controller) have access to the front-end resources and are called by the controller, MQ, or the Cron.

Singleton

Singleton is a creational design pattern. It’s a pattern that restricts the instantiation of a class to one “single” instance. It’s useful when exactly one object is needed to coordinate actions across the system.

In our project, we have instantiated the components used in the handlers in the init.go (as shown from lines 3 to 9). These components can be accessed from all the other handlers. In lines 37 to 54, the instantiated components are set to certain values from the methods put in it. Using this pattern makes it easier to manage all the resources.

Chain of Responsibility

Chain of Responsibility is one of the behavioral design patterns that let you pass requests along a chain of handlers. When the handler receives a request, it decides to either process the request or pass it to the next handler in the chain.

In our FleetTracker project, this pattern is applied in error handling. In our exception handlings, the errors are passed to the callers and are then sent to the next handler for that particular error.

So these were some of the design patterns used in our project. Hopefully, this article has been useful. Thanks for reading!

Sources:

--

--