Reputation: 653
The motivation for using Static Factory methods staes as:
The most obvious motivation for Replace Constructor with Factory Method comes with replacing a type code with subclassing.
You have an object that often is created with a type code but now needs subclasses. The exact subclass is based on the type code.
However, constructors can only return an instance of the object that is asked for. So you need to replace the constructor with a factory method.
Can anyone please explain this with code? And what does this type code mean?
Upvotes: 0
Views: 136
Reputation: 719279
A second advantage that a factory method has over a bare constructor is that it can return an existing object. The Integer.valueOf(int)
method makes good use of this. By contrast, new
always creates a new object.
Finally, if we broaden the discussion slightly, a non-static factory method (e.g. in the form of a factory object) allow you use polymorphism to implement different object creation strategies.
And what does this type code mean?
You need to read this in the context of the page that you got this from. What the page is talking about is a single class that represents different "types" of thing. (In the example given, it is different types of employee represented by one Employee
class.) The "type code" is simply the attribute of the class that discriminates the different types.
Upvotes: 2
Reputation: 66313
Imagine we have a Person
class with a field called type
to indicate if that person is a teacher or a student. This will be the type code.
Now imagine we replace this type field with an object hierarchy: Person
class with 2 subclasses Teacher
and Student
.
If I use the constructors then I can only create a specific type of person i.e. new Teacher()
or new Student()
but if I want a method that creates a different kind of person based on some logic then I can do:
public static Person newPerson() {
// can return either a Teacher or a Student
}
Upvotes: 1
Reputation: 533710
A constructor can only return one implementation, however a static factory method can return any number of implementations, often a sub-class of the class referred to. Static factory methods also have the advantage of being named so you can have different behaviour for the same arguments.
e.g. These two methods return a sub-class of EnumSet (based on the number of elements in the enum) with different values based on the name (even though the arguements are the same)
EnumSet<MemoryType> memoryTypes = EnumSet.noneOf(MemoryType.class);
EnumSet<MemoryType> memoryTypes2 = EnumSet.allOf(MemoryType.class);
From the source
public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
Enum[] universe = getUniverse(elementType);
if (universe == null)
throw new ClassCastException(elementType + " not an enum");
if (universe.length <= 64)
return new RegularEnumSet<>(elementType, universe);
else
return new JumboEnumSet<>(elementType, universe);
}
You can see it has one implementation for smaller enum sets and another for larger ones.
Upvotes: 1