Skip to main content

Design Patterns used in the jQuery library



jQuery is highly focused on the DOM and provides a nice abstraction around it. In doing so, it makes use of various well known design patterns which just hit me yesterday. One obvious example would be the Decorator pattern. The jQuery object provides new and additional functionality around a regular DOM object.





For example, the DOM has a native insertBefore method but there is no corresponding insertAfter method. There are various implementations available to fill this gap, and jQuery is one such library that provides this functionality:







$(selector).after(..)

$(selector).insertAfter(..)







There are many other examples of the Decorator pattern being heavily used in jQuery.





What other examples, big or small, of design patterns have you noticed that are part of the library itself? Also, please provide an example of the usage of the pattern.





Making this a community wiki as I believe that various things people love about jQuery can be traced back to well known design patterns, just that they are not commonly referred to by the pattern's name. There is no one answer to this question, but cataloging these patterns will provide a useful insight into the library itself.



Source: Tips4all

Comments

  1. Lazy Initialization:

    $(document).ready(function(){
    $('div.app').myPlugin();
    });


    Adapter or wrapper

    $('div').css({
    opacity: .1 // opacity in modern browsers, filter in IE.
    });


    Facade

    // higher level interfaces (facades) for $.ajax();
    $.getJSON();
    $.get();
    $.getScript();
    $.post();


    Observer

    // jQuery utilizes it's own event system implementation on top of DOM events.
    $('div').click(function(){})
    $('div').trigger('click', function(){})


    Iterator

    $.each(function(){});
    $('div').each(function(){});


    Strategy

    $('div').toggle(function(){}, function(){});


    Proxy

    $.proxy(function(){}, obj); // =oP


    Builder

    $('<div class="hello">world</div>');


    Prototype

    // this feels like cheating...
    $.fn.plugin = function(){}
    $('div').plugin();


    Flyweight

    // CONFIG is shared
    $.fn.plugin = function(CONFIG){
    CONFIG = $.extend({
    content: 'Hello world!'
    }, CONFIG);
    this.html(CONFIG.content);
    }

    ReplyDelete
  2. The Composite pattern is also very commonly used in jQuery. Having worked with other libraries, I can see how this pattern is not so obvious as it looks at first sight. The pattern basically says that,


    a group of objects are to be treated in the same way as a single instance of an object.


    For example, when dealing with a single DOM element or a group of DOM elements, both can be treated in a uniform manner.

    $('#someDiv').addClass('green'); // a single DOM element

    $('div').addClass('green'); // a collection of DOM elements

    ReplyDelete
  3. How about the Singleton/Module pattern, as discussed in this article about YUI: http://yuiblog.com/blog/2007/06/12/module-pattern/

    I believe jQuery uses this pattern in its core, as well as encouraging plug-in developers to use the pattern. Using this pattern is a handy and efficient way of keeping the global namespace clear of clutter, which is further useful by assisting developers in writing clean, encapsulated code.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.