4

Factory pattern is supposed to be used when using new operator for creating object is considered harmful. In what terms does new operator is considered harmful

4 Answers 4

8

new is not considered harmful.

If you want to create a new instance of a class, you need to use new somewhere. Whether you wrap the use of new in a factory is a design/architecture question.

What you are probably referring to is that "newing up" (create an instance by using new SomeClass(..)) instances all over the place, is usually considered bad design / bad practice. The reason for this, is that it can be harder to change the implementation in the future because all your classes are tightly coupled. A very common argument/example used is testing. If you create new instances directly in the code, it can be harder to test that code in isolation and/or use mocks of certain classes.

I recommend that you read about the arguments for (and against) using Dependency Injection.

Sometimes you cannot rely on a single instance injected into your class. Sometimes you need to be able to create a new instance (or multiple) on demand. In those cases, if you want to avoid using new directly, it makes sense to look into the various factory patterns as a means to extract the responsibility of creating a new instance.

Whether you follow such practices is entirely up to you and/or your team.

2

Using of new operator directly is hardcoding.

It also means that class is not following single responsibility principle.

Factories along with interfaces allow long term flexibility:

  • It allows decoupling and therefore more testable design.
  • It allows you to introduce an Inversion of Control (IoC) container easily.
  • It makes your code more testable as you can mock interfaces.
  • It gives you a lot more flexibility when it comes time to change the application (i.e. you can create new implementations without changing the dependent code).

Hope this helps.

2

The main reason you use factory pattern is that you don't need a lot of constructors for a object;

public Person(int age, String firstName, String lastName);
public Person(int age, String firstName, String middleName, String lastName);
public Person(int age, String firstName, String lastName, String socialNumber);
....

With factory pattern you can create a lot of different objects that are initialised with different parameters without writing so many constructor methods.

new object() is usually used in order to create an instance of the object to use it's non-static methods. I read about some issues using new operator and how it's allocating heap memory(you might not want that). Ignoring that there is no issue using new.

2

A framework needs to standardize the architectural model for a range of applications, but allow for individual applications to define their own domain objects and provide for their instantiation.

Usually, object creation in Java or some languages takes place like so:

SomeClass someClassObject = new SomeClass();

The problem with the above approach is that the code using the SomeClass’s object, suddenly now becomes dependent on the concrete implementation of SomeClass. There’s nothing wrong with using new to create objects but it comes with the baggage of tightly coupling our code to the concrete implementation class, which can occasionally be problematic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.