design-patterns-api

0.4.2 • Public • Published

design-patterns-api

Catalog of Design Patterns as Interface classes (by means of mixin-interface). This catalog includes the 23 original ("Gang of Four") patterns, as well as further patterns (e.g. Null Object).

ATM this framework provides 20 out of the 23 'Original Patterns' described by the Gang of Four in their 1995's book (Elements of Reusable Object-Oriented Software). There are further design patterns described later (e.g. Patterns of Enterprise Architecture Application written by Martin Fowler).

Your feedback (via GitHub) is welcome especially to report bugs or design issues, request for features or enhancements and contribute to the 'religious' aspects of this project, like which patterns are in fact Antipatterns or if it depends on the context (e.g. Singleton, Service Locator...).

Note about Design Issues

  • Craft a proposal for each pattern: there are many references about Design Patterns. My proposals should be not be taken as reference but more as an ongoing work (for which your feedback is welcome). Thus I advise you to study by yourself the references (I have gathered some of them in the References paragraph) to check it they fits your learning curve and your own design issues.
  • Resolve name conflicts across patterns: in the references, it happens that participant names (and operation names as well) are shared across patterns. In my proposals, I resolved these name conflicts either by mining in the references or by adding parent interface classes (e.g. IElement, _IAction, ICoreComponent...).

Changelog for Release 0.4.2

  • Package extraction: original ("Gang of Four") patterns extracted to design-patterns-core-api in order to leave room for further patterns (e.g. Multiton, Service Location, Intercepting Filter...)
  • New pattern released: Proxy
  • Design Issue: name 'Subject' for a participant is used both in 'Observer' and Proxy' patterns.
  • Design Fix: rename 'Subject' to 'Observable' thus releasing 'Subject' name for 'Proxy' pattern. 'ISubject' also becomes superclass of 'IAdapter'
  • Minor documentation fix: (link to documentation link in 'Singleton' pattern)

Roadmap

  • Documentation upgrades: UML class diagram and implementation sample for each pattern

Preliminary notes

Naming convention: for any given interface class, its name is written with PascalCase convention (e.g. IAbstractFactory) and its source code uses snake_case convention (e.g. i_abstract_factory.js)

Why ...args ? Thanks to the rest parameter feature of javascript es6 (...args), most interface services accept optional and variable number of arguments.

What is the purpose of xxx_id arguments ? This is a design choice motivated by 2 design intents. The first is when the service call is propagated (e.g. 'request_id' argument is propagated by Adapter when IAdapter.request() calls IAdaptee.specificRequest()). The second is to avoid counterproductive class proliferation by using xxx_id argument as a way to make the call more specific (e.g. 'request_id' argument when calling IHandler.handleRequest() within the Chain Of Responsability pattern)

Creational patterns

Abstract Factory

Interface classes: IAbstractFactory and IProduct.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Abstract Factory.

Factory Method

Interface classes: I_Creator and IProduct.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Factory Method.

Builder

Interface classes: IBuilder and IProduct.
Detailed description: BlackWasp - Builder design pattern.

Singleton

Interface class: MxI.$ISingleton.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Singleton.

Behavioral patterns

Observer

Interface classes: IObserver and IObservable.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Observer.

Iterator

Interface classes: IIterator and ICollection.
Detailed description: Tutorials Point - Design Patterns: Iterator pattern.

State

Interface classes: IState and IStateContext.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - State.

Chain of Responsability

Interface classes: IHandler and IRequest.
Detailed description: OOODesign - Chain of Responsability.

Visitor

Interface classes: IVisitor and IElement.
Detailed description: Tutorials Point - Design Patterns: Visitor pattern.

Memento

Interface classes: IMemento, IOriginator and ICareTaker.
Detailed description: Tutorials Point - Design Patterns: Memento pattern.

Strategy

Interface classes: IStrategy and IStrategyContext.
Detailed description: Tutorials Point - Design Patterns: Strategy pattern.

Command

Interface classes: ICommand, IInvoker and IReceiver.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Command.

Template Method

Interface class: ITemplateMethod.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Template Method.

Mediator

Interface classes: IMediator, IColleague and IRequest.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Mediator.

Structural patterns

Bridge

Interface class: IImplementor.
Refer to Enginyeria del Software I - Curs 2006-2007 - Bridge.

Adapter

Interface classes: IAdapter and IAdaptee.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Adapter.

Facade

Interface class: IFacade.
Detailed description: Tutorials Point - Design Patterns: Memento pattern.

Decorator

Interface classes: IDecorator and ICoreComponent.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Decorator.

Composite

Interface classes: IComponent, IComposite and ILeaf.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Composite.

Proxy (new)

Interface classes: ISubject and IProxy.
Detailed description: Enginyeria del Software I - Curs 2006-2007 - Proxy.

Beyond "Gang of Four" patterns

Null Object

Interface class: MxI.$INullObject](https://github.com/Echopraxium/mixin-interface-api/blob/master/README.md#null-object-feature).
Detailed description: Tutorials Point - Design Patterns: Null Object pattern.

Developer's Guide

How to implement a Design Pattern

A given Design Pattern is composed of one or more participants, this is very much like Role(s) in a play. Within design-patterns-api project, each participant is implemented as an interface classs. Thus, in order to implement a Design Pattern you must implement the interface class(es). Please refer to How to code an Implementation class in the documentation of mixin-interface-api package.

Code Sample: LoggerFactory

LoggerFactory shows how to delegate the instanciation of a Logger (a more flexible way to log traces than console.log) by implementing the Abstract Factory design pattern.

See source code of LoggerFactory here. It is demonstrated in Unit Test.

How to run the Unit Test

Step 1: Install Prerequisite Tools

Install NodeJS and Git

Step 2: Clone the 'design-patterns-api' repository locally

Open a command shell then enter the following commands:

git clone git://github.com/Echopraxium/design-patterns-api
cd design-patterns-api
npm update

Step 3: Run the Unit Test

Now enter the following command:

node test.js

You should get the following output:

============================================================
======== Unit Test for 'design-patterns-api' package =======
============================================================
1. Creational Patterns
----------
1.1. Abstract Factory
Demonstrate 'Abstract Factory' Design pattern by changing DefaultLogger:

==> Logger is now 'arrow_prefix_logger_0'
[17:21:07 PM] Logger is now 'timestamp_prefix_logger_0'
[0] Logger is now 'count_prefix_logger_0'
----------
1.2. Factory Method
----------
1.3. Builder
----------
1.1. Singleton
----------------------------------------
2. Behavioral Patterns
----------
2.1. Observer
----------
2.2. Iterator
----------
2.3. State
----------
2.4. Chain Of Responsability
----------
2.5. Visitor
----------
2.6. Memento
----------
2.7. Strategy
----------
2.8. Command
----------
2.9. Mediator
----------
2.10. Template Method
----------
2.11. Null Object
MxI.$Null:              MxI.NULL
MxI.$isNull(MxI.$Null): true
null_node:              null_node_0
MxI.$isNull(null_node): true
Child Count:            0
----------------------------------------
3. Structural Patterns
----------
3.1. Bridge
----------
3.2. Adapter
----------
3.3. Facade
----------
3.4. Decorator
----------
3.5. Composite
----------
3.6. Proxy
===================== End of Unit Test =====================

References

Package Sidebar

Install

npm i design-patterns-api@0.4.2

Version

0.4.2

License

MIT

Last publish

Collaborators

  • echopraxium