SlideShare une entreprise Scribd logo
1  sur  51
Télécharger pour lire hors ligne
JavaScript Patterns
Adding Tools to Your Toolbox
Derek Brown / @derekbrown
Question
Can you drive a nail into a board using a Phillips-head
screwdriver?
Answer
Yep. But it'll suck. Get a hammer, moron.
Why Patterns?
Great design patterns are reusable, modular expressions of
what's going on in your code. They allow you to
communicate to other developers simply by the way you
code, in addition to being easily maintainable themselves.
Put simply, patterns are the available tools in the
developer's toolbox.
The Patterns We'll Cover
There are hundreds of design patterns that can be
leveraged for use here in your code base. Ain't nobody got
time for that, so we'll cover only a few that are pretty
different from one another, but are prevalent in
applications, to get a glance at what's out there.
For each pattern, we'll look at the following:
Definition
Code Sample
Advantages & Disadvantages
Common Usage
Module / Revealing Module
You are already familiar with this pattern. Trust me. It's one
of the most common patterns on the web.
Singleton
There can be only one....instance of this object.
Facade
A facade is exactly what it sounds like: makeup on a bulldog.
You're covering over complex, ugly things with a simplified
interface for future, more scalable development.
Command
This pattern is useful for action-oriented objects.
Factory
The factory pattern lets you encapsulate multiple types of
objects within a categorical constructor.
Observer
This is a pattern that we've talked about previously, within
the context of Ember. But how can you implement a
publish/subscribe model yourself? And when should you?
Delegate
Delegates are a pattern that allow for event-like
communication between components.
Module / Revealing Module
This is one of the most fundamental design patterns in the
JavaScript universe. It's primary use is to include both
private and public variables within a single class-like object
while at the same time protecting the private
methods/properties from the application.
Modules accomplish this encapsulation by using closures to
protect the private pieces while allowing the developer to
determine which pieces of the object should be publicly
exposed to the rest of the application.
Module / Revealing Module
vrmMdl =(ucin( {
a youe
fnto )
vrmPoet ='au'
a yrpry
Vle;
rtr {
eun
gtrpry fnto ( {
ePoet: ucin )
rtr mPoet;
eun yrpry
}
,
strpry fnto (eVle {
ePoet: ucin nwau)
mPoet =nwau;
yrpry
eVle
}
}
;
}(;
))
mMdl.ePoet(;/ Rtrs'au'
youegtrpry) / eun Vle
mMdl.ePoet(NwVle)
youestrpry'e au';
mMdl.ePoet(;/ Rtrs'e Vle
youegtrpry) / eun Nw au'
Module / Revealing Module
My return object is going to get a bit messy....
Module / Revealing Module
vrmMdl =(ucin( {
a youe
fnto )
vrmPoet ='au'
a yrpry
Vle;
fnto gtrpry( {
ucin ePoet )
rtr mPoet;
eun yrpry
}
fnto strpry(eVle {
ucin ePoet nwau)
mPoet =nwau;
yrpry
eVle
}
rtr {
eun
gt gtrpry
e: ePoet,
st strpry
e: ePoet
}
;
}(;
))
mMdl.e(;/ Rtrs'au'
youegt) / eun Vle
mMdl.e(NwVle)
youest'e au';
mMdl.e(;/ Rtrs'e Vle
youegt) / eun Nw au'
Advantages: Module
Clean, readable, & consistent syntax.
Less clutter in the global namespace.
Allows developers to control scope of properties & methods.
Localization of functions & variables
Disadvantages: Module
Unit testing can be difficult if the methods aren't exposed.
Private methods are unaccessible.
Can't easily extend private methods.
Common Usage
Everything. <sarcasm>
But really.
Singleton
The singleton pattern is extremely useful if you only one
instance of the object to ever exist. Basically, what occurs
within the Singleton pattern is that you write your object,
and then as a part of that object, you have an additional
method. This function simply checks if an instance of the
object already exists. If it does, use that instance. If not, then
create a new one and store the reference.
Singleton
vrmSnltn=(ucin( {
a yigeo
fnto )
vrisac,
a ntne
mPoet ='au'
yrpry
Vle;
fnto gtrpry( {
ucin ePoet )
rtr mPoet;
eun yrpry
}
fnto strpry(eVle {
ucin ePoet nwau)
mPoet =nwau;
yrpry
eVle
}
fnto iiilz ( {
ucin ntaie )
rtr {
eun
gt gtrpry
e: ePoet,
st strpry
e: ePoet
}
;
}
rtr {
eun
gtntne fnto ( {
eIsac: ucin )
i (!ntne){
f
isac
isac =iiilz(;
ntne
ntaie)
Advantages: Singleton
Reduced memory usage
Single point of access
Delayed initialization prevents instantiation until required
Disadvantages: Singleton
Once an instance exists, it's difficult to "reset".
Harder to unit test.
Common Usage
Application Instances.
Facade
The facade pattern is often paired with other patterns to
add an extra layer of security while at the same time
providing a simpler interface to the underlying functionality.
Facade
vrmFcd =(ucin( {
a yaae
fnto )
vrmPoet ='au'
a yrpry
Vle;
fnto gtrpry( {
ucin ePoet )
rtr mPoet;
eun yrpry
}
;
fnto strpry(eVle {
ucin ePoet nwau)
mPoet =nwau;
yrpry
eVle
}
rtr {
eun
gt fnto ( {rtr gtrpry) }
e: ucin )
eun ePoet(; ,
st fnto (eVle {strprynwau) }
e: ucin nwau)
ePoet(eVle;
}
;
}(;
))
Advantages: Facade
Enhances security, as internal functions aren't exposed.
Easy to implement
Works well with other design patterns.
Easy to patch internals
Provides a simple public interface
Disadvantages: Facade
Is the cost of implementation really worth the extra layer of
abstraction?
Common Usage
One of the most prevalent uses of the Facade pattern ever:
jQuery
Command
The command pattern completely separates the
implementation and execution of methods. Usually, in order
to execute a method, you directly invoke the method itself.
The command pattern takes the name of the method to
execute as an argument into an "execute" or "run" method
on the command object, applying the rest of the arguments
list to the function being invoked.
In most programming, objects represent nouns. In the
command pattern, objects are verbs.
Command
vrmCmad={
a yomn

rqetaa fnto (d atiue {
eusDt: ucin i, trbt)
rtr 'aaatiue'+atiue+'hsbe rqetdfrojc '
eun Dt trbt
trbt
a en euse o bet
}
,

ceraa fnto (d atiue {
laDt: ucin i, trbt)
rtr 'h dt atiue'+atiue+'hsbe rstfrojc '
eun Te aa trbt
trbt
a en ee o bet
}
}
mCmadrn=fnto (omn){
yomn.u
ucin cmad
rtr mCmadcmadrqet(omn.d cmadatiue
eun yomn[omn.eus]cmadi, omn.trbt)
}
;

/
*
Atraiey ternfnto cudlo lk ti,wihwudps eta
lentvl, h u ucin ol ok ie hs hc ol as xr
mCmadrn=fnto (omn){
yomn.u
ucin cmad
rtr mCmadcmad.pl(mCmad [.lc.alagmns 1)
eun yomn[omn]apy yomn, ]siecl(ruet, );
}
;
o fragmnls cmad,yucng ee smlr
r o ruetes omns o a o vn ipe:
mCmadrn=fnto (omn){
yomn.u
ucin cmad
Advantages: Command
Decouples implementation from execution, which allows for
extensibility while minimizing code changes.
Stacking command objects allows you to cache them, store them in a
history, or otherwise manipulate them. Undo, anyone?
Disadvantages: Command
Counter-intuitive to most OOP practices.
Very limited use to 'verb-centric' applications.
Common Usage
Command Line Interfaces for Node.js
Factory
The factory pattern is as a interface that can be used to
create objects, usually which belong to a set or category.
The factory takes in the attributes of the object to be
created, and then returns a new instance of our object.
Factory
fnto Mngr(cni ){
ucin aae
ofg
ti.ae=cni.ae| 'ihe Sot;
hsnm
ofgnm | Mcal ct'
ti.oe=cni.oe| 0
hsrl
ofgrl | ;
ti.sr=cni.sr| 'iheSot2'
hsue
ofgue | Mcalct13;
}
fnto Cnrbtr(cni ){
ucin otiuo
ofg
ti.ae=cni.ae| 'o Sih;
hsnm
ofgnm | Bb mt'
ti.oe=cni.oe| 1
hsrl
ofgrl | ;
ti.sr=cni.sr| 'oSih2'
hsue
ofgue | Bbmt13;
ti.nr=cni.nr| 'iheSot2'
hsmg
ofgmg | Mcalct13;
}
fnto Cnrco (cni ){
ucin otatr
ofg
ti.ae=cni.ae| 'onDe;
hsnm
ofgnm | Jh o'
ti.oe=cni.oe| 2
hsrl
ofgrl | ;
ti.sr=cni.sr| 'ono13;
hsue
ofgue | JhDe2'
ti.nr=cni.nr| 'iheSot2'
hsmg
ofgmg | Mcalct13;
ti.em=cni.em| ' mnh'
hstr
ofgtr | 6 ots;
}
fnto WreFcoy){
ucin okratr( }
WreFcoypooyeepoeCas=Cnrbtr
okratr.rttp.mlyels
otiuo;
WreFcoypooyehrSmoe=fnto (cni )
okratr.rttp.ieoen
ucin
ofg {
Advantages: Factory
Allows the sharing of properties across multiple objects.
Extremely useful when object or component setup is complex.
Also useful when you need to generate different instances based on
context.
Disadvantages: Factory
Fairly complex for smaller applications.
Garbage collection can have high overhead.
Can introduce problems with unit testing.
Common Usage
Address Book (contacts), To-Do App (tasks)
Observer
In the observer pattern, a type of publish-subscribe pattern,
there is an object (often called the subject or observable)
that notifies other objects (observers) of any changes that
occur to the state of the subject. The observers are often
maintained in a list on the observable, to be iterated upon
when a change occurs.
Observer
vrosre ={
a bevr
adusrbr fnto (alak {
dSbcie: ucin clbc)
ti.usrbr[hssbcieslnt]=clbc;
hssbciesti.usrbr.egh
alak
}
,
rmvSbcie:fnto (alak {
eoeusrbr ucin clbc)
fr(a i=0 i<ti.usrbr.egh i+ {
o vr
;
hssbcieslnt; +)
i (hssbciesi ==clbc){
f ti.usrbr[] = alak
dlt(hssbciesi)
eeeti.usrbr[];
}
}
}
,
pbih fnto (ht {
uls: ucin wa)
fr(a i=0 i<ti.usrbr.egh i+ {
o vr
;
hssbcieslnt; +)
Source: For another example using Ember's implementation, check out Chad Hietala's Connection
i ( y e f t i . u s r b rrepository. ' u c i n ) {
f tpo hssbciesi == fnto'
Viewer [ ] =
ti.usrbr[]wa)
hssbciesi(ht;
}
}
}
,
mkPbihr fnto (){/ trsa ojc it apbihr
aeulse: ucin o
/ un n bet no
ulse
fr(a ii ti){
o vr
n hs
oi =ti[]
[]
hsi;
osbcies=[;
.usrbr
]
Advantages: Observer
Faciliates application-level thinking.
Removes direct relationships that are often unnecessary.
Can relate objects without tightly coupling them.
Disadvantages: Observer
There is no way to know if the other end of the telephone is still
listening.
Subscribers aren't aware of one another.
Common Usage
YUI Custom Events. Ember Observables.
Delegate
In the delegate pattern, an object (the delegator) offloads a
task to an associated helper object (the delegate), rather
than performing the task itself. Often times, this is within an
MVC framework or architecture, involving a Controller as
the delegator.
Delegate
vrVeCas=fnto ( {
a iwls
ucin )
rtr {
eun
ii :fnto (l {
nt
ucin e)
vr$l=$e)
a e
(l;

/ RQIE:dlgt sol hv aveWslce mto ipee
/ EURD eeae hud ae
iwaCikd ehd mlmn
$lo(cik,ti.eeaeveWslce)
e.n'lc' hsdlgt.iwaCikd;
}
,
dlgt :nl
eeae
ul
}
;
}
;
vrCnrleCas=fnto ( {
a otolrls
ucin )
rtr {
eun
veWslce :fnto (){
iwaCikd
ucin e
Source: David Drew's fantastic article on implementing Objective-C Delegates in JavaScript
cnoelg' wscle!)
osl.o(I a ald';
}
}
}
;
vrcnrle =nwCnrleCas)
a otolr
e otolrls(;
vrve =nwVeCas)
a iw
e iwls(;
ve.eeae=cnrle;
iwdlgt
otolr
ve.ntdcmn.eEeetyd'yedr);
iwii(ouetgtlmnBI(mHae')
Advantages: Delegate
Delegation allows for loose coupling without global eventing.
Easy to maintain structure within an application.
Disadvantages: Delegate
No well-structured way to enforce delegation attachment.
Assignment of delegate takes place prior to initialization.
Common Usage
Most MVC frameworks at least involve delegation in their
design in some regard, due to the interaction between
models, views, and controllers.
Helpful Resources & Reading
Learning JavaScript Design Patterns by Addy Osmani
JavaScript Patterns on GitHub
Carl Danley: JavaScript Design Patterns
JavaScript Design Patterns on Adobe DevNet
Pro JavaScript Design Patterns
Zakas on the Factory Pattern
JavaScript Design Patterns
Fin.

Contenu connexe

Tendances

5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScriptTodd Anglin
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
JavaScript and the AST
JavaScript and the ASTJavaScript and the AST
JavaScript and the ASTJarrod Overson
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016Manoj Kumar
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: PrototypesVernon Kesner
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Wilson Su
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Wilson Su
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java DevelopersBob McCune
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascriptDoeun KOCH
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To ClosureRobert Nyman
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptStoyan Stefanov
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptMichael Girouard
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almostQuinton Sheppard
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptDonald Sipe
 

Tendances (20)

JavaScript Patterns
JavaScript PatternsJavaScript Patterns
JavaScript Patterns
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
JavaScript and the AST
JavaScript and the ASTJavaScript and the AST
JavaScript and the AST
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java Developers
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To Closure
 
Introduction of ES2015
Introduction of ES2015Introduction of ES2015
Introduction of ES2015
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScript
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 

Similaire à JavaScript Design Patterns

nescala 2013
nescala 2013nescala 2013
nescala 2013Hung Lin
 
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best PracticesJohannes Hoppe
 
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1Ganesh Samarthyam
 
SecureSocial - Authentication for Play Framework
SecureSocial - Authentication for Play FrameworkSecureSocial - Authentication for Play Framework
SecureSocial - Authentication for Play Frameworkjaliss
 
Javascript fundamentals for php developers
Javascript fundamentals for php developersJavascript fundamentals for php developers
Javascript fundamentals for php developersChris Ramakers
 
Architecting Android Apps: Marko Gargenta
Architecting Android Apps: Marko GargentaArchitecting Android Apps: Marko Gargenta
Architecting Android Apps: Marko Gargentajaxconf
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...corehard_by
 
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...Codemotion
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
Clojurescript up and running
Clojurescript up and runningClojurescript up and running
Clojurescript up and runningTimo Sulg
 
Building modern web apps with html5, javascript, and java
Building modern web apps with html5, javascript, and javaBuilding modern web apps with html5, javascript, and java
Building modern web apps with html5, javascript, and javaAlexander Gyoshev
 
Beginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at GoogleBeginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at GoogleAri Lerner
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
Java 8 - project lambda
Java 8 - project lambdaJava 8 - project lambda
Java 8 - project lambdaIvar Østhus
 
Drupal 8 in action, the route to the method
Drupal 8 in action, the route to the methodDrupal 8 in action, the route to the method
Drupal 8 in action, the route to the methodjuanolalla
 

Similaire à JavaScript Design Patterns (20)

nescala 2013
nescala 2013nescala 2013
nescala 2013
 
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
 
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
 
SecureSocial - Authentication for Play Framework
SecureSocial - Authentication for Play FrameworkSecureSocial - Authentication for Play Framework
SecureSocial - Authentication for Play Framework
 
Javascript fundamentals for php developers
Javascript fundamentals for php developersJavascript fundamentals for php developers
Javascript fundamentals for php developers
 
Architecting Android Apps: Marko Gargenta
Architecting Android Apps: Marko GargentaArchitecting Android Apps: Marko Gargenta
Architecting Android Apps: Marko Gargenta
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
 
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
 
JavaFX, because you're worth it
JavaFX, because you're worth itJavaFX, because you're worth it
JavaFX, because you're worth it
 
Enumerable
EnumerableEnumerable
Enumerable
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
C++ boot camp part 1/2
C++ boot camp part 1/2C++ boot camp part 1/2
C++ boot camp part 1/2
 
C++ Boot Camp Part 1
C++ Boot Camp Part 1C++ Boot Camp Part 1
C++ Boot Camp Part 1
 
Clojurescript up and running
Clojurescript up and runningClojurescript up and running
Clojurescript up and running
 
Building modern web apps with html5, javascript, and java
Building modern web apps with html5, javascript, and javaBuilding modern web apps with html5, javascript, and java
Building modern web apps with html5, javascript, and java
 
Beginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at GoogleBeginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at Google
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Java 8 - project lambda
Java 8 - project lambdaJava 8 - project lambda
Java 8 - project lambda
 
Javascript: The Important Bits
Javascript: The Important BitsJavascript: The Important Bits
Javascript: The Important Bits
 
Drupal 8 in action, the route to the method
Drupal 8 in action, the route to the methodDrupal 8 in action, the route to the method
Drupal 8 in action, the route to the method
 

Plus de Derek Brown

Gospel Formed - Drinking Deeply
Gospel Formed - Drinking DeeplyGospel Formed - Drinking Deeply
Gospel Formed - Drinking DeeplyDerek Brown
 
Be Not Afraid: The Christian & Fear
Be Not Afraid: The Christian & FearBe Not Afraid: The Christian & Fear
Be Not Afraid: The Christian & FearDerek Brown
 
Reverse Engineering Your Life
Reverse Engineering Your LifeReverse Engineering Your Life
Reverse Engineering Your LifeDerek Brown
 
1 Timothy 6:2-10
1 Timothy 6:2-101 Timothy 6:2-10
1 Timothy 6:2-10Derek Brown
 
Sent: Part One - The Mixed Multitude
Sent: Part One - The Mixed MultitudeSent: Part One - The Mixed Multitude
Sent: Part One - The Mixed MultitudeDerek Brown
 
Honoring Those in Spiritual and Economic Authority
Honoring Those in Spiritual and Economic AuthorityHonoring Those in Spiritual and Economic Authority
Honoring Those in Spiritual and Economic AuthorityDerek Brown
 
Serve the Church
Serve the ChurchServe the Church
Serve the ChurchDerek Brown
 
Hillside Men - 1 Timothy 5:1-2
Hillside Men - 1 Timothy 5:1-2Hillside Men - 1 Timothy 5:1-2
Hillside Men - 1 Timothy 5:1-2Derek Brown
 
Hillside Men - 1 Timothy 4:11-15
Hillside Men - 1 Timothy 4:11-15Hillside Men - 1 Timothy 4:11-15
Hillside Men - 1 Timothy 4:11-15Derek Brown
 
Hillside Men - 1 Timothy 4:6-10
Hillside Men - 1 Timothy 4:6-10Hillside Men - 1 Timothy 4:6-10
Hillside Men - 1 Timothy 4:6-10Derek Brown
 
Flood: Seeing Beauty in Tragedy
Flood: Seeing Beauty in TragedyFlood: Seeing Beauty in Tragedy
Flood: Seeing Beauty in TragedyDerek Brown
 
Hillside Men - 1 Timothy 4:1-5
Hillside Men - 1 Timothy 4:1-5Hillside Men - 1 Timothy 4:1-5
Hillside Men - 1 Timothy 4:1-5Derek Brown
 
Hillside Men - 1 Timothy 3:14-16
Hillside Men - 1 Timothy 3:14-16Hillside Men - 1 Timothy 3:14-16
Hillside Men - 1 Timothy 3:14-16Derek Brown
 
Hillside Men - 1 Timothy 3:8-13
Hillside Men - 1 Timothy 3:8-13Hillside Men - 1 Timothy 3:8-13
Hillside Men - 1 Timothy 3:8-13Derek Brown
 
Pdf hillside men - 1 timothy 2.8-15
Pdf   hillside men - 1 timothy 2.8-15Pdf   hillside men - 1 timothy 2.8-15
Pdf hillside men - 1 timothy 2.8-15Derek Brown
 
Hillside Men: 1 Timothy 3:1-7
Hillside Men: 1 Timothy 3:1-7Hillside Men: 1 Timothy 3:1-7
Hillside Men: 1 Timothy 3:1-7Derek Brown
 
Hillside Men: 1 Timothy 2:1-8
Hillside Men: 1 Timothy 2:1-8Hillside Men: 1 Timothy 2:1-8
Hillside Men: 1 Timothy 2:1-8Derek Brown
 
Hillside Men: 1 Timothy 1:12-20
Hillside Men: 1 Timothy 1:12-20Hillside Men: 1 Timothy 1:12-20
Hillside Men: 1 Timothy 1:12-20Derek Brown
 
Hillside Men: 1 Timothy 1:3-11
Hillside Men: 1 Timothy 1:3-11Hillside Men: 1 Timothy 1:3-11
Hillside Men: 1 Timothy 1:3-11Derek Brown
 

Plus de Derek Brown (20)

Gospel Formed - Drinking Deeply
Gospel Formed - Drinking DeeplyGospel Formed - Drinking Deeply
Gospel Formed - Drinking Deeply
 
Be Not Afraid: The Christian & Fear
Be Not Afraid: The Christian & FearBe Not Afraid: The Christian & Fear
Be Not Afraid: The Christian & Fear
 
Reverse Engineering Your Life
Reverse Engineering Your LifeReverse Engineering Your Life
Reverse Engineering Your Life
 
1 Timothy 6:2-10
1 Timothy 6:2-101 Timothy 6:2-10
1 Timothy 6:2-10
 
Sent: Part One - The Mixed Multitude
Sent: Part One - The Mixed MultitudeSent: Part One - The Mixed Multitude
Sent: Part One - The Mixed Multitude
 
Honoring Those in Spiritual and Economic Authority
Honoring Those in Spiritual and Economic AuthorityHonoring Those in Spiritual and Economic Authority
Honoring Those in Spiritual and Economic Authority
 
Serve the Church
Serve the ChurchServe the Church
Serve the Church
 
Hillside Men - 1 Timothy 5:1-2
Hillside Men - 1 Timothy 5:1-2Hillside Men - 1 Timothy 5:1-2
Hillside Men - 1 Timothy 5:1-2
 
Hillside Men - 1 Timothy 4:11-15
Hillside Men - 1 Timothy 4:11-15Hillside Men - 1 Timothy 4:11-15
Hillside Men - 1 Timothy 4:11-15
 
Hillside Men - 1 Timothy 4:6-10
Hillside Men - 1 Timothy 4:6-10Hillside Men - 1 Timothy 4:6-10
Hillside Men - 1 Timothy 4:6-10
 
Flood: Seeing Beauty in Tragedy
Flood: Seeing Beauty in TragedyFlood: Seeing Beauty in Tragedy
Flood: Seeing Beauty in Tragedy
 
Hillside Men - 1 Timothy 4:1-5
Hillside Men - 1 Timothy 4:1-5Hillside Men - 1 Timothy 4:1-5
Hillside Men - 1 Timothy 4:1-5
 
Hillside Men - 1 Timothy 3:14-16
Hillside Men - 1 Timothy 3:14-16Hillside Men - 1 Timothy 3:14-16
Hillside Men - 1 Timothy 3:14-16
 
Hillside Men - 1 Timothy 3:8-13
Hillside Men - 1 Timothy 3:8-13Hillside Men - 1 Timothy 3:8-13
Hillside Men - 1 Timothy 3:8-13
 
Pdf hillside men - 1 timothy 2.8-15
Pdf   hillside men - 1 timothy 2.8-15Pdf   hillside men - 1 timothy 2.8-15
Pdf hillside men - 1 timothy 2.8-15
 
Hillside Men: 1 Timothy 3:1-7
Hillside Men: 1 Timothy 3:1-7Hillside Men: 1 Timothy 3:1-7
Hillside Men: 1 Timothy 3:1-7
 
Gardens
GardensGardens
Gardens
 
Hillside Men: 1 Timothy 2:1-8
Hillside Men: 1 Timothy 2:1-8Hillside Men: 1 Timothy 2:1-8
Hillside Men: 1 Timothy 2:1-8
 
Hillside Men: 1 Timothy 1:12-20
Hillside Men: 1 Timothy 1:12-20Hillside Men: 1 Timothy 1:12-20
Hillside Men: 1 Timothy 1:12-20
 
Hillside Men: 1 Timothy 1:3-11
Hillside Men: 1 Timothy 1:3-11Hillside Men: 1 Timothy 1:3-11
Hillside Men: 1 Timothy 1:3-11
 

Dernier

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 

Dernier (20)

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 

JavaScript Design Patterns

  • 2. Question Can you drive a nail into a board using a Phillips-head screwdriver?
  • 3. Answer Yep. But it'll suck. Get a hammer, moron.
  • 4. Why Patterns? Great design patterns are reusable, modular expressions of what's going on in your code. They allow you to communicate to other developers simply by the way you code, in addition to being easily maintainable themselves. Put simply, patterns are the available tools in the developer's toolbox.
  • 5. The Patterns We'll Cover There are hundreds of design patterns that can be leveraged for use here in your code base. Ain't nobody got time for that, so we'll cover only a few that are pretty different from one another, but are prevalent in applications, to get a glance at what's out there. For each pattern, we'll look at the following: Definition Code Sample Advantages & Disadvantages Common Usage
  • 6. Module / Revealing Module You are already familiar with this pattern. Trust me. It's one of the most common patterns on the web.
  • 7. Singleton There can be only one....instance of this object.
  • 8. Facade A facade is exactly what it sounds like: makeup on a bulldog. You're covering over complex, ugly things with a simplified interface for future, more scalable development.
  • 9. Command This pattern is useful for action-oriented objects.
  • 10. Factory The factory pattern lets you encapsulate multiple types of objects within a categorical constructor.
  • 11. Observer This is a pattern that we've talked about previously, within the context of Ember. But how can you implement a publish/subscribe model yourself? And when should you?
  • 12. Delegate Delegates are a pattern that allow for event-like communication between components.
  • 13. Module / Revealing Module This is one of the most fundamental design patterns in the JavaScript universe. It's primary use is to include both private and public variables within a single class-like object while at the same time protecting the private methods/properties from the application. Modules accomplish this encapsulation by using closures to protect the private pieces while allowing the developer to determine which pieces of the object should be publicly exposed to the rest of the application.
  • 14. Module / Revealing Module vrmMdl =(ucin( { a youe fnto ) vrmPoet ='au' a yrpry Vle; rtr { eun gtrpry fnto ( { ePoet: ucin ) rtr mPoet; eun yrpry } , strpry fnto (eVle { ePoet: ucin nwau) mPoet =nwau; yrpry eVle } } ; }(; )) mMdl.ePoet(;/ Rtrs'au' youegtrpry) / eun Vle mMdl.ePoet(NwVle) youestrpry'e au'; mMdl.ePoet(;/ Rtrs'e Vle youegtrpry) / eun Nw au'
  • 15. Module / Revealing Module My return object is going to get a bit messy....
  • 16. Module / Revealing Module vrmMdl =(ucin( { a youe fnto ) vrmPoet ='au' a yrpry Vle; fnto gtrpry( { ucin ePoet ) rtr mPoet; eun yrpry } fnto strpry(eVle { ucin ePoet nwau) mPoet =nwau; yrpry eVle } rtr { eun gt gtrpry e: ePoet, st strpry e: ePoet } ; }(; )) mMdl.e(;/ Rtrs'au' youegt) / eun Vle mMdl.e(NwVle) youest'e au'; mMdl.e(;/ Rtrs'e Vle youegt) / eun Nw au'
  • 17. Advantages: Module Clean, readable, & consistent syntax. Less clutter in the global namespace. Allows developers to control scope of properties & methods. Localization of functions & variables
  • 18. Disadvantages: Module Unit testing can be difficult if the methods aren't exposed. Private methods are unaccessible. Can't easily extend private methods.
  • 20. Singleton The singleton pattern is extremely useful if you only one instance of the object to ever exist. Basically, what occurs within the Singleton pattern is that you write your object, and then as a part of that object, you have an additional method. This function simply checks if an instance of the object already exists. If it does, use that instance. If not, then create a new one and store the reference.
  • 21. Singleton vrmSnltn=(ucin( { a yigeo fnto ) vrisac, a ntne mPoet ='au' yrpry Vle; fnto gtrpry( { ucin ePoet ) rtr mPoet; eun yrpry } fnto strpry(eVle { ucin ePoet nwau) mPoet =nwau; yrpry eVle } fnto iiilz ( { ucin ntaie ) rtr { eun gt gtrpry e: ePoet, st strpry e: ePoet } ; } rtr { eun gtntne fnto ( { eIsac: ucin ) i (!ntne){ f isac isac =iiilz(; ntne ntaie)
  • 22. Advantages: Singleton Reduced memory usage Single point of access Delayed initialization prevents instantiation until required
  • 23. Disadvantages: Singleton Once an instance exists, it's difficult to "reset". Harder to unit test.
  • 25. Facade The facade pattern is often paired with other patterns to add an extra layer of security while at the same time providing a simpler interface to the underlying functionality.
  • 26. Facade vrmFcd =(ucin( { a yaae fnto ) vrmPoet ='au' a yrpry Vle; fnto gtrpry( { ucin ePoet ) rtr mPoet; eun yrpry } ; fnto strpry(eVle { ucin ePoet nwau) mPoet =nwau; yrpry eVle } rtr { eun gt fnto ( {rtr gtrpry) } e: ucin ) eun ePoet(; , st fnto (eVle {strprynwau) } e: ucin nwau) ePoet(eVle; } ; }(; ))
  • 27. Advantages: Facade Enhances security, as internal functions aren't exposed. Easy to implement Works well with other design patterns. Easy to patch internals Provides a simple public interface
  • 28. Disadvantages: Facade Is the cost of implementation really worth the extra layer of abstraction?
  • 29. Common Usage One of the most prevalent uses of the Facade pattern ever: jQuery
  • 30. Command The command pattern completely separates the implementation and execution of methods. Usually, in order to execute a method, you directly invoke the method itself. The command pattern takes the name of the method to execute as an argument into an "execute" or "run" method on the command object, applying the rest of the arguments list to the function being invoked. In most programming, objects represent nouns. In the command pattern, objects are verbs.
  • 31. Command vrmCmad={ a yomn rqetaa fnto (d atiue { eusDt: ucin i, trbt) rtr 'aaatiue'+atiue+'hsbe rqetdfrojc ' eun Dt trbt trbt a en euse o bet } , ceraa fnto (d atiue { laDt: ucin i, trbt) rtr 'h dt atiue'+atiue+'hsbe rstfrojc ' eun Te aa trbt trbt a en ee o bet } } mCmadrn=fnto (omn){ yomn.u ucin cmad rtr mCmadcmadrqet(omn.d cmadatiue eun yomn[omn.eus]cmadi, omn.trbt) } ; / * Atraiey ternfnto cudlo lk ti,wihwudps eta lentvl, h u ucin ol ok ie hs hc ol as xr mCmadrn=fnto (omn){ yomn.u ucin cmad rtr mCmadcmad.pl(mCmad [.lc.alagmns 1) eun yomn[omn]apy yomn, ]siecl(ruet, ); } ; o fragmnls cmad,yucng ee smlr r o ruetes omns o a o vn ipe: mCmadrn=fnto (omn){ yomn.u ucin cmad
  • 32. Advantages: Command Decouples implementation from execution, which allows for extensibility while minimizing code changes. Stacking command objects allows you to cache them, store them in a history, or otherwise manipulate them. Undo, anyone?
  • 33. Disadvantages: Command Counter-intuitive to most OOP practices. Very limited use to 'verb-centric' applications.
  • 35. Factory The factory pattern is as a interface that can be used to create objects, usually which belong to a set or category. The factory takes in the attributes of the object to be created, and then returns a new instance of our object.
  • 36. Factory fnto Mngr(cni ){ ucin aae ofg ti.ae=cni.ae| 'ihe Sot; hsnm ofgnm | Mcal ct' ti.oe=cni.oe| 0 hsrl ofgrl | ; ti.sr=cni.sr| 'iheSot2' hsue ofgue | Mcalct13; } fnto Cnrbtr(cni ){ ucin otiuo ofg ti.ae=cni.ae| 'o Sih; hsnm ofgnm | Bb mt' ti.oe=cni.oe| 1 hsrl ofgrl | ; ti.sr=cni.sr| 'oSih2' hsue ofgue | Bbmt13; ti.nr=cni.nr| 'iheSot2' hsmg ofgmg | Mcalct13; } fnto Cnrco (cni ){ ucin otatr ofg ti.ae=cni.ae| 'onDe; hsnm ofgnm | Jh o' ti.oe=cni.oe| 2 hsrl ofgrl | ; ti.sr=cni.sr| 'ono13; hsue ofgue | JhDe2' ti.nr=cni.nr| 'iheSot2' hsmg ofgmg | Mcalct13; ti.em=cni.em| ' mnh' hstr ofgtr | 6 ots; } fnto WreFcoy){ ucin okratr( } WreFcoypooyeepoeCas=Cnrbtr okratr.rttp.mlyels otiuo; WreFcoypooyehrSmoe=fnto (cni ) okratr.rttp.ieoen ucin ofg {
  • 37. Advantages: Factory Allows the sharing of properties across multiple objects. Extremely useful when object or component setup is complex. Also useful when you need to generate different instances based on context.
  • 38. Disadvantages: Factory Fairly complex for smaller applications. Garbage collection can have high overhead. Can introduce problems with unit testing.
  • 40. Observer In the observer pattern, a type of publish-subscribe pattern, there is an object (often called the subject or observable) that notifies other objects (observers) of any changes that occur to the state of the subject. The observers are often maintained in a list on the observable, to be iterated upon when a change occurs.
  • 41. Observer vrosre ={ a bevr adusrbr fnto (alak { dSbcie: ucin clbc) ti.usrbr[hssbcieslnt]=clbc; hssbciesti.usrbr.egh alak } , rmvSbcie:fnto (alak { eoeusrbr ucin clbc) fr(a i=0 i<ti.usrbr.egh i+ { o vr ; hssbcieslnt; +) i (hssbciesi ==clbc){ f ti.usrbr[] = alak dlt(hssbciesi) eeeti.usrbr[]; } } } , pbih fnto (ht { uls: ucin wa) fr(a i=0 i<ti.usrbr.egh i+ { o vr ; hssbcieslnt; +) Source: For another example using Ember's implementation, check out Chad Hietala's Connection i ( y e f t i . u s r b rrepository. ' u c i n ) { f tpo hssbciesi == fnto' Viewer [ ] = ti.usrbr[]wa) hssbciesi(ht; } } } , mkPbihr fnto (){/ trsa ojc it apbihr aeulse: ucin o / un n bet no ulse fr(a ii ti){ o vr n hs oi =ti[] [] hsi; osbcies=[; .usrbr ]
  • 42. Advantages: Observer Faciliates application-level thinking. Removes direct relationships that are often unnecessary. Can relate objects without tightly coupling them.
  • 43. Disadvantages: Observer There is no way to know if the other end of the telephone is still listening. Subscribers aren't aware of one another.
  • 44. Common Usage YUI Custom Events. Ember Observables.
  • 45. Delegate In the delegate pattern, an object (the delegator) offloads a task to an associated helper object (the delegate), rather than performing the task itself. Often times, this is within an MVC framework or architecture, involving a Controller as the delegator.
  • 46. Delegate vrVeCas=fnto ( { a iwls ucin ) rtr { eun ii :fnto (l { nt ucin e) vr$l=$e) a e (l; / RQIE:dlgt sol hv aveWslce mto ipee / EURD eeae hud ae iwaCikd ehd mlmn $lo(cik,ti.eeaeveWslce) e.n'lc' hsdlgt.iwaCikd; } , dlgt :nl eeae ul } ; } ; vrCnrleCas=fnto ( { a otolrls ucin ) rtr { eun veWslce :fnto (){ iwaCikd ucin e Source: David Drew's fantastic article on implementing Objective-C Delegates in JavaScript cnoelg' wscle!) osl.o(I a ald'; } } } ; vrcnrle =nwCnrleCas) a otolr e otolrls(; vrve =nwVeCas) a iw e iwls(; ve.eeae=cnrle; iwdlgt otolr ve.ntdcmn.eEeetyd'yedr); iwii(ouetgtlmnBI(mHae')
  • 47. Advantages: Delegate Delegation allows for loose coupling without global eventing. Easy to maintain structure within an application.
  • 48. Disadvantages: Delegate No well-structured way to enforce delegation attachment. Assignment of delegate takes place prior to initialization.
  • 49. Common Usage Most MVC frameworks at least involve delegation in their design in some regard, due to the interaction between models, views, and controllers.
  • 50. Helpful Resources & Reading Learning JavaScript Design Patterns by Addy Osmani JavaScript Patterns on GitHub Carl Danley: JavaScript Design Patterns JavaScript Design Patterns on Adobe DevNet Pro JavaScript Design Patterns Zakas on the Factory Pattern JavaScript Design Patterns
  • 51. Fin.