Design patterns, solutions to common problems.

When working in software development, you will often come across the same problems over and over. A design pattern provides a solution for these recurring problems. A design pattern’s main goal is to speed up development by providing well tested solutions. Design patterns, rather than being a block of code, they are a concept of how the problem should be solved, meaning, they are programming language independent. A challenge that commonly arises is knowing which design pattern to use. You will have to know the purpose of design patterns if you want to use them properly. One example of a design pattern are singleton classes, which are classes that there can only be one instance of. These may be needed in cases where there’s only one person in a position, such as the CEO of a company.

Design patterns can be classified into three types. First, creational design patterns which have to do with class instantiation. Here are some examples of these types of patterns:

  • Factory method: an interface for object creation is made, but what actually decides which object is made are its subclasses. It’s like a virtual constructor.
Scheme of Factory Method

Image source

  • Object pool: the goal is to reuse objects. When an object is not being used anymore, it gets added to a pool and when a new object needs to be created, one from the pool is selected. This can give a performance boost.
  • Singleton: already mentioned before, it’s used when a class can only have one instance. A class handles creating, initializing and accessing the instance. The actual instance is a private variable.

Second, there are structural design patterns. These patterns organize classes and objects to create larger structures, inheritance is commonly used for this type. A popular design pattern in this category are adapters. Adapters convert an interface of a class into another interface so it can be used by other programs. It’s similar to the adapters that we use for VGA to HDMI.

Third, behavioral design patterns. These patterns are all about how objects communicate with each other. An example of this type of design pattern is the template method. This pattern defines only the skeleton of a class and all of its core functions are done by external classes. This way, you can change how a certain step works by only changing one of these external classes.

Template Method scheme

Image source

And on the completely opposite side of the spectrum, we have anti-patterns, which are bad programming practices. While anti-patterns may not seem that useful at first glance, they can help you see whats is a bad practice so you never implement it in the first place. A common anti-pattern in object-oriented design is a god class, which is a class that can do everything. This is not wanted since object-oriented design aims to separate code into smaller parts.

In summary, design patterns help us solve many of the common problems that we face every day as developers. If we apply these to our projects, it will be more efficient and easier to work with. Also, we shouldn’t forget anti-patterns, they can help us see bad practices so we don’t commit them in the first place.

Leave a comment

Design a site like this with WordPress.com
Get started