0

I stumbled across Lukas Ruebbelke excellent book On AngularJS, AngularJS in Action.

When you define a service using a module.service, the instance is returned via a constructor function.This lends itself well to developers who prefer writing Object-oriented code and like to define methods and properties on the this keyword.

angular.module("Mod").
        service("myService", function($rootScope){
                                  var service = this;
                                  service.setLoading = function(loading){
                                      // some code goes here
                                  }
});

Now, he show's the same service using module.factory to achieve the same functionality,

angular.module("Mod").
            factory("myFactory", function($rootScope){
                                      var setLoading = function(loading){
                                          // some code goes here
                                      };

                                      return {
                                          setLoading : setLoading
                                      }
});

which is very similar to the Revealing Module Design pattern.

The question is not related to the difference b/w service and factory, for that is very much cleared to me, but in which scenarios, one of the design pattern mentioned above preferred over the other and vice-versa.

Or is it just user-preference and better to stick with just one of the two?

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
  • 2
    That sentence does sound to me like "*This lends itself well to developers who prefer using the `this` keyword and calling their code "object-oriented" while not understanding the difference*". – Bergi Sep 05 '15 at 09:45

2 Answers2

0

Yes, the one will find himself doing Revealing Pattern with factory most times.

While service and factory practical difference is a bit more than that (service is limited to extending this object while factory can return everything and is not limited to objects), they do the same job in the context of the question.

service syntax with this is efficient for refactoring the controllers with controllerAs syntax, so their methods (while keeping in mind possible $scope dependence) can be copypasted to new service, here's an example of using this for extending controllers via mixins.

The telling argument for service is that it is suited great to be converted to ES2015 or TS class.

Community
  • 1
  • 1
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
0

There are three different implementations of the module pattern. Your question boils down to a comparison between the merits of two of the implementations

  • The Revealing Module Pattern behaves poorly with respect to overriding and prototypal inheritance.
  • The Stub Module Pattern behaves well to overriding but behaves poorly with prototypal inheritance
  • The Original Module Pattern behaves well with both overriding and prototypal inheritance (when executed correctly).

However, the use of this in the Original Module Pattern means that you can't use just pass its functions around as arguments without using some variation of var self = this or function.bind(). Also, the Revealing Module Pattern has the advantage of allowing you to easily change public members to private members and vice versa without having to touch the function body.

So, you might infer from the above that there are tradeoffs and benefits to each implementation and that in some situations you might choose one or another. That would be an incorrect inference, in my opinion. The advantages of the Original Module Pattern using this are substantive -- the objects created with the Original Module Pattern exhibit expected behavior with respect to normal overriding and prototypal overriding. On the other hand, the supposed advantages of the Revealing Module Pattern are mere convenience -- the coder has to type a little bit less code. Thus there are absolutely no technical reasons to prefer Revealing Module Pattern over the Original Module Pattern. One should ALWAYS use the Original Module Pattern.

However, there are indeed non-technical reasons for preferring the Revealing Module Pattern. By completely avoiding the use of this, the Revealing Module Pattern coder doesn't have to learn the nuances of JavaScript's this. This is the real reason, I suspect, for the Revealing Module Pattern's overwhelming popularity.

Community
  • 1
  • 1
I-Lin Kuo
  • 3,220
  • 2
  • 18
  • 25