Factory and Decorator in AngularJS

Factory pattern is very ubiquitous design pattern which finds its uses in most of the design strategy. In this post we will use factory with Decorator and demonstrate - how decorator can be used along side of factory and provide extra functionality without disturbing cleanness of factory.

We will take user input and in return displays reversed output of that input string. Factory comes into picture to give an instance which contains getter and setter method. We define decorator for this Factory instance and reverse input string set via setter method.

Script for Factory and Decorator:


'use strict'

var app = angular.module('DecoratorApp', []);

app.controller("decoratorController", function($scope, reverseFactory) {
 $scope.reverseCall = function() {
  reverseFactory.setData($scope.inputVal);
  reverseFactory.reverse();
  $scope.outputVal = reverseFactory.getData();
 }
});

app.factory('reverseFactory', function() {
 var myPrivateData;
 return {
  getData : function() {
   return myPrivateData
  },
  setData : function(data) {
   return myPrivateData = data
  }
 }
})

/* Decoration created for factory reverseFactory */
app.config(function($provide) {
 $provide.decorator('reverseFactory', function($delegate) {
  $delegate.reverse = function() {
   $delegate.setData($delegate.getData().split('')
     .reverse().join(''))
  }
  return $delegate
 })
})

HTML for input and output  :
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Filter application</title>
</head>
<body ng-app="DecoratorApp" ng-controller="decoratorController" ng-cloak>
 <div class="mainBody">
  <div class="mainBodyMinHeight">
   <img ng-src="https://toddmotto.com/img/tags/angularjs.svg"
    height="100px">
   <p>
    <label>Input String: </label>
   </p>
   <div flex="100">
    <input type='text' ng-model="inputVal" ng-blur="reverseCall()" />
   </div>
   <hr />
   Reversed String:
   <div flex="100">
    <p>{{outputVal}}</p>
   </div>
  </div>
 </div>
 <script
  src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/
 1.4.3/angular.min.js"></script>
 <script src="scripts/decoratorApp.js"></script>
</body>
</html>

Output:-



Download complete source code.

7 Comments




  1. Our Russian Escorts in Mahipalpur have smooth bodies that can prepare you for a night of sexual loving. You don’t want to feel self-conscious as you’re safe here at our Call Girls agency. Our Services are all-time service that is accessible for you 24*7 hours. Our Escorts Girls are always prepared to make you fill you’ll have a fantastic alluring night with our lovely girls and you only need for believing simultaneously. Check our other Services also...
    Call Girl in Chanakyapuri
    Independent Call Girls in Ahmedabad
    Escorts in Agra
    Call Girls in Pune

    ReplyDelete


  2. We have been in the industry for several years now and have been able to satisfy hundreds of clients who have come back to us wanting more. Our Escorts Service in Agra to book so that their needs may be fulfilled. We establish a detailed conversation with our clients and help them choose the best amongst the various choices available.
    Female Escorts in Agra
    Russian Escorts in Agra
    Female Escorts in Faridabad
    Call Girls in Faridabad

    ReplyDelete




  3. Also known to come with crucial offerings for its sensible, down-to-earth and important clients,Call Girls in Gurgaon really means in fulfilling their lives with cheerful moments to make love in concern to ensure them live their lives in abetter way. Check our other Services...
    Charges of Call Girls in Gurgaon
    Escorts Service in Gurgaon
    Female Escorts in Gurgaon
    Gurgaon Escorts Service
    Housewife Escorts in Gurgaon



    ReplyDelete
  4. Don’t you have got any put, where you'll be able go with these astonishing Call girls in Mumbai but still need to have a few fun with them? In case, the reply is towards positive side, hold up no assist and get in touch with solid Mumbai Escort service, which our young ladies are continuously prepared to offer you with. Here, you've got the freedom to visit our young ladies, right at their given address, and spend a few astounding minutes and make a few extraordinary comes about, within the conclusion.
    jalna-escorts l
    kolhapur-escorts l
    latur-escorts l
    nagpur-escorts l

    ReplyDelete
  5. If you happen to be in the Surat region with a Surat Escort, I can profoundly suggest booking a table for two at Dipal de Luxe. This superb eatery won the grant for the finest French eatery of the year 2008 and essentially cannot be beaten for fashion and course. After your extravagant feast takes a delightful walk down Surat to the adjacent Dipal Sharma where you and your Independent Escort in Surat can enjoy some of the uncommonly outlined cocktails on offer. Here you'll be able to unwind in one of the comfortable booths, listen to some cool offbeat music, and truly get to know your Surat Call Girl. If this bar may be small as well calm for you save the time for a visit to the Playroom where the energetic environment and live music will have you and your byasanagar-escorts moving in no time. At this club, you'll be able to appreciate more tasty cocktails and you'll be able both truly let your hair down. chandipur-escorts
    chatrapur-escorts
    chaudwar-escorts

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
Previous Post Next Post