Friday, April 8, 2016

Design Patterns Summary

Gang of Four patterns (basic patterns)

  • Factory: You want a single method that returns one of several possible objects that share a common super class. The object is chosen at runtime.
  • Singleton: It is used when you want to eliminate the option of instantiating more than one object. This one object is used by several other objects so its attribute values are the same among all of the clients.
  • Observer: A Publisher send updates that possible multiple Subscribers needs to take or be aware of them. The Observer is the Subscriber.
  • Strategy: The algoritm can change at runtime. An interface is implemented by different algoritms and at runtime the actual classes that need that algoritm can be set dinamically without forcing a class to implement an specific interface.
  • Facade: Create a simplified interface that performs many other actions behind the scenes.
  • Decorator: You need to add functionality at runtime and modify the object dinamically. You define an abstract class that will be your Decorator base class, and several concrete classes can dinamically add functionalities.
  • Template: Used to create a group of subclasses that have to execute a similar group of methods. You defined an abstract class with a final method, and inside this method you call several methods to be implemented by concrete classes. The template method just make references to the actual method implemented on the concrete classes.
  • Composite: Allows you to treat individual objects and compositions of objects uniformly. You can represent part-whole hierarchies. Remember you can have a class which inside made references to other Leaf, but also to other branches. It is like a tree, one object has references to other objects which in turns has other references.