Pétur Ingi Egilsson
Pétur Ingi Egilsson

Reputation: 4503

Why use a factory instead of 'new'?

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.

Upvotes: 4

Views: 2375

Answers (5)

Ahmad Ismail
Ahmad Ismail

Reputation: 13942

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.

Upvotes: -1

Alex Frenkel
Alex Frenkel

Reputation: 409

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.

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136102

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)

Upvotes: 7

xsilmarx
xsilmarx

Reputation: 749

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.

Upvotes: 0

novy1234
novy1234

Reputation: 332

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

Upvotes: -1

Related Questions