As a PHP developer, mastering object-oriented programming can feel like a never-ending challenge. You start by familiarizing yourself with core object-oriented features like:
- Encapsulation
- Inheritance
- Polymorphism with Abstract classes and Interfaces
You start writing object-oriented code and playing with those features. This can leave you with some mixed feelings. You don’t feel that using those features are worth the trouble. You might just drop everything all together and go back to what you were doing before. That’d be a mistake.
You only took the first few steps
Your journey towards object-oriented supremacy is just starting! It’s one that’s longer than you expected. So what have you seen so far?
What you’ve seen is the foundation of object-oriented programming. While those features are fundamental, it can feel weird to use them just like that. There’s a difference between seeing an example on the site using them and using it by yourself. That’s why those basics don’t always help you feel like you can do any of it.
If you think about it, it makes sense. Think back on any craft you’ve learned in the past.You always started with the basics. Once you mastered them, you could move on to the more advanced techniques. That said, that knowledge didn’t always feel useful at first.
Where do you go from here?
With that in perspective, what is the next step you can take? Well, it’s time to start looking at common problems and their solutions.
We call these solutions “software design patterns“.
What are software design patterns?
Software design patterns are reusable solutions to programming problems. These are problems that you’ll encounter quite often. They’re also not language specific either. If you learn to use one in PHP, you’ll feel right at home using it in another language.
In most cases, you should see a software design pattern as a starting template for solving a problem. You need to adapt them to your unique context. That’s you’ll see each of them explained in detail in the future.
They also vary in size and complexity. Some of them only need a single class or method. Meanwhile, others need several different classes with specific relationships and interactions.
How they make you awesome
Let’s say for a second that you’re building a website. You add a navigation bar at the top. Did you stop and think if you should use something else? Of course not! When you need a way to navigate your site, that’s the default solution everyone uses.
After all, that’s not what you care about at the moment. You want to get that kick ass site out the door and into the hands of your clients or customers. You’re not interested in reinventing a tried and true web design pattern.
Well, the same is true with software design patterns. Once you’re familiar with a design pattern, you can just use it like you would with a navigation bar. This lets you focus on the important stuff. Writing great software that helps your clients or customers.
Types of software design patterns
We can divide these software design patterns into several broad categories. These categories come from the types of problems the patterns are trying to solve.
Creational patterns
This is the most straightforward category. As the name suggests, creational patterns help with the creation of objects. This is a common issue in modern software.
That’s because modern software can grow quite complex. This leads to classes that have a lot of requirements. Creating and instantiating them can become a complicated process. These patterns help with that problem.
Patterns like “Singleton” and “Factory” are some of the better known creational patterns. It’s not uncommon that you start using these patterns first. They are easy to understand and useful.
Structural patterns
Structural patterns are quite different from creational patterns. That’s because the creation of objects is the core focus of all creational patterns. They help create objects.
Meanwhile, each structural pattern solves a specific problem. These problems don’t share a common purpose. What links these patterns together then?
We group together because they all deal with relationships between objects. Their goal is to simplify these relationships. You can then combine those objects into larger data structures. Thus the name.
Behavioural patterns
Behavioural patterns are all about objects can communicate and interact with each other. This is an important problem in modern software. You need ways for different parts of an application to communicate with each other.
WordPress developers had that problem. They needed a way for plugins to safely interact with WordPress as well as other plugins. They built its Plugin API to solve that problem. The API itself is an implementation of the “Mediator” pattern.
This shows that you’re already familiar with some software design patterns. You’re just not aware of it.
Concurrency patterns
You won’t see concurrency patterns mentioned often. They’re used to solve problems related to multi-threaded programming. While you can do this type of programming in PHP, it’s not something that sees a lot of use in the community.
This means you’re unlikely to see concurrency patterns in practice. You’ll see them used more often in programming languages that rely on threads like Java. I mention them here for the sake of completeness.
The next step in your journey
You’ve learned about object-oriented programming basics. You might not feel 100% comfortable with them yet. That’s ok! You’ll get plenty of practice.
Software design patterns are the next chapter in your quest for object-oriented mastery. You’ll start solving real problems. You’ll put what you’ve seen so far to better use.