As programmers we are problem solvers; that is what we do. The industry sure is older than the average programmer’s age. What this means is that a good number of problems that you face have already being solved in the past. So how do we benefit from the solutions of other programmers who faced similar problems and solved them? Design Patterns is the way to go
. In layman’s language, a design pattern is a general solution to a frequently occurring problem. Let’s us explain this with a practical example
. Imagine you are developing an application that connects to the database and you have a class for that. You create an instance of the class when interacting with the database. Suppose your application user opens multiple forms at the same time. Your application will open multiple connections to the server. Let’s assume your application supports multiple current users and you have 23 users online at the same time and each user has an average of 11 connections open. You will have 253 connections open
. This creates performance problems for your application and may crush your database server The above scenario is one of the frequently occurring problems, design patterns respond to the above problem by providing us with a singleton pattern
A singleton pattern prevents creating multiple instances of the same object by providing access to a shared instance
The class diagram below shows the design of the singleton pattern Our above application would open 23 connections only as opposed to 253
. Think of the performance gains one would get if they used the singleton pattern as opposed to traditional coding without design patterns? As if this isn’t sinful enough, design patterns’ side effects include the following;
5 Reasons why you should love design patterns
Just like any tool, design patterns should be used wisely
- Reusability – you do not need to reinvent the wheel
- Reduced development time – by using already existing designs, you will spend less time solving problems
- Tested and proven to work – a good number of other programmers have tested the designs and proved them to work
- Increased confidence in yourself and application – since you are using proven designs, you can confidently look into your boss/users’ face and say I have delivered what you requested and it will work the way you expect it to.
- Facilitate communication among developers – all that the other developer needs to know is the pattern used and he will know what the code does assuming he is familiar with design patterns.
. In the next article of the design pattern series, we will look at what design patterns are not, and factors to consider when deciding on a design pattern to use. The design pattern series will also have practical implementations of popular patterns. The examples will be implemented in C#, Java and PHP.