GitXplorerGitXplorer
r

JavaScript-Design-Patterns

public
170 stars
61 forks
1 issues

Commits

List of commits on branch master.
Unverified
457b924391d7cf4e2a7156d54160aee4b9d32883

Add factory examples

rrobdodson committed 12 years ago
Unverified
fc6a4ebc890541b55bf66954a465248a7ed3016a

added closure example to decorators

rrobdodson committed 12 years ago
Unverified
cac9359fcafe1b2c90bb4514922b7f7e6b62f93a

Add Singleton Example

rrobdodson committed 12 years ago
Unverified
91e99f442a77ea97ff366bf398119d1525cd2043

testing ssh

rrobdodson committed 12 years ago
Unverified
4d6294aadca59fc7ab2d2c873067eeda5a2c503a

Add source for decorator, iterator and observer

rrobdodson committed 12 years ago
Unverified
23f681f26b29c97fbd53ba5907c816932e9dcf54

Added README

rrobdodson committed 12 years ago

README

The README file for this repository.

JavaScript Design Patterns

Creational

  • Abstract Factory
  • Builder
  • Factory Method
  • Object Pool
  • Prototype
  • Singleton

Structural

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Private Class Data
  • Proxy

Behavioral

  • Chain of Responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Null Object
  • Observer
  • State
  • Strategy
  • Template Method
  • Visitor
  • Monad Pattern / Promises

It occurred to me that I’ve always wanted to go through the Gang of Four book and just write my own interpretation of each pattern. Since I’m currently working primarily in JavaScript I thought it might be an interesting challenge to convert their examples, often in strongly typed languages, to something as dynamic and loosey-goosey as JS.

I know there are a lot of people out there who aren’t too keen on design patterns but that’s not to say that they shouldn’t be used or studied. There’s a lot of code out there that starts with jQuery.click() or addEventListener or .on() and all of them are implementations of the Observer pattern. Finding this reusable approach is the main point of patterns and along with it comes a shared vocabulary that can be passed on to other developers. Rather than saying “Let’s defer the methods of our object that are subject to change to well encapsulated algorithms.” We can just say “A Strategy pattern might be nice here.”

Patterns should be used with caution as not everything fits so neatly into their paradigms. It’s often said that a beginner never met a pattern he didn’t like. In my experiences I’ve been burned by pattern overuse and at other times they have legitimately saved my ass. It’s also true that many patterns don’t really work or aren’t appropriate for particular languages. For instance, the GoF book was written primarily for languages which shared features of C++ and SmallTalk. I totally agree with this sentiment but I feel like along the way we’ll discover what does and doesn’t make sense in a dynamic language like JS and hopefully we can toss in some new patterns of our own. Already to the list I’ve added Promises which I use quite frequently and find to be a wonderful alternative to JavaScript’s oft seen pyramid of callbacks. Again, this is all about learning and experimenting. In my opinion a good understanding of design patterns is a threshold that needs to be crossed at some point in your career. I’m committed to doing this twice a week for the next several weeks so hopefully by the end of it we’ll have a useful resource that others can benefit from. Stay tuned!

http://robdodson.me/blog/2012/08/03/javascript-design-patterns/