Design Patterns Introduction

Beginner

2019-03-04

What is Design Patterns ?

A design pattern is a general solution that was found to be commonly occurring in software problems.

It's a reusable blueprint that can be customized to meet the problem in hand.

Design patterns in OOP world are used to describe best practices adapted by experienced object oriented software developers.

What are design patterns categories ?

As the famous GoF book categorized the commonly used design patterns to three categories according to their usage :

  • Creational
    • Creational Patterns are ones that create objects for you, rather than having you instantiate objects directly. This gives your program more flexibility in deciding which objects need to be created for a given case.
  • Structural
    • These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
  • Behavioral
    • Most of these design patterns are specifically concerned with communication between objects.

When to use design patterns ?

Design patterns are supposed to provide a structure in which problems can be solved. When solving a real problem, you have to consider many tiny variations of a solution to that problem to see whether any fits a design pattern. In particular, you will probably need to generalise your problem, or its solution, in order to make a design pattern fit.

The answer is, it's an art. Knowing the design patterns is certainly an important step. One way to get used to this sort of thing is to study applications of design patterns, not just the patterns. Seeing many different applications of one pattern can help you over time to get better at mapping a task onto a pattern.

Throughout this series, we will study various design patterns with applications and over time you will get a better understanding for how to effectively use design patterns into your system.

Which design pattern is best ?

As with all the software engineering solutions out there, there is no silver bullet or one-fit-all solution, you have to study the problem, it's causes and consequences inside your application and the system. Then try to generalize the problem to find a common pattern, then you will be able to come up with a pattern that will fit the problem in hand.

What is anti-pattern ?

An anti pattern is a bad usage of a design pattern.

In other words, when you use a design pattern to solve a problem , then you use this pattern to solve all your problems causing you to add more and more code to your pattern file. Then this patterns becomes harder to maintain and extend functionality.

It's now considered a design pattern.

Example for when design pattern becomes an anti-pattern ?

Consider the Singleton pattern ( if you haven't heard about it don't worry we will talk about it later ) .

The singleton pattern states that when you have a common block of code that is shared among various classes, and normally we don't need to instantiate and destroy this object every time we need it, Then it's better to be served inside a singleton pattern.

The problem comes when a developer extend this pattern when he needs to add a new feature, with every new feature he keeps adding an adding to the patterns until it becomes a God Class which contains every thing.

At this point this pattern becomes an anti-pattern and it's now harder to maintain and extend.

Summary

In this series we are going to study common design patterns in usage today, our main reference is Gof Book.