OOP (object-oriented programming) gurus once concluded that it’s pointless to reinvent the wheel every time you encounter a problem in application design. They knew they should not always tackle problems from the ground up. Instead, they gathered and categorized battle-tested approaches in structuring code that were reoccurring in many projects. This is why in many systems, applications and frameworks we see repetitive patterns and class diagrams.
Reusing known patterns is not specific to software development. Take a look around you. In literature, themes, motifs and symbols from the bible or other ancient sources are still present in modern writings. This applies also to other aspects of life.
Patterns that worked before are likely to work now. This, in theory, is a promise that design patterns give us.
Before we introduce any specific design pattern, let’s first discuss what are they made of and how they are categorized.
According to GOF, a design pattern is made of four elements :
- Name - one or two words that describe the pattern
- Problem description - describes when to use the given pattern
- Solution - abstract description of classes, objects and their relations in the pattern
- Consequences - pros and cons of using the pattern
Each pattern belongs to one of three categories :
Creational - patterns responsible for object creation.
Structural - patterns involved in the way of composing classes and objects.
Behavioural - patterns that describe the way objects and classes work, share tasks and cooperate.
Besides this, there is one more important thing that separates design patterns:
Class-scope - when pattern is categorized as class-scope, it means that its goals are achieved at the compile time using inheritance
Object-scope - when pattern is categorized as object-scope its goals are achieved through composition during the runtime
A design pattern is a battle-tested, proven and general solution to a commonly occurring problem. It describes how to solve the given problem and can be used in many different situations.