0

One of the most clearest examples I read about services/factories/providers was that they corresponded to: A car, a factory that gives you cars(e.g. red car, blue car), and a configurable factory that outputs cars(e.g. high-quality cars, low-quality cars)

The part that is confusing me though is the "everything is a singleton" explanation. From the above example I had assumed that a "service" was a singleton that returned itself, and that the factory and provider were singletons that returned objects(each one unique). That is, if one controller got a blue car, and the other a red car, they would be two separate cars.

I think though the confusion comes from factories being written in two different ways. In one way, you return an object. This makes it functionally equivalent to a "service". In the other way, you return a function which is an instantiable object so that anything using this factory can get new separate instances.

Is this correct? Of the two methods of writing a factory is any one of them an anti-pattern?

user2483724
  • 2,089
  • 5
  • 27
  • 43
  • possible duplicate of [Angular.js: service vs provider vs factory?](http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory) – hassassin Mar 19 '14 at 17:48
  • I really hope they clean this up in Angular2. It's overly complex with no real benefit that I can see. Internally all approaches call `factory` anyway. – Matt Greer Mar 19 '14 at 17:48
  • i dont find it complex at all, you have have factory which returns something and service which returns a constructor which is good for a classical approach.. i use factory like the revealing module pattern – David Chase Mar 19 '14 at 17:51
  • @MattGreer I think you mean that internally, all approaches call provider. I had to read this article five times, but I eventually got it [I think]: http://joelhooks.com/blog/2013/08/18/configuring-dependency-injection-in-angularjs/ Basically, factory uses the new keyword, whereas service does not. I haven't the foggiest idea what the implications of that are in practical development. – JeffryHouser Mar 19 '14 at 17:54
  • 1
    Yeah there are a bunch of threads that talk about the difference between angular services but none that specifically addresses how a factory can be used in different ways. The most common example just shows how a service/factory are the same, one uses "this", one returns an object. – user2483724 Mar 19 '14 at 17:54
  • @JeffryHouser Yes provider, thanks. I can appreciate the arguments for having all three. But I do believe if Angular had only given us `provider` most everyone would have been just fine and not even noticed something was missing. – Matt Greer Mar 19 '14 at 21:59

2 Answers2

1

From a blog I wrote specifically to answer this question:

So here it is. I’m going to summarize then follow up with some examples:

  • Use a service when you want to pass a constructor function. The function will be invoked with the “new” operator and the result stored.
  • Use a factory when you want to return the object from a function (i.e. factory pattern). The function will be called and the result stored.
  • Use a provider when you want to be able to provide module-wide configuration for your object before making it available.

Why have multiple ways? Primarily because some systems that sit on JavaScript like TypeScript and CoffeeScript generate code that dictates one approach over the other, so it's there for flexibility.

To see full post with examples: http://csharperimage.jeremylikness.com/2014/01/understanding-providers-services-and.html

Jeremy Likness
  • 7,531
  • 1
  • 23
  • 25
  • So using factory to return a function(not an object like normal) and thus being able to instantiate what it returns shouldn't be used and is a hack? – user2483724 Mar 19 '14 at 23:28
  • The names are bad names. Factory means use a factory to set up the service, and service means use the constructor. They both end up with singletons. If you want to create your own factory, you can still use either method. A service can have a function with a function on it that returns new instances, and a factory could return it's own function that returns new instances. – Jeremy Likness Mar 19 '14 at 23:31
  • Ah I see, a service can also help you instantiate new instances if necessary. The word factory connected too much with "creating new objects" to me. Thanks for clearing that up. (The typescript/coffeescript support explanation was good too!_ – user2483724 Mar 19 '14 at 23:39
  • Yes - here is a fiddle that demonstrates using service and factory in Angular for both services and factories. http://jsfiddle.net/jeremylikness/7dUyf/ – Jeremy Likness Mar 19 '14 at 23:41
0

What I have seen relations to is a factory is the equivalent of the revealing module design pattern in Javascript.

Module Design Pattern - Singelton:

var x = (function() {
    var y = 1,

    zMethod = function() {
       return y + y;
    }

    return {
       y: y,
       z: zMethod 
    }

}());

Angular Factory:

angular.module('app', [])
    .factory('x', function() {
        var y = 1,
        zMethod = function() { return y + y; }

        return {
            y: y,
            z: zMethod
        }
    });

Helps to clarify what is going on behind the scenes... But this is just one of the three parts!

Sten Muchow
  • 6,623
  • 4
  • 36
  • 46