Design Patterns Tutorial

Design Patterns are commonly used solutions for software problems. Each Design Pattern is created to address a specific problem.
When you encounter a design problem, it is well worth to learn and understand design patterns so that you can see if any of them make sense to implement.

In this tutorial, we will go through common design patterns and see their implementation in Java and C# languages.
Design Patterns are split into three primary categories, Creational, Structural and Behavorial.

Creational Patterns

Pattern Name
Purpose
If we want only a single instance of a class to be present in memory at any given time, we can use a Singleton pattern. Example uses include: Database drivers, Logging utilities etc.
If we want a seperate object creation logic away from the client, we can use a Factory pattern. Example uses include: Inversion of control etc.
If we want to reduce the number of Constructor arguments on creation of a complex object, we can use a Builder pattern. Example use cases include: Building XML or HTML objects.
If we want object instances to be created via pre-configured object and avoid boilerplate code and instantiation expense, we can use a Prototype pattern.

Structural Patterns

Pattern Name
Purpose
If we want to adapt an interface to another client interface, which normally can't interface due to incompatible interface, we can use an Adapter pattern. Example use case include Database adapters.
If we want to Compose our objects into a tree structure and represent individuals and composites identically, we can use Composite Pattern.
If we want to add functionality to existing objects, but avoid altering the class, we can go for a Decorator pattern.
If we want to seperate interface and implemenation into seperate hierarchies, we can use a Bridge pattern.
If we want to provide a simplified interface for a complex body of code, we can use Facade pattern.

Behavorial Patterns

Pattern Name
Purpose
If we want to create a chain of objects to serve a request, and pass control to the next object in the chain if it cannot be processed, we can use Chain of Responsibility pattern.
If we want to loop through an Aggregate object, but without exposing underlying data structures, we can use Iterator pattern. Example use case include collections API in languages.
If we want inter object communication to happen not directly, but via an intermediary, then we can use Mediator pattern. Example use case is Group chat rooms.
If we want to observe state changes to a subject, we can use Observer pattern. Example use case is to observe GUI Events.

Next: Singleton
>
Share this page:

Report Error in Content