SlideShare a Scribd company logo
How Angular Embraced
Traditional OOP Design
Patterns

Ran Mizrahi (@ranm8)

Open Source Dpt. Leader @ CodeOasis
About CodeOasis

•
•
•
•

CodeOasis specializes in cutting-edge web solutions.

!

Large variety of customers (from startups to enterprises).

!

We’re passionate about JavaScript (-:

!

Technologies we love:


•
•
•
•
•
•

!

Play Framework

AngularJS 

nodeJS 

HTML5

CSS3

!

Our Microsoft department works with C#, WPF, etc.
Implementing
Design Patterns in
JavaScript
Implementing Design Patterns in JavaScript
Most traditional object-oriented languages

•
•
•
•
•

Classes.

!

Interfaces.

!

Inheritance.

!

Encapsulation.

!

Polymorphism.
Implementing Design Patterns in JavaScript
We don’t have classes…
var User = new Class({
initialize: function(name) {
this.name = name;
},
setEmail: function(mail) {
this.mail = mail;
}
});
!
var someUser = new User('Ran Mizrahi');
* MooTools Example
Implementing Design Patterns in JavaScript
We don’t have interfaces…

var DynamicMap = new Interface('DynamicMap', ['centerOnPoint', 'zoom',
'draw']);

!

function displayRoute(mapInstance) {
Interface.ensureImplements(mapInstance, DynamicMap);

!

mapInstace.centerOnPoint(12, 34);
mapInstace.draw(5);
mapInstace.zoom();
}
* Taken from the book “Pro JavaScript Design Patterns”
Implementing Design Patterns in JavaScript
We don’t have classical inheritance..
var User = new Class({
!
extends: BaseUser,
initialize: function(name) {
this.name = name;
},
setEmail: function(mail) {
this.mail = mail;
}
});
!
var someUser = new User('Ran Mizrahi');
* MooTools Example
Stop Forcing It!
Embrace It!
Learning To Embrace JavaScript

“When I see a bird that walks
like a duck and sounds like a
duck, I call that bird a duck”
— James Whitcomb Riley
Learning To Embrace JavaScript

This is how a duck looks like…

function isWindow(obj) {
return obj && obj.document && obj.location
&& obj.alert && obj.setInterval;
}
* Angular implementation of the isWindow method…
Learning To Embrace JavaScript
JavaScript is simple (can be explained over a beer) and
makes us write less code…
Java:!
  List<Book> books = new ArrayList<Book>();!

!

JavaScript:!
  var books = [];!
Learning To Embrace JavaScript
Object is a unit responsible for state and behaviour and
JavaScript function has them both.
function Unit() {
var state;
!
function behaviour() {
// Some behaviour
}

}

return function() {
// Do something
}
Learning To Embrace JavaScript
We don’t have visibility keywords (e.g. private, protected,
public) but we do have closures
var Service = (function(window, undefined) {
// Private
function base64Encode(string) {
return window.btoa(string);
}
// Public
return {
encode: function(string) {
return base64Encode(string);
}
};
}(window));
Learning To Embrace JavaScript
State management can be as easy by using constructor
functions or closures…
function someFunction(baseUrl) {
var config = {
url: baseUrl
};

};

return function() {
return config.url + '/hey';
}

function Duck(name) {
// Object state
this.name = name;
}
Learning To Embrace JavaScript
Polymorphism can be beautiful without interfaces, if it’s just a
duck…
function Duck() {
// Private state here…

!

function Bird() {
// Private state here…

!

// Public state
return {
walk: function() {
// Walk like a duck
return quack();
},

// Public state
return {
walk: function() {
// Walk like a bird
return tweet();
},

talk: function() {
// Talk like a duck
}

talk: function() {
// Talk like a bird
}

};
}

};
}
function talker(someBird) {
// Make it talk
someBird.talk();
}
Learning To Embrace JavaScript
But what will happen if the duck won’t talk…

JavaScript has prototypical inheritance but yet, I don’t often
feel I need to inherit anything (prefer polymorphic composition
over inheritance).
Learning To Embrace JavaScript
For true coverage and confidence, compilers won’t do the job
and they can’t replace real unit test coverage.

•
•
•

Almost everything is mutable.

!

Easily stub anything.

!

Easily fake dependencies.
Design Patterns in
AngularJS
MVC and Controllers
angular.module('myModule')
.controller('MyCtrl', ['$http', MyCtrl]);
!
// Dependencies are in closure (-:
function MyCtrl($http) {
var someState = {};

}

function doSomething() {
// Closure is accessible.
}

•

Controllers are just JavaScript function. 


•

They can encapsulate and preserve state by using closures.


•

!

Exposes behaviour with $scope.
Dependency Injection
angular.module('myModule')
.controller('MyCtrl', ['$http', MyCtrl]);
function MyCtrl($http) {
$http.get('http://google.com').then(getTheMonkey);
}

•

Simple dynamic dependency injection based on arrays and
naming convention. 


•

Makes your code polymorphic by easily replacing your
implementation.


•

!

Super easy to isolate and test.
Decorator
Decorator is a pattern used to “decorate” functionality of
certain object.
Decorator
Angular made service decoration really easy…
$provider.decorator('$log', ['delegate', function(delegate) {
// Convert warning to error
delegate.warn = delegate.error;
// Return the decorated service
return delegate;
}]);

Ask for a function, manipulate it and return a new one (-:
Facade
A facade is an object that provides a simplified interface to a
larger body of code and logic.
angular.module('myModule')
.factory('ReallyComplicatedService', [Service]);

!

function Service($http) {
// Do all the private stuff and handle the other library
// Expose really simple interface
return {
get: function() {
// Get it
},
del: function() {
// Delete it
}
};
}
Singleton
Singletons is a design pattern that restricts the instantiation of
a class to one object.
var Singleton = (function() {
function privateFunction() {
// Do private stuff
}
return {
someMethod: function() {
// Do public stuff
},
anotherMethod: function() {
// Do some more public stuff
}

};
}());

JavaScript makes singleton really easy (-: 

But still, it’s global and hard to configure…
Singleton
Angular used it and provided us with dependency injection to
avoid singleton downsides among others.
angular.module('myModule')
.provider('myHttp', myHttp);
function myHttp() {
var baseUrl;
this.baseUrl = function(value) {
if (!value) {
return baseUrl;
}
};

}

baseUrl = value;

this.$get = ['$q', function() {
// myHttp service implementation...
}];
Thank you!

Questions?
Ad

Recommended

Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
Intro to JavaScript Testing
Intro to JavaScript Testing
Ran Mizrahi
 
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Ran Mizrahi
 
Dependency Injection @ AngularJS
Dependency Injection @ AngularJS
Ran Mizrahi
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
Doris Chen
 
Javascript Best Practices
Javascript Best Practices
Christian Heilmann
 
JavaScript 101
JavaScript 101
ygv2000
 
Javascript and Jquery Best practices
Javascript and Jquery Best practices
Sultan Khan
 
Intro to JavaScript
Intro to JavaScript
Yakov Fain
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Angularjs
Angularjs
Vincenzo Ferrari
 
Javascript Module Patterns
Javascript Module Patterns
Nicholas Jansma
 
Java script tutorial
Java script tutorial
Doeun KOCH
 
JavaScript Modules Done Right
JavaScript Modules Done Right
Mariusz Nowak
 
Dart for Java Developers
Dart for Java Developers
Yakov Fain
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
Visual Engineering
 
Secrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
JavaScript Library Overview
JavaScript Library Overview
jeresig
 
AngularJS Basics
AngularJS Basics
Ravi Mone
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
Mike Subelsky
 
Javascript
Javascript
theacadian
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
Jyothishmathi Institute of Technology and Science Karimnagar
 
Angularjs Basics
Angularjs Basics
Anuradha Bandara
 
Lecture 5 javascript
Lecture 5 javascript
Mujtaba Haider
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Apaichon Punopas
 
Javascript basics for automation testing
Javascript basics for automation testing
Vikas Thange
 
Architektura ngrx w angular 2+
Architektura ngrx w angular 2+
Paweł Żurowski
 
AngularJS Introduction
AngularJS Introduction
Carlos Morales
 

More Related Content

What's hot (20)

Javascript and Jquery Best practices
Javascript and Jquery Best practices
Sultan Khan
 
Intro to JavaScript
Intro to JavaScript
Yakov Fain
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Angularjs
Angularjs
Vincenzo Ferrari
 
Javascript Module Patterns
Javascript Module Patterns
Nicholas Jansma
 
Java script tutorial
Java script tutorial
Doeun KOCH
 
JavaScript Modules Done Right
JavaScript Modules Done Right
Mariusz Nowak
 
Dart for Java Developers
Dart for Java Developers
Yakov Fain
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
Visual Engineering
 
Secrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
JavaScript Library Overview
JavaScript Library Overview
jeresig
 
AngularJS Basics
AngularJS Basics
Ravi Mone
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
Mike Subelsky
 
Javascript
Javascript
theacadian
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
Jyothishmathi Institute of Technology and Science Karimnagar
 
Angularjs Basics
Angularjs Basics
Anuradha Bandara
 
Lecture 5 javascript
Lecture 5 javascript
Mujtaba Haider
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Apaichon Punopas
 
Javascript basics for automation testing
Javascript basics for automation testing
Vikas Thange
 
Javascript and Jquery Best practices
Javascript and Jquery Best practices
Sultan Khan
 
Intro to JavaScript
Intro to JavaScript
Yakov Fain
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Javascript Module Patterns
Javascript Module Patterns
Nicholas Jansma
 
Java script tutorial
Java script tutorial
Doeun KOCH
 
JavaScript Modules Done Right
JavaScript Modules Done Right
Mariusz Nowak
 
Dart for Java Developers
Dart for Java Developers
Yakov Fain
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
Visual Engineering
 
Secrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
JavaScript Library Overview
JavaScript Library Overview
jeresig
 
AngularJS Basics
AngularJS Basics
Ravi Mone
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
Mike Subelsky
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Apaichon Punopas
 
Javascript basics for automation testing
Javascript basics for automation testing
Vikas Thange
 

Viewers also liked (20)

Architektura ngrx w angular 2+
Architektura ngrx w angular 2+
Paweł Żurowski
 
AngularJS Introduction
AngularJS Introduction
Carlos Morales
 
React 101
React 101
Casear Chu
 
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
Carlos Sierra
 
Quick introduction to Angular 4 for AngularJS 1.5 developers
Quick introduction to Angular 4 for AngularJS 1.5 developers
Paweł Żurowski
 
Introduction to Angular 2
Introduction to Angular 2
Knoldus Inc.
 
Getting Started with Angular 2
Getting Started with Angular 2
FITC
 
Drfowlerpix
Drfowlerpix
Laurie Fowler
 
Brazilian protests – June 2013
Brazilian protests – June 2013
Luciana Viter
 
Wix Application Framework
Wix Application Framework
Ran Mizrahi
 
Ran Mizrahi - Symfony2 meets Drupal8
Ran Mizrahi - Symfony2 meets Drupal8
Ran Mizrahi
 
0318
0318
guest1a335
 
00 are you free - taxis
00 are you free - taxis
Luciana Viter
 
John Mitchell Presentation Oct 08 Ppt 2007
John Mitchell Presentation Oct 08 Ppt 2007
Dan Foster
 
Merkatu ikerketa-Irutxuloko Hitza
Merkatu ikerketa-Irutxuloko Hitza
Itxaso Ferreras
 
Rochelle Michalek
Rochelle Michalek
jlandsman
 
Reading Revolution Blogs
Reading Revolution Blogs
Laurie Fowler
 
The Pmarca Blog Archives
The Pmarca Blog Archives
Razin Mustafiz
 
"Twitter: what's all the fuss about?" / Jenni Lloyd @ Revolution Forum / Nove...
"Twitter: what's all the fuss about?" / Jenni Lloyd @ Revolution Forum / Nove...
Jenni Lloyd
 
Architektura ngrx w angular 2+
Architektura ngrx w angular 2+
Paweł Żurowski
 
AngularJS Introduction
AngularJS Introduction
Carlos Morales
 
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
Carlos Sierra
 
Quick introduction to Angular 4 for AngularJS 1.5 developers
Quick introduction to Angular 4 for AngularJS 1.5 developers
Paweł Żurowski
 
Introduction to Angular 2
Introduction to Angular 2
Knoldus Inc.
 
Getting Started with Angular 2
Getting Started with Angular 2
FITC
 
Brazilian protests – June 2013
Brazilian protests – June 2013
Luciana Viter
 
Wix Application Framework
Wix Application Framework
Ran Mizrahi
 
Ran Mizrahi - Symfony2 meets Drupal8
Ran Mizrahi - Symfony2 meets Drupal8
Ran Mizrahi
 
00 are you free - taxis
00 are you free - taxis
Luciana Viter
 
John Mitchell Presentation Oct 08 Ppt 2007
John Mitchell Presentation Oct 08 Ppt 2007
Dan Foster
 
Merkatu ikerketa-Irutxuloko Hitza
Merkatu ikerketa-Irutxuloko Hitza
Itxaso Ferreras
 
Rochelle Michalek
Rochelle Michalek
jlandsman
 
Reading Revolution Blogs
Reading Revolution Blogs
Laurie Fowler
 
The Pmarca Blog Archives
The Pmarca Blog Archives
Razin Mustafiz
 
"Twitter: what's all the fuss about?" / Jenni Lloyd @ Revolution Forum / Nove...
"Twitter: what's all the fuss about?" / Jenni Lloyd @ Revolution Forum / Nove...
Jenni Lloyd
 
Ad

Similar to How AngularJS Embraced Traditional Design Patterns (20)

All of Javascript
All of Javascript
Togakangaroo
 
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
FITC
 
Javascript the New Parts v2
Javascript the New Parts v2
Federico Galassi
 
Angular js
Angular js
Arun Somu Panneerselvam
 
OOPS JavaScript Interview Questions PDF By ScholarHat
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
AngularJS
AngularJS
Yogesh L
 
Intro to AngularJs
Intro to AngularJs
SolTech, Inc.
 
JavaScript Essentials
JavaScript Essentials
Triphon Statkov
 
The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015
jbandi
 
Angular js 1.3 basic tutorial
Angular js 1.3 basic tutorial
Al-Mutaz Bellah Salahat
 
JavaScript Core
JavaScript Core
François Sarradin
 
Javascript Design Patterns
Javascript Design Patterns
Subramanyan Murali
 
Javascript Common Design Patterns
Javascript Common Design Patterns
Pham Huy Tung
 
Modern JavaScript Development @ DotNetToscana
Modern JavaScript Development @ DotNetToscana
Matteo Baglini
 
JavaScript Beyond jQuery
JavaScript Beyond jQuery
Bobby Bryant
 
JavaScript OOPs
JavaScript OOPs
Johnson Chou
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
Framework prototype
Framework prototype
DevMix
 
Framework prototype
Framework prototype
DevMix
 
Framework prototype
Framework prototype
DevMix
 
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
FITC
 
Javascript the New Parts v2
Javascript the New Parts v2
Federico Galassi
 
OOPS JavaScript Interview Questions PDF By ScholarHat
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015
jbandi
 
Javascript Common Design Patterns
Javascript Common Design Patterns
Pham Huy Tung
 
Modern JavaScript Development @ DotNetToscana
Modern JavaScript Development @ DotNetToscana
Matteo Baglini
 
JavaScript Beyond jQuery
JavaScript Beyond jQuery
Bobby Bryant
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
Framework prototype
Framework prototype
DevMix
 
Framework prototype
Framework prototype
DevMix
 
Framework prototype
Framework prototype
DevMix
 
Ad

Recently uploaded (20)

Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 

How AngularJS Embraced Traditional Design Patterns

  • 1. How Angular Embraced Traditional OOP Design Patterns Ran Mizrahi (@ranm8) Open Source Dpt. Leader @ CodeOasis
  • 2. About CodeOasis • • • • CodeOasis specializes in cutting-edge web solutions. ! Large variety of customers (from startups to enterprises). ! We’re passionate about JavaScript (-: ! Technologies we love: • • • • • • ! Play Framework AngularJS nodeJS HTML5 CSS3 ! Our Microsoft department works with C#, WPF, etc.
  • 4. Implementing Design Patterns in JavaScript Most traditional object-oriented languages • • • • • Classes. ! Interfaces. ! Inheritance. ! Encapsulation. ! Polymorphism.
  • 5. Implementing Design Patterns in JavaScript We don’t have classes… var User = new Class({ initialize: function(name) { this.name = name; }, setEmail: function(mail) { this.mail = mail; } }); ! var someUser = new User('Ran Mizrahi'); * MooTools Example
  • 6. Implementing Design Patterns in JavaScript We don’t have interfaces… var DynamicMap = new Interface('DynamicMap', ['centerOnPoint', 'zoom', 'draw']); ! function displayRoute(mapInstance) { Interface.ensureImplements(mapInstance, DynamicMap); ! mapInstace.centerOnPoint(12, 34); mapInstace.draw(5); mapInstace.zoom(); } * Taken from the book “Pro JavaScript Design Patterns”
  • 7. Implementing Design Patterns in JavaScript We don’t have classical inheritance.. var User = new Class({ ! extends: BaseUser, initialize: function(name) { this.name = name; }, setEmail: function(mail) { this.mail = mail; } }); ! var someUser = new User('Ran Mizrahi'); * MooTools Example
  • 10. Learning To Embrace JavaScript “When I see a bird that walks like a duck and sounds like a duck, I call that bird a duck” — James Whitcomb Riley
  • 11. Learning To Embrace JavaScript This is how a duck looks like… function isWindow(obj) { return obj && obj.document && obj.location && obj.alert && obj.setInterval; } * Angular implementation of the isWindow method…
  • 12. Learning To Embrace JavaScript JavaScript is simple (can be explained over a beer) and makes us write less code… Java:!   List<Book> books = new ArrayList<Book>();! ! JavaScript:!   var books = [];!
  • 13. Learning To Embrace JavaScript Object is a unit responsible for state and behaviour and JavaScript function has them both. function Unit() { var state; ! function behaviour() { // Some behaviour } } return function() { // Do something }
  • 14. Learning To Embrace JavaScript We don’t have visibility keywords (e.g. private, protected, public) but we do have closures var Service = (function(window, undefined) { // Private function base64Encode(string) { return window.btoa(string); } // Public return { encode: function(string) { return base64Encode(string); } }; }(window));
  • 15. Learning To Embrace JavaScript State management can be as easy by using constructor functions or closures… function someFunction(baseUrl) { var config = { url: baseUrl }; }; return function() { return config.url + '/hey'; } function Duck(name) { // Object state this.name = name; }
  • 16. Learning To Embrace JavaScript Polymorphism can be beautiful without interfaces, if it’s just a duck… function Duck() { // Private state here… ! function Bird() { // Private state here… ! // Public state return { walk: function() { // Walk like a duck return quack(); }, // Public state return { walk: function() { // Walk like a bird return tweet(); }, talk: function() { // Talk like a duck } talk: function() { // Talk like a bird } }; } }; } function talker(someBird) { // Make it talk someBird.talk(); }
  • 17. Learning To Embrace JavaScript But what will happen if the duck won’t talk… JavaScript has prototypical inheritance but yet, I don’t often feel I need to inherit anything (prefer polymorphic composition over inheritance).
  • 18. Learning To Embrace JavaScript For true coverage and confidence, compilers won’t do the job and they can’t replace real unit test coverage. • • • Almost everything is mutable. ! Easily stub anything. ! Easily fake dependencies.
  • 20. MVC and Controllers angular.module('myModule') .controller('MyCtrl', ['$http', MyCtrl]); ! // Dependencies are in closure (-: function MyCtrl($http) { var someState = {}; } function doSomething() { // Closure is accessible. } • Controllers are just JavaScript function. 
 • They can encapsulate and preserve state by using closures. • ! Exposes behaviour with $scope.
  • 21. Dependency Injection angular.module('myModule') .controller('MyCtrl', ['$http', MyCtrl]); function MyCtrl($http) { $http.get('http://google.com').then(getTheMonkey); } • Simple dynamic dependency injection based on arrays and naming convention. 
 • Makes your code polymorphic by easily replacing your implementation. • ! Super easy to isolate and test.
  • 22. Decorator Decorator is a pattern used to “decorate” functionality of certain object.
  • 23. Decorator Angular made service decoration really easy… $provider.decorator('$log', ['delegate', function(delegate) { // Convert warning to error delegate.warn = delegate.error; // Return the decorated service return delegate; }]); Ask for a function, manipulate it and return a new one (-:
  • 24. Facade A facade is an object that provides a simplified interface to a larger body of code and logic. angular.module('myModule') .factory('ReallyComplicatedService', [Service]); ! function Service($http) { // Do all the private stuff and handle the other library // Expose really simple interface return { get: function() { // Get it }, del: function() { // Delete it } }; }
  • 25. Singleton Singletons is a design pattern that restricts the instantiation of a class to one object. var Singleton = (function() { function privateFunction() { // Do private stuff } return { someMethod: function() { // Do public stuff }, anotherMethod: function() { // Do some more public stuff } }; }()); JavaScript makes singleton really easy (-: 
 But still, it’s global and hard to configure…
  • 26. Singleton Angular used it and provided us with dependency injection to avoid singleton downsides among others. angular.module('myModule') .provider('myHttp', myHttp); function myHttp() { var baseUrl; this.baseUrl = function(value) { if (!value) { return baseUrl; } }; } baseUrl = value; this.$get = ['$q', function() { // myHttp service implementation... }];