↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Constructors or Static Factory Methods?

The page argues against Joshua Bloch’s preference for static factory methods over constructors. It says their advantages, such as names, caching, and subtype selection, are workarounds for a design that should instead use polymorphism, encapsulation, or separate objects.

Reading notes
#

  • A constructor is presented as the natural way to create an object in object-oriented software.
  • Named static factories are said to signal that the design should be split into more specific classes, such as HexColor and RGBColor.
  • Caching through a static factory is treated as a storage concern that belongs in a separate object like Palette.
  • Choosing a subtype from a static factory is described as taking decision-making away from the object itself.
  • The preferred alternative is to keep the relevant logic inside the class or move management concerns to dedicated objects, not to static methods.