I am reading the "pro javascript design patterns" book and finding little difficulty in understanding the "Interface" pattern given in the book chapter 2 as there isn't a complete code example demonstrating the use of this pattern.
I am looking for some help understanding this pattern with some running code example may be on jsfiddle etc.
This pattern is explained in the book pages 14 - 22, main point I am not understanding is where and how "addForm" method is called. OR if somebody can complete the ResultFormatter example with some test data and object this will really be very helpful in understanding the pattern.
Code for the book "Pro Javascript Design Patterns" can be downloaded from http://jsdesignpatterns.com/ and this is Chapter 2.
Thanks for the help !!
Completed the book example and here is the working jsfiddle, -
I have seen the pattern implemented in different ways, but the idea is simple:
Some people may find this pattern unpractical because of the overhead involved, but given Javascript's lack of native support for interfaces, this is not too bad a solution. Some people may also find that using interfaces for small projects in Javascript is overkill.
Example
Usage
ES6 has added syntactical sugar to the language. Below is the ES6 implementation of the same example.
Try playing around with the code by removing the methods in the
Map
class. Hope it explains better to the people coming from oops background