Oct 22, 2018

Software design patterns levels and notes

Eight design patterns are some of the simplest and most commonly used of the GOF 23 patterns:
  1. Abstract factory
  2. Adapter
  3. Composite
  4. Decorator
  5. Factory method
  6. Observer
  7. Strategy
  8. Template method
Road map of GOF

Easy:
  1. Facade
  2. Singleton
  3. Mediator 
  4. Iterator 
  5. Strategy 
  6. Command
  7. Builder
  8. State
  9. Template Method
  10. Factory Method
  11. Memento
  12. Prototype
Intermediate:
  1. Proxy
  2. Decorator
  3. Adapter
  4. Bridge
  5. Observer
Advanced:
  1. Composite
  2. Interpreter 
  3. Chain Of Responsibility
  4. Abstract Factory
  5. Flyweight
  6. Visitor
Software design patterns are broadly classified into four categories based on their use:

  1. creational
  2. structural
  3. behavioral
  4. concurrency

When a design pattern is commonly used but is ineffective and/or counterproductive, the design pattern is known as an anti-pattern.

Class creational patterns use inheritance to vary the classes that are instantiated. Object creational patterns delegate instantiation to other objects.

Strategy vs Command vs State

Command is almost identical to Strategy. You call an "execute" method and the instance executes itself. Command is more dynamic because it is triggered by an external agent such as input from user. In contrast, a Strategy often represents a business rule.

State is similar to Strategy and Command. It is based on polymorphism and is centered around a single super-class/interface with any number of sub-classes. Unlike those patterns, it is not intended to perform a single action. Instead, it tells how any number of actions are performed while in a certain state. Don't worry too much about state transitions. State is more about what happens in state X. The concept of state transitions follows naturally.

Builder vs Strategy

Builder is just a special case of Strategy, applied specifically to the problem of creating an object. The actual construction depends on what its building, and that construction process is encapsulated in a Strategy object called the Builder.

Template Method vs Strategy

Both involves polymorphism. Template Method maybe an alternative to Strategy.

Template Method vs Factory Method

Factory Method is just a special case of Template Method. It applies only to Template Methods which involve the creation of an object. 

Memento vs Command

Memento is related to Command  with its involvement in Undo functionality. Some commands cause loss of information and you can't reverse the effect of a command just by knowing the final state. 

Prototype

Prototype is not used very much in practice. Actually, the structure diagram shows a class hierarchy, but the pattern is essentially about a single class being cloned.

Proxy vs Decorator

Familiar with web proxy? Proxy is a special case of Decorator. A Decorator could wrap classes to perform tasks other than those performed by Proxy. Proxy is usually used with regard to security and networking, and typically a class with a proxy cannot be accessed directly. In contrast, a Decorator is usually used to enhance an existing class's functionality. A client can freely choose whether to wrap a class with its Decorator(s) or not. As with the Strategy-Command relationship, Decorator shares the same mechanism as Proxy, varying only in the application area and style of use.

Adapter vs Bridge

Bridge is similar to Adapter. Adapter is usually used to avoid changing existing classes. Bridge is usually designed the Adapter to glue things together. Many Java frameworks are an example of Bridge, where a set of interfaces are listed and individual vendors can then provide their own implementations.