How AngularJS Embraced Traditional Design Patterns
Upcoming SlideShare
Loading in...5
×
 

How AngularJS Embraced Traditional Design Patterns

on

  • 6,075 views

Ran Mizrahi talks about how AngularJS embraced traditional design patterns and used the benefits of JavaScript for that cause.

Ran Mizrahi talks about how AngularJS embraced traditional design patterns and used the benefits of JavaScript for that cause.

Statistics

Views

Total Views
6,075
Views on SlideShare
5,924
Embed Views
151

Actions

Likes
4
Downloads
34
Comments
1

4 Embeds 151

https://twitter.com 141
http://www.linkedin.com 5
http://www.pearltrees.com 4
https://tweetdeck.twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • I really enjoy the way AngularJS approaches JavaScript: No 'class' at all!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

How AngularJS Embraced Traditional Design Patterns How AngularJS Embraced Traditional Design Patterns Presentation Transcript

  • 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?