Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:18:11+00:00 2026-06-13T18:18:11+00:00

I am reading the pro javascript design patterns book and finding little difficulty in

  • 0

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 !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:18:12+00:00Added an answer on June 13, 2026 at 6:18 pm

    I have seen the pattern implemented in different ways, but the idea is simple:

    1. You have some class – your interface – that simply specifies the names of some functions. (You may want to have a class called Interface that your actual interfaces instantiate, just so your interfaces are of type Interface)
    2. You then have some other class that implements such interface. This means that this second class must have at least all of the functions specified by the interface.
    3. Finally, you have some other function somewhere else, that expects to receive an object that implements the interface. In the sample code you mention, this function is addForm, which expects an object that implements the ‘Composite’ and ‘FormItem’ interfaces.
    4. This function then loops through all the methods of the interface(s) it expects, and checks that the object you passed in to it also have those methods. If a method from one of the interfaces is not found in the object passed into the function, it determines that object doesn’t implement the interface, and throws an exception.

    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

    var Interface = function(name, methods) {
        this.name = name;
        this.methods = [];
    
        if (methods.constructor == Array)
            this.methods = methods;
        else if (methods.constructor == String)
            this.methods[0] = methods;
        else
            throw new Error("Interface must define methods as a String or an Array of Strings");
    };
    
    var InterfaceHelper  = {
        ensureImplements : function(obj, interfaces) {
           // If interfaces is not an array, assume it's a function pointer
           var toImplement = interfaces.constructor == Array ? interfaces : [interfaces];
           var interface;
    
           // For every interface that obj must implement:
           for (var i = 0, len = toImplement.length; i < len; i++) {
              interface = toImplement[i];
    
              // Make sure it indeed is an interface
              if (interface.constructor != Interface)
                 throw new Error("Object trying to implement a non-interface. "
                 + interface.name + " is not an Interface.");
    
              // Make sure obj has all of the methods described in the interface
              for (var j = 0, interfaceLen = interface.methods.length; j < interfaceLen; j++)
                 if (!obj[interface.methods[j]])
                    throw new Error("Interface method not implemented. " 
                    + interface.name + " defines method " + interface.methods[j]);
           }
    
           return true;
        }
    };
    
    var Drawable = new Interface("Drawable", ["onDraw"]);
    
    var Surface = function() {
       this.implements = ["Drawable"];
    
       this.onDraw = function() {
          console.log("Surface Drawing");
       };
    };
    

    Usage

    var myDrawableSurface = new Surface();
    
    // Returns true
    InterfaceHelper.ensureImplements(myDrawableSurface, Drawable);
    
    // Returns false (Error thrown)
    InterfaceHelper.ensureImplements(myDrawableSurface, Array);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been reading Diaz's book Pro JavaScript Design Patterns. Great book. I myself am
I am reading the Pro Spring 2.5 book and in chapter 4 they talk
I was reading chapter 2 of Apres Javascript Pro techniques and in particular the
I am reading Pro Spring 2.5 and I have a question to chapter 2
I'm currently reading the book Pro Asp.Net MVC Framework. In the book, the author
I was reading Steven Sanderson's book Pro ASP.NET MVC Framework and he suggests using
Reading the Scala by Example book and there is this example when Martin explains
I'm reading Pro JPA 2. The book talks begins by talking about ORM in
I have finally finished reading Pro ASP.NET MVC 2 Framework Chapter 3: Pre-requisites but
In the book I'm reading Pro Linq in C# 2010 (APress, Freeman and Ratz)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.