4

I know this has been asked so many times (Actually, a lot!), but I still can't manage to fully understand what is the main difference between these two.

I am aware a service returns a singleton instance of the function provided, and that a factory will simply invoke this function and return its value.

But...

As I see it, you can do and achieve the same effects using one or another, so how should I choose which one to use? Why should I choose services over factories, or the other way around?

Is there a situation where one can do something that the other cannot?

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • I am aware of the differences stated there, what I am asking however is not the *how*, but the *why*. Why should I chose one over the other?. Edited the question's title to evade possible confussions – Matias Cicero Oct 30 '14 at 13:42
  • all services, provides and factories are singletons, you may understand the basic of each and then decide what's the best fit for your solution, theres no one size fits all here – pedrommuller Oct 30 '14 at 13:44
  • The problem is that I understand the basic differences, but I cannot see one possible situation when one can do something that the other cannot. With that in mind, I can always stick to one approach and never use the other one. That's not what I intend though. – Matias Cicero Oct 30 '14 at 13:46
  • the main difference reside on each object has its own level of configuration prior to send the instance, the answer on the question I referred explain it very clearly – pedrommuller Oct 30 '14 at 13:50
  • Services and factories have the same level of configuration. Providers, however, have a more advanced one. That's why Providers are out of my question, since that's one thing the other approaches don't have or cannot accomplish. – Matias Cicero Oct 30 '14 at 13:53
  • Misko explains it all: https://groups.google.com/forum/#!msg/angular/56sdORWEoqg/b8hdPskxZXsJ – mccainz Oct 30 '14 at 14:01

1 Answers1

3

In Short

If you want your function to be called like a normal function, use factory. If you want your function to be instantiated with the new operator, use service. If you don't know the difference, use factory

Source :

URL : http://iffycan.blogspot.in/2013/05/angular-service-or-factory.html

Detailed Explanation

When you use service , the variables and functions you declare will be prototyped to new object and object will be returned to you. Where in factory , it will return the function simply as it is.

Lets say you created a service with 10 functions , when you include it as dependency, new object will be created and all function will be prototyped , check below example

function Gandalf() {
    this.color = 'grey';
}

Gandalf.prototype.comeBack = function() {
    this.color = 'white';
}

"this" will be returned. Performance issues are negligible here.

Senthil
  • 946
  • 1
  • 14
  • 34
  • There must be something I am missing here, but I still can't understand why would I chose to use service or factory. The factory's function can return an object too: `function() { var color = 'grey'; return { comeBack: function() { color = 'white'; } }; }` – Matias Cicero Oct 30 '14 at 13:40
  • 1
    He chose a service because he is providing it a prototype, a newable object. In TypeScript, CoffeeScript, or ES6 this would be a class. It is an object that instantiated with the new keyword. – Martin Oct 30 '14 at 14:09