Monday, October 17, 2016

Design Pattern Categorization

Design Pattern Categorization:

It is recommended using this table as reference, but do remember that there are a number of additional patterns that are not mentioned here.

A brief note on classes

There will be patterns in this table that reference the concept of "classes". Javascript is a class-less language, however classes can be simulated using functions.  The most common approach to achieving this is by defining a javascript function where we then create an object using the 'new' keyword. 'this' can be used to help new properties and methods for the object as follows:
  function Car( model ) { 
           this.model = model;   
                           this.color = "silver";
                               this.year = "1988";                               this.getInfo = function(){   
                                                 return this.model + " " + this.year;
 
                              };
                       }

We can then instantiate the object using the Car constructor we defined above like this:
  var my Car = new Car("ford");
  myCar.year = "2016";
          console.log( myCar.getInfo() );

Creational                    Based on the concepts of creating an object.

Class
Factory Method
This makes an instance of several derived classed based on interfaced data or events.
Object
Abstract Factory
Creates an instance of several families of classes without detailing concrete classes.
Builder
Separates object construction from its representation, always creates the same type of object.
Prototype
A fully initialized instance used for copying or cloning.
Singleton
A class with only a single instance with global access points 
Structural           Based on the idea of building blocks of objects.

Class
Adapter
Matching interfaces of different classes therfore classes can work together despite incompitable interfaces.
Object
Adapter
Matches interfaces of different classes therfore classes can work together despite incompitable interfaces.
Bridge
Separates an objects interface from its implementation so the two can vary independently.
Composite
A structure of simple and composite objects which makes the total object more than just the sum of its parts.
Decorator
Dynamically add alternate processing to objects.
Facade
A single class that hides the complexity of an entire subsystem.
Flyweight
A fine-grained instance used for efficient sharing of information that is contained elsewhere.
Proxy
A place holder object presenting the true object.
Behavioral                             Based on the way objects play and work together.

Class
Interpreter
A way to include language elements in an application to match the grammer of the intended language. 
Template Method
Creates the shell of an algorithm in a method, then defer the exact steps to a subclass. 
Object
Chain of responsibility
A way of passing a request between a chain of objects to find the object that can hable the request.
Command
Encapsulate a command request as an object to enable, logging and /or queing of request, and provides error-handling for unhandled requests.
Iterator
Sequentially access the elements of a collection without knowing the inner workings of the collection.
Mediator
Defines simplified communication between classes to prevent a group of classes from referring explicilty to each other.
Memento
Capture an object's internal state to be able to restore it later.
Observer
A way of notifying change to a number of classes to ensure consistency between the classes.
State
Alter an object's behavior when its state changes.
Strategy
Encapsulates an algorithm inside a class separating the selection from the implementation.
Visitor
Adds a new operation to a class without changing the class.

Sunday, October 16, 2016

Javascript Design Patterns

Categories of Design Pattern:

A glossary from the well known design book, Domain-Driven Terms, states that

"A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful a reusable object-oriented design. The design pattern identifies the participating classes and their instances, their roles and collaborations, and the distribution of responsibilities.


Each design pattern focuses on a particular object-oriented design problem or issue. It describes when it applies, whether or not it can be applied in view of other design constraints, and the consequences and trade offs of its use. Since we must eventually implement our designs, a design pattern also provides sample, code to illustrate an implementation.


Although design patterns describe object-oriented designs, they are based on practical solutions that have been implemented in mainstream object-oriented programming languages.."


Design patterns can be broken down into a number of different categories. In this section we'll review three of these categories and briefly mention a few examples of the patterns that fall into these categories


Creational Design Patterns

Creational design patterns focus on handling object creation mechanisms where objects are created in a manner suitable for the situation we're working in. The basic approach to object creation might otherwise lead to added complexity in a project whilst these patterns aim to solve this problems by controlling the creation process.

Some of the patterns that fall under this category are: Constructor, Factory, Abstract, Prototype, Singleton and Builder.



Structural Design Patterns

Structural Design Patterns are concerned with object composition and typically identify simple ways to realize relationships between different objects. They help ensure that when one part of system changes, the entire structure of the system doesn't need to do the same. They also assist in recasting parts of the system which don't fit a particular purpose into those that do.

Patterns that fall under this category include: Decorator, Facade, Flyweight, Adapter and Proxy

Behavioral Design Patterns

Behavioral patterns focus on improving or streamlining the communication between disparate objects in a system.

Some behavioral patterns includes: Iterator, Mediator, Observer and Visitor.