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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex