1

I am a beginner in Java and the book that I use to learn it seems to have cryptic examples and sentences that completely confuse me.

I understand what interfaces are and how/where to apply the concept in real world. But what are Factory Methods? The term "factory method" is ambiguous (JavaScript has a different meaning for that) so I am providing the snippet that the book has, in order to make my question clear. Here is the code:

interface Service {
    void method1();
    void method2();
}

interface ServiceFactory {
    Service getService();
}

The Service interface is just a normal interface. ServiceFactory interface looks like a normal interface but it is a "Factory Method". What's that? What does it solve and why I should use them?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Fresher
  • 333
  • 1
  • 3
  • 13
  • Strictly, `ServiceFactory` is a "normal" interface too. A Factory Method needs some kind of implementation (starting with Java 8, this can be a default method in an interface) since it needs to produce (create) an instance of some type (hence the term "factory"). You *can* specify the factory method in an interface, but still you need a concrete Factory class which creates specific types. See https://en.wikipedia.org/wiki/Factory_method_pattern – Andreas Fester Aug 03 '16 at 05:16
  • Possible duplicate of [Design Patterns: Factory vs Factory method vs Abstract Factory](http://stackoverflow.com/questions/13029261/design-patterns-factory-vs-factory-method-vs-abstract-factory) – jaco0646 Aug 03 '16 at 21:44

2 Answers2

1

A factory method is simply a method that encaspulate the creation of an object. Instead of using the new operator as you normally would for creating an instead of a Service, in your example, you're using the factory method on some object.

ServiceFactory sf = new ServiceFactoryImpl();
// factory method
Service s = sf.getService();

To better illustrate the role of the method, it could be called createService instead. Now the method encapsulates the details of the creation of a Service, you can provide many flavors of the methods (by overloading), you can have it return different subclasses depending on the context, or parameters passed to the factory method.

Arthur Noseda
  • 2,534
  • 19
  • 28
0

A "factory method" is a method that constructs an object.

More specifically, the term usually refers to a static method that returns an instance of its declaring class (either a direct instance, or an instance of a subclass). In my experience, there are a few cases where that's particularly commonly done:

  • If you need to have multiple different constructors, using factory methods lets you give them appropriate names (whereas calls to different constructors can only be distinguished by the argument-types, which aren't always obvious at a glance).
  • If you need to have a few different implementations of a single abstract class that serves as the entry-point, the factory method can instantiate the appropriate subtype.
  • If you need any sort of caching logic or shared instances, the factory method can handle that, returning an already-existing instance if appropriate.
  • If you need to do some work with side effects, a factory method can be named in such a way that it's more clear what those side effects are.

So, your example doesn't really involve a "factory method" IMHO, but you can cheat a bit and describe getService() as one. (Rather, I would just describe ServiceFactory as a "factory" at leave it at that.) The benefits of ServiceFactory.getService() are the same as those of a factory method, plus the usual benefits of having an instance instead of a static method.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 1
    It should always be noted that the Static Factory Method is NOT the same as the GoF Factory Method Pattern. The two are completely different. – jaco0646 Aug 03 '16 at 21:09
  • @jaco0646: Ah, you are right. The OP's example does not seem to be an example of either one, though. – ruakh Aug 03 '16 at 21:19
  • 1
    Agreed; the term _factory_ is often used very loosely to describe any creational method. This loose terminology is then conflated with GoF or other terminology, and confusion ensues. – jaco0646 Aug 03 '16 at 21:33