Campbell Ritchie wrote:Most of the wrapper classes have their constructors deprecated, so the usage is no longer new Integer(123) but IntegervalueOf(123).
Most of the types introduced in the date time and Streams API have factory methods and private constructors, so the following usages would be impossible:- new Optional<String>("CodeRanch"), new IntStream(1, 2, 3), and new LocalDate(2021, 11, 7). In all cases the usages use the of() method:-- Optional<String>.of("CodeRanch"), IntStream.of(1, 2, 3), and LocalDate.of(2021, 11, 7). In all these cases the usages use the of() method, and I am not sure I have got the Optional example right.
someone quoted earlier wrote:Factory Method Pattern defines an interface to instantiate an object, but lets the sub classes decide what objects need to be instantiated. Factory method pattern lets the class defer to sub classes for object instantiation.
Ilja Preuss wrote:Actually not, no. A creation method typically is static, whereas a Factory Method has to be an instance method, because it needs to be called polymorphically. Typically the factory method is declared abstract in the class it is used in and defined in subclasses (it is a specialization of the Template Method pattern).
Ilja Preuss wrote:Certainly the term Factory Method is already defined by GoF, in a way that doesn't allow a Factory Method to be a simple static method. So it seems clear to me that if we mean a static method that creates things, we should use a different name for it...
David Hibbs wrote:No, just being a little bit antagonistic -- and point out that by pure GoF names, there is no name for a static creation method. Call it whatever you want, but I call it a factory (especially if you pass it a parameter to indicate what you want). Judging by the Apache code, I'm not the only one.
1) We agree (I think) that this is a pattern of some sort.
2) We agree (Again, I think) that this is a creational pattern.
3) Limiting scope to GoF terms, that means it must be one of Abstract Factory, Builder, Factory Method, Prototype, or Singleton.
4) Rule out Builder and Prototype.
This means it must be one of Abstract Factory, Factory Method, or Singleton.
It could be a Singleton, but there is no guarantee. Barring Singleton, a static creational method then must be a Factory of some sort. Perhaps we could call it a Simple Factory ?
w3sdesign wrote:Note that the Factory Method pattern can be implemented differently (abstract, concrete, or static factory method). See Implementation.
Jesse Silverman wrote: Arguably, the most important part of Design Patterns is facilitating communications between people.
Paul Clapham wrote:
(And yes, a lot of the Swing architecture is now out of fashion due to both advances in the language and broader knowledge of architectural principles. Things like implementing SwingConstants so you could write EAST instead of SwingConstants.EAST in your code -- nowadays you would have some Enums for those constants and you'd use "import static" to simplify using them.)
An increasingly popular definition of factory method is: a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass. Unlike a constructor, an existing object might be reused, instead of a new object created. Unlike a constructor, factory methods can have different and more descriptive names (e.g. Color.make_RGB_color(float red, float green, float blue) and Color.make_HSB_color(float hue, float saturation, float brightness)