Why use a factory instead of 'new'?
Asked Answered
A

5

4

I am reading the book EMF: Eclipse Modeling Framework where its stated:

The EMF programming model strongly encourages, but doesn’t require, the use of factories for creating objects. Instead of simply using the new operator to create [an object]...

Why is the use of factories encouraged over new?

Your answer does not have to be EMF specific, as long as it has to do with Java.

Abbotson answered 26/3, 2015 at 9:44 Comment(0)
A
7

You can read Effective Java Item 1: Consider static factory methods instead of constructors. It describes advantages of using factory methods in detail:

  • One advantage of static factory methods is that, unlike constructors, they have names

  • A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked.

  • A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.

  • A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances (seems to be outdated since Java 7)

Adaline answered 26/3, 2015 at 9:51 Comment(5)
A better answer would summarize the stuff here, instead of referring to a (very good) bookSilvana
I agree with all that. The questioner asked about 'factories' which I read as both static factory methods and factory objects. A factory object is an object with the purpose of serving up instances. They have lots of additional power in terms of 'injecting' settings and polymorphism into other objects.Caseinogen
2nd advantage: "exception thrown in the constructor" lurks in the background there.Undress
(Offtopic?) Interesting book. Reading it now. Have read clean code and code complete in the past. Can you recomment other similiar books?Vishnu
google.com.ua/url?url=https://google-collections.googlecode.com/…Adaline
B
0

The answere here is not specific to Java too.

  1. Factory methods have names, it's easier to remember, and less error-prone.
  2. They do not require a new instance to be created each time they are called, you can use preconstructed classes and cache here.
  3. They can return an object of any subtype not only the one called in new
  4. You can parameterize calling "new" object.
Bargeboard answered 26/3, 2015 at 9:55 Comment(0)
E
0

I agree with mostly every answers given here, but those arguments apply generally to every situation in Java, however in this particular case of EMF there is another additional reason: EMF has its own introspection mechanisms, which are used, for instance, for the serialization and deserialization, which doesn't rely in the Java reflection.

For the deserialization, for instance, it reads the XML file, and instantiate the Java objects using the Ecore model information and the respective Factories. Otherwise it would need to use Java reflection.

Emulsify answered 27/3, 2015 at 11:6 Comment(0)
R
-1

Mainly it is simplicity of creating objects. It's a lot easier to call method from factory than to remember what each parameter in constructor means + it makes changes in code easier

Robbyrobbyn answered 26/3, 2015 at 9:54 Comment(1)
Well, a factory method would have parameters as well, if necessary. You don't write either kind of call without referring to javadoc if you are in doubt.Undress
P
-1

The answer to this question is explained in Elegant Objects by Yegor Bugayenko, Chapter 1, Under "1.1 Never use -er names" section.

What the author says is:

A class is a factory of objects.

A class makes objects, though we usually phrase that by saying a class instantiates them:

class Cash {
    public Cash(int dollars) {
    //...
    }
}
Cash five = new Cash(5);

This is different from what we call a Factory Pattern, but only because this "new" operator in Java is not as powerful as it could be. The only thing you can use it for is to make an instance—an object. If we ask class Cash to make a new object, we get a new object. There is no check for whether similar Objects already exist and can be reused, there are no parameters that would modify the behavior of new. etc.

"new" operator is a primitive control for a factory of objects.

Factory Pattern is a more powerful alternative to operator new, but conceptually they are the same. A class is a factory of objects. A class makes objects, keeps track of them, destroys them when necessary, etc.

A Factory Pattern, in Java, works like an extension to the new operator. It makes it more flexible and powerful, by adding an extra logic in front of it.

For example:

class Shapes {
    public Shape make(String name) {
        if (name.equals("circle")) {
            return new Circle();
        }
        if (name.equals("rectangle")) {
            return new Rectangle() ;
        }
        throw new IllegalArgumentException("not found");
    }
}

This is a typical factory in Java that helps us instantiate objects, using textual names of their types. In the end, we still use the new operator. My point is that conceptually, there is not much difference between Factory Pattern and new operator. In a perfect OOP language this functionality would be available in the new operator.

Prandial answered 1/9, 2022 at 6:19 Comment(1)
Yegor has many ideas that are based on a poor understanding of programming. Most of it is just common knowledge, much of the the rest is confused and misguided dogma. Take what he says with several grains of salt!Oday

© 2022 - 2024 — McMap. All rights reserved.