Design Patterns – An introduction

Design patterns are a quintessential topic that all entry level software developers are intimidated by. This post is my attempt at explaining the concepts in a simple way. I will avoid going into detailed examples since there are a lot of great linksvideos and books that do that well. I’d like to explain the core ideas behind design patterns in a concise way. This post will provide a high level summary about the types of design patterns and when they are used.

 

What?

A design pattern is a general, reusable solution to a commonly occurring problem software design.
There are three main categories design patterns can be divided into:
  1. Creational
  2. Structural
  3. Behavioral

Why?

They speed up development by reusing existing paradigms (tested, proven to be efficient).
int main(){

}

How?

Drawing a picture of a few objects takes time. But if we use stencils of objects to compose the drawing, it can be done much faster.
A design pattern is like a template (stencil) to solve problems in software development.

 

Creational patterns

are used for creating instances of classes and objects. They have two main ideas
  1. Encapsulate the information about which concrete class to use at creation
  2. Hide the nitty gritty details of creation and composition of the concrete classes and objects
Object creational patterns delegate object creation to another object.
Class creational patterns use the principle of inheritance to delegate object creation to subclasses. Some patterns that fall in the category of creational patterns are:
  • Factory pattern allows a class to delegate instantiation to its subclasses
  • Abstract factory pattern provides an interface to create a family of objects without specifying the concrete classes
  • Builder pattern encapsulates object construction from object representation
  • Prototype pattern clones from a fully initialized instance
  • Singleton pattern ensures that a class has only one instance during program execution and provide a global point of access to that instance

 

Structural patterns

simplify composition of entities (their relationships and functionality). Some common patterns in this category are:
  • Adapter pattern matches interface of a class to what the client expects
  • Bridge pattern decouples the interface from implementation to provide ‘loose coupling’
  • Composite pattern is a tree structure of objects with the same interface. Some objects are simple, while others are complex but they all have a uniform interface
  • Decorator pattern provides a way to extend functionality of a class during runtime without changing the interface
  • Facade pattern provides a higher level simplified interface to a set of interfaces in a subsystem
  • Flyweight pattern enables sharing by allowing a lot of objects share common properties
  • Proxy pattern provides a placeholder for another object to control access

 

Behavioral patterns

provide flexibility in communication across objects. Some common patterns in this category are:

  • Chain of responsibility passes the request along a chain of receivers until an object handles it
  • Command pattern encapsulates a request into an object
  • Iterator pattern provides a way to access the elements of a collection of objects without exposing the underlying details
  • Mediator pattern defines how different objects interact without referring to each other explicitly
  • Null object pattern provides a default object to avoid null references
  • Observer pattern defines a one to many dependency across objects so that a state change in the one object causes a notification to be sent to all it’s dependents
  • State pattern allows an object to change behavior when there is an internal state change
  • Strategy pattern defines a family of algorithms that are encapsulated to be interchangeable. Thus the algorithm can vary independently of the clients that use them
  • Template pattern lets subclasses redefine (override) some (specific) steps of an algorithm without affecting the high level structure of that algorithm
  • Visitor pattern allows a new operation to be defined without changing the class definition of the entities on which it operates
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s