Design patterns are an important concept for junior-level engineers to understand as they approach mid-level. When first learning how to program, there is much emphasis on syntax, and how to accomplish a given task with code. This becomes easier with time, and then it becomes more important to understand more efficient ways to structure your code.
Building a functioning application that is full of spaghetti code may feel like an accomplishment at first, particularly if it was a true test of your abilities. Eventually, however, maintaining it becomes a nightmare.
Software design pattern
"In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code."
Design patterns can be thought of as architectural blueprints. Though every building is different, there are different conventions that are followed depending on the type of building being designed. Hospitals, schools, and residential buildings will all follow their blueprint conventions for appropriate construction.
An exhaustive list of design patterns is beyond the scope of this blog, but here are some popular examples:
Singleton pattern: Ensures that only one instance of a class is created and provides a global point of access to it.
Observer pattern: Defines a one-to-many relationship between objects so that when one object changes state, all of its dependents are notified and updated automatically.
Factory pattern: Provides an interface for creating objects, but allows subclasses to decide which class to instantiate.
Strategy pattern: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows the algorithm to vary independently from the clients that use it.
Decorator pattern: Allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.
Adapter pattern: Converts the interface of a class into another interface the clients expect. It lets classes work together that couldn't otherwise because of incompatible interfaces.
Command pattern: Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Facade pattern: Provides a simplified interface to a larger body of code, such as a class library.
Template method pattern: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Iterator pattern: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Aside from choosing the most appropriate design pattern for your project, it is important to understand that you stand to gain from properly implementing a design pattern. A well executed design pattern will faciliate an engineer's ability to create efficient, scalable and maintainable code. This is a proven safeguard against commonly occuring problems in software applications, and allows engineers to focus on technical challenges.
Next up in this series, we will take a closer look at the Singleton pattern.