Programming Design Patterns

Java Java!
Admin
Site Admin
Posts: 951
Joined: Thu Aug 23, 2007 10:35 am

Programming Design Patterns

Postby Admin » Fri Jun 13, 2008 5:03 pm

Module

Used to encapsulate variables and methods, to avoid cluttering the global (or any other) namespace. Encapsulation facilitates code reuse, and enables minifiers/compressors to save you more space than declaring everything globally. The more code you write, the more important it is to avoid using the global namespace.

Code: Select all

// basic module pattern
var moduleName = function() {
    // private variables and methods go here


    return {
        // public variables and methods go here
    }
};


// example:
var myModule = function() {
    var somePrivateVariable = "This is not available outside the module.";
    var anotherPrivateVar = 1234.56;

    function myMethod() = {
        // this is also only visible by code inside the module
    }

    function anotherMethod(str) = {
        // currently private, but will be made public in the "return" block
        alert('You passed this argument: ' + str);
    }


    // begin public space
    return {
        pubName: anotherMethod,
        getVariable: function() {
            // other modules can now get (but not set) this value
            return anotherPrivateVar;
        }
    }
};

// alerts "1234.56"
alert( myModule.getVariable() );

// alerts "You passed this argument: blah blah blah"
myModule.pubName("blah blah blah");

// this will throw an error, you can't directly access this var
alert( myModule.anotherPrivateVar );

Admin
Site Admin
Posts: 951
Joined: Thu Aug 23, 2007 10:35 am

singleton pattern

Postby Admin » Fri Jun 13, 2008 5:23 pm

A Singleton is a module that can have one and only one instance. In javascript, this can be implemented by inline execution. Putting parentheses immediately after a function definition executes that function. By combining this feature with the Module pattern, we create a Singleton.

Code: Select all

var mySingleton = function() {
    // private variables and methods
    var counter = 0;

    var addOne = function() {
        counter++;
        return counter;
    };

    // public variables and methods
    return {
        addOne: addOne, // public and private names can be the same...
        count: addOne // ... or different, it doesn't matter
    }
}(); // this is called inline execution

alert( mySingleton.addOne() ); // alerts "1"
alert( mySingleton.addOne() ); // alerts "2"
alert( mySingleton.count() ); // alerts "3"



Singletons are useful when there are variables or methods that you only need to create once, but use everywhere.

EnergyDrink
Posts: 4
Joined: Thu Aug 07, 2008 6:38 pm

Re: Programming Design Patterns

Postby EnergyDrink » Thu Jul 09, 2009 9:00 pm

Small mistake above, since the modules don't have inline execution, you need to instantiate them:

Code: Select all

    var myModule = function() {
        ...
    };
    var myInstance = new myModule();
   
    // alerts "1234.56"
    alert( myInstance.getVariable() );


Return to “Javascript”

Who is online

Users browsing this forum: No registered users and 1 guest