My Book reviews /

Pro JavaScript Design Patterns: The Essentials of Object-Oriented JavaScript Programming

Title Pro JavaScript Design Patterns: The Essentials of Object-Oriented JavaScript Programming
Authors Dustin Diaz, Ross Harmes
Publisher Apress
Date 2007-12-17
Pages 269
ISBN 159059908X
Rating

I have read very useful book on design patters: Ross Harmes; Dustin Diaz Pro JavaScriptтДв Design Patterns. During daily life programmers use pattern very often without realizing this. But taking a systematic approach toward design patterns definitely helps organize your knowledge.

The book starts with an overview of JavaScript. Even though it is an object-oriented language with many useful features it is quite different from a traditional object-oriented language such as C++. For example there are no built-in mechanisms of inheritance or interfacing, thus for the sake of convenience one needs to emulate those. The book describes several types of Javascript inheritance, in particular the traditional and prototypical inheritance. The former type is most similar to traditional C++ inheritance. However, the programmer has to invoke the constructor of the superclass manually in the derived class. In the latter type of inheritance the programmers define the prototype member variable of a class which is a structure describing the functions and variables the class has. When the class is instantitated the prototype of the superclass is assigned to its prototype, thus the derived class obtains all the features of the superclass.

There is no built-in mechanism of private variables in JavaScript. However, it is possible to emulate it using closures. The introduction is concluded with a brief overview of chaining, a mechanism that allows to modify properties of many DOM elements simultaneously. This notaion is used in popular libraries, for example in jQuery.

After this brief introduction the book explains many design patters in a unified fashion. Each subsequent chapter consists of a description of a pattern, one or many real life examples, and discussion of its benefits and drawbacks. The following patterns are described:

  • Singleton - this pattern is used when only one instance of object is necessary, for example in case of page-specific code. Another example is branching to support incompatible browsers. A real life example is creating XHR object.
  • Factory is used to implement various kinds of managers. Imagine a complex object that consists of a number of other objects. This pattern is useful if the type of each component is known only at run-time, it makes it easy to replace the parts.
  • Bridge - decouples abstraction from its implementation so that they may vary independently.
  • Composite - implements a tree-like data structure.
  • Facade. This is a convenience method. One usage example is a class that accounts for differences among various browsers.
  • Adapter - wrap an existing interface into a new one.
  • Decorator - it is possible to use it as class decorator to extend exisitng functionality as well as function decorator, for example to implement a profiler.
  • Flyweight allows one to optimize the memory consumption of a class. The idea is to separate the class into intrinsic and extrinsic states. An example is implementation of a tooltip.
  • Proxy - two types of proxies exist: remote proxy facilitates access to a remote object, whereas virtual proxy allows to delay the instantiation of a complex object. This gives an impression of big speedup.
  • Observer - publish/subscribe architecture. Example implementation: animation.
  • Command implements an action which is separated from the object that invokes it. It is possible to use the action with a button or with a menu item.
  • Chain of responsibility тАУ allows to implement requests that are passed down a chain of hierachical objects.