Categories
JavaScript

Javascript patterns: namespacing your objects.

Avoid clustering the global scope.

Start with uppercase (recommended):

var MYAPP = MYAPP || {};

To be able to construct objects with several levels of deepness, use the following utility function:

var UTILITIES = UTILITIES || {};
UTILITIES.namespace = function (ns_string) {
var parts = ns_string.split(‘.’),
parent = MYAPP,
i;

// strip redundant leading global
if (parts[0] === “MYAPP”) {
parts = parts.slice(1);
}

for (i = 0; i < parts.length; i += 1) { // create a property if it doesn't exist if (typeof parent[parts[i]] === "undefined") { parent[parts[i]] = {}; } parent = parent[parts[i]]; } return parent; };

Categories
PHP Programming

Design patterns: singletons

Singleton classes basically mean there is only one object class of a certain type running at all times. That could be used to store Global variables, for example.

The way to implement it on PHP:

class Preferences {
    private $props = array();
    private static $instance;

    private function __construct() { }  // By setting the class constructor to private, it can’t be instantiated from outside

    public static function getInstance() { // This static method will serve as instantiation, or to serve the only instance available
         if ( empty( self::$instance ) ) {
              self::$instance = new Preferences();
         }
         return self::$instance;
      }

      public function setProperty( $key, $val ) {
          $this->props[$key] = $val;
      }

      public function getProperty( $key ) {
          return $this->props[$key];
      }
}

Example of the driver class that is calling this:

$pref = Preferences::getInstance();
$pref->setProperty( “name”, “matt” );

unset( $pref ); // remove the reference

$pref2 = Preferences::getInstance();
print $pref2->getProperty( “name” ) .”n”; // demonstrate value is not lost

Categories
Programming

Design patterns: Composition and Inheritance (Strategy pattern)

Instead of creating child classes, you create a new tree of classes and move some of the functionality away from the main tree.

You connect the trees via object creation, meaning some of the classes in one tree contrain objects with the other classes type.

The advantage is that the trees are more specialized in what they do, so functionality in one tree is independent from the other tree, and it just complement it.

The disadvantages is that now you are creating more classes, so it may be difficult to maintain and keep track of it.

Categories
Programming

MVC architecture

It is a design pattern that helps separate business logic from input and presentation, making it easier to maintain each one of those components independenly from each other.

Model: it is the representation of data according to the application’s function or domain. It includes data access but it is not exclusively the retrieval or records.

View: renders the model so it can interact with users. It is the UI. It can have several manifestations for a given Model.

Controller: receives input and coordinates actions between the Model and the View. In the case of web servers translates HTTP requests into calls to backend and ultimately HTML pages.