background preloader

Design Patterns

Facebook Twitter

A JavaScript implementation of the Decorator pattern. For background information on the decorator pattern check the Wikipedia article or for PHP implementations, here and here. Motivation and example use Let's take an example - you've created a class and released to the world. You want people to be able to easily build upon it and also release to the world. Some other folks can take your base class and selectively choose from the extensions only those that make sense for them.

The example I choose is a class that does something on some text, beautifies it. Makes sure that there's always a space after the dots in a sentence and the dashes are also surrounded by spaces and so on. Var t = new TextBeautifier.Core('Some text.And some more.Yeah, baby,yeah-Yahoo'); t = t.getDecorator('Punctuation'); t = t.getDecorator('FixDashes'); t = t.getDecorator('Yodel'); t.get(); t.set('bla-bla-blah! The idea is that no matter how many or how little decorators you add, the basic functionality (setting text input, getting beautified output) remains the same.

JavaScript Programming Patterns. JavaScript is meant to be used to add behaviour to a website, might it be for form validation or for more complex operations like drag & drop functionality or performing asynchronous requests to the webserver (aka Ajax). During the past few years, JavaScript libraries became increasingly popular. One of the reasons is definitely that websites are getting more and more complex and reinventing the wheel each time is not acceptable if you are working on a tight schedule. But letting aside libraries and focusing on the “bare” syntax of JavaScript, it is very valuable to know what kind of options you have in terms of programming patterns when writing JavaScript.

In this article I am trying to present some of the techniques out there that I have discovered. The patterns I would like to mention are the following: The way I decided to present and compare these different patterns is by solving the same given task with every pattern. The Old-School Way Please proceed to the working example. Why I don't love JavaScript's Module Pattern. The Module Pattern is the use of closures to create, in essence, private functions that are only accessible by other functions created within that closure. Functions can be attached to an object and returned outside of that function call. This establishes a public API and only the functions defined within that public API will have access to those hidden functions.

The YUI blog documents this well but here is a simple example of the Module Pattern: In this example, the ManageChildren object will have two methods: addChild and removeChild. From outside the wrapper function, you cannot access the children array that is defined within. After having worked with and used this pattern for some time, I now avoid it. Debugging When it comes to troubleshooting a particular troublesome page, I like to crank open Firebug's console and play around willy-nilly.

Going back to that example I just showed, what if you needed to determine the length of the children array? Make extending easier See what I did? JavaScript Getters and Setters. It is with much happiness that I think I can finally say, without seeming like a fool, that: “JavaScript Getters and Setters are now prevalent enough to become of actual interest to JavaScript developers.” Wow, I’ve been waiting a long time to be able to say that. I want to start by giving a whirlwind tour of Getters and Setters and why they’re useful.

Followed by a look into what platforms now support Getters and Setters as to make them relevant. Getters and Setters Getters and Setters allow you to build useful shortcuts for accessing and mutating data within an object. Generally, this can be seen as an alternative to having two functions with an object that are used to get and set a value, like so: The obvious advantage to writing JavaScript in this manner is that you can use it obscure values that you don’t want the user to directly access. Function Field(val){ var value = val; this.getValue = function(){ return value; }; this.setValue = function(val){ value = val; };} Platforms Browsers.

Books

Objectifying JavaScript. The web professional's online magazine of choice. In: Columns > Behind the Curtain By Jonathan Snook Published on September 18, 2006 When I first began programming with JavaScript, I created my variables and then encapsulated any functionality I needed to reuse into a function. As my tasks got more complicated and as I started learning object-oriented programming in other languages, I began to see the benefits of applying that to JavaScript. In JavaScript, a function is an object; creating a function creates an object. As scripts get larger, functions become more interrelated.

In a true object-oriented sense, an object represents a thing. The important idea here is simply that objects encapsulate related functionality. In JavaScript, we often perform a series of tasks on existing objects. We’ve established that objects are good and useful, but how do we go about creating them? Using the object literalusing the new keyword with a function Using the Object Literal Example: Creating Objects.

Object Literals

Adequately Good - JavaScript Module Pattern: In-Depth - by Ben Cherry. The module pattern is a common JavaScript coding pattern. It's generally well understood, but there are a number of advanced uses that have not gotten a lot of attention. In this article, I'll review the basics and cover some truly remarkable advanced topics, including one which I think is original. The Basics We'll start out with a simple overview of the module pattern, which has been well-known since Eric Miraglia (of YUI) first blogged about it three years ago.

If you're already familiar with the module pattern, feel free to skip ahead to "Advanced Patterns". Anonymous Closures This is the fundamental construct that makes it all possible, and really is the single . (function () { // ... all vars and functions are in this scope only // still maintains access to all globals }()); Notice the () around the anonymous function. Global Import JavaScript has a feature known as . Luckily, our anonymous function provides an easy alternative. Module Export Advanced Patterns Augmentation Sub-modules. Essential JavaScript Design Patterns For Beginners. I hope this book helps on your journey to improving your knowledge of design patterns and the usefulness of their application to JavaScript. Before we get started, I would like to thank Rebecca Murphey for inspiring me to write the original version of this online book and more importantly, open-source it.

I believe educational material should be freely available for anyone to use, access and improve where possible and hope that efforts such as this inspire other authors. I would also like to extend my thanks to the always brilliant Alex Sexton who was kind enough to be the technical reviewer for the first edition of this work. Finally, I would like to thank my wonderful wife Elle for putting up with my obsession for technical writing over the years.

My hope is that I haven't abused the English language too badly. At the beginning of this book I will be focusing on a discussion about the importance and history of design patterns in any programming language.