The Blueprints of Programming

29 Apr 2021

Design Patterns

In computer science, design patterns are general, reusable solutions to commonly occurring problems. This idea is analogous to the practice of building a house. Design patterns are general blueprints whereas the problems are houses equipped with different features or floor plans. For example, the floor plan for a cabin is much different in design from the floor plan of a beach house. As such, a specific blueprint is required for each. Additionally, you can edit a design pattern to your liking much like how you can edit a blueprint to build the house of your dreams. Using a design pattern correctly can save a lot of time and effort on a problem that someone has already solved, especially for new programmers.

There are many design patterns for different kinds of applications such as the Factory, Singleton, Observer, and Model-View-Controller (MVC) design patterns. In fact, we sometimes use them without even noticing. If you are implementing event handling systems, you are using the Observer design pattern which is defined by an object, named the Subject, that notifies other objects, named Observers, of any state changes. If you are building a user interface, you would apply the MVC pattern to separate the internal representation of information from what is presented to the user. As previously mentioned, what makes design patterns so great is their versatility to be applied to problems as you see fit.

Experience with Design Patterns

Prior to learning of their existence, I have used design patterns in my own code. Most recently, my Music Match project for ICS 314 utilizes multiple design patterns in different parts of the application. For example, my group and I implemented the Observer design pattern to allow the client and server to communicate in a “request-response” fashion. Using the Meteor framework, an API on the server has a set of data constructed, called a publication, that is sent to the client. The client initiates a subscription that connects to a publication and receives an initial batch of this data. The client will receive incremental updates as the published data changes, thus utilizing the Observer design pattern.

Another design pattern I’ve used is the Prototype pattern which is characterized by defining a prototypical instance for a type of object you want to create and cloning the prototype to produce new objects. As an example, let’s say you wanted to create a Dog object. Well you could define a Dog class and provide member variables to define the number of legs it has, the number of tails, its color, etc. Perhaps later on, you want to create a Cat object. Now you must define a Cat class and again provide member variables to describe the characteristics of a Cat, which in this case is extremely similar a Dog. To make your program more efficient, you can implement the Prototype design pattern by defining a prototype class called Animal that has the same member variables or characteristics as a Dog and Cat would. Thus, you could clone the Animal class to create Dog and Cat objects, as well as other types of animals, more efficiently.

I have used this design pattern and others on countless occasions throughout my academic career without even realizing it as it is so common among programmers to take general solutions and ideas and apply them to their own problems or projects. I think it is necessary to teach design patterns to beginner programmers because ultimately, I believe this concept is teaching programmers the foundations for problem solving and critical thinking so I am grateful for having learned the basics of design patterns. Why struggle with designing a house from scratch, when you can implement a blueprint that someone has already designed and make it your own?