Questions tagged [static-factory]

Static factory methods are an alternative to public constructors in OO languages that support class-level methods. They were popularized by Joshua Bloch in his book, Effective Java. Static factory methods are *not* one of the Gang of Four [design-patterns]. The [abstract-factory] and [factory-method] patterns are entirely separate.

Joshua Bloch has promoted the idea of static factory methods since at least 2001 with the first edition of his Effective Java book. He writes,

The traditional way for a class to allow a client to obtain an instance is to provide a public constructor. There is another technique that should be a part of every programmer’s toolkit. A class can provide a public static factory method, which is simply a static method that returns an instance of the class.

Bloch offers several reasons for using static factory methods instead of constructors.

  1. One advantage of static factory methods is that, unlike constructors, they have names.
  2. 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.
  3. A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
  4. A fourth advantage of static factories is that the class of the returned object can vary from call to call as a function of the input parameters.
  5. A fifth advantage of static factories is that the class of the returned object need not exist when the class containing the method is written.

There are caveats to static factory methods, however.

  • The main limitation of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
  • A second shortcoming of static factory methods is that they are hard for programmers to find.

Note the Gang of Four published two factory , named and , which are entirely different from static factory methods. There are more factory patterns outside the GoF book, so the term "factory" by itself is ambiguous.

39 questions
43
votes
20 answers

Is this Factory Method creation pattern?

I have been using factory method creation pattern for awhile now. I was just recently told that this: public static class ScheduleTypeFactory { public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType) { …
39
votes
3 answers

Factory Pattern - CreateInstance static or not?

This is about the Factory Pattern. I am a little confused. I saw implementations where the createInstance() method is static and some implementations that are non-static. Some say it's depending on "style" or "taste" and some say it does not.…
dknaack
  • 60,192
  • 27
  • 155
  • 202
18
votes
3 answers

JDK 11 Generics Issue when using Set.of

I am unable to understand the below issue for type safety when using JDK 11. Can anyone explain the reason for not getting a compilation error when I am directly passing the Set.of in the argument: public static void main(String[] args) { var…
Alok Dubey
  • 419
  • 4
  • 13
16
votes
4 answers

Spring 3 @Component and static factory method

If I'm writing a static factory method to create objects, how do I use the '@Component' annotation for that factory class and indicate (with some annotation) the static factory method which should be called to create beans of that class? Following…
shrini1000
  • 7,038
  • 12
  • 59
  • 99
14
votes
5 answers

static factory method in interface class java

I was reading Effective java text book. First item is about using static factory methods instead of public constructor. My doubt is that if i am specifying an Interface how can i specify a static factory method in the Interface ? Because java does…
Manu Viswam
  • 1,606
  • 2
  • 18
  • 33
11
votes
3 answers

Static factory method vs public constructor

Background: Here's the code for what I'm currently working on. First, the base class, which is an account class that holds information about the account and has some methods that for the most part change the values of the class's properties. public…
Nick Bull
  • 9,518
  • 6
  • 36
  • 58
10
votes
1 answer

Can I configure myBatis to create an instance of a class using the Builder Pattern?

I have a (Java) class with many instance fields (many of which are optional). I would like all fields (thus class) to be immutable. So, I would like to use the Builder Pattern for constructing instances of the class. Can I configure myBatis to…
James
  • 2,876
  • 18
  • 72
  • 116
5
votes
5 answers

Use Enum or String for a static factory method?

Is it better to use enum or String to dispatch to the right object to create in a static factory method ? Example with String : public static Car createCar(String carName){ if(carName.equals("Ferrari")){ return new Ferrari(); } …
Mik378
  • 21,881
  • 15
  • 82
  • 180
5
votes
5 answers

Static factory disadvantage over constructor

I am going through Effective Java. The very first item makes a convincing case for static factory methods over constructors. I didn't get the first disadvantage which is The main disadvantage of providing only static factory methods is that …
Anand
  • 20,708
  • 48
  • 131
  • 198
4
votes
1 answer

Why does JavaFX use “static” constructors?

In JavaFX, there are a few classes, for example javafx.scene.text.Font and javafx.scene.paint.Color that have static methods that act like constructors. In the Font class, it is possible to create a new Font object with the statement new Font(name,…
HLR
  • 69
  • 4
4
votes
1 answer

What's the reason to instantiate Spring beans with factory methods?

I know there are constructor-based injection and setter-based injection in Spring. When should I use factory methods to inject beans?
Diamond
  • 217
  • 1
  • 3
  • 7
4
votes
3 answers

How to choose between methods of acquiring dependencies?

I've seen at least three ways of acquiring dependencies in a Java object without coupling the object to the creation of the dependency; Dependency Injection - some framework injects a required object into another object based on an external…
brabster
  • 42,504
  • 27
  • 146
  • 186
3
votes
3 answers

How to hide constructor on a Java record that offers a public static factory method?

I have this simple record in Java: public record DatePair( LocalDate start , LocalDate end , long days ) {} I want all three properties (start, end, & days) to be available publicly for reading, but I want the third property (days) to be…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3
votes
1 answer

Can I Create a Subclass Instance Utilising a Factory-like Static Method of the Superclass?

Problem: I want to extend Java's java.util.BitSet, using my own MyBitSet, just to add some functionality/transformation methods I often use. A method of BitSet I really find useful in my code is the "factory-like" public static method…
okariotis
  • 95
  • 1
  • 1
  • 6
3
votes
2 answers

What is the difference between a Singleton and static factory methods

I want to know if both singleton and static factory method create only one instance then why there are two concepts for same purpose? Note: Here term "static factory method" is taken from Effective java book by Joshua bloch where he has written: "A…
1
2 3