java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Refactor to Factory Enum Pattern for Cleaner Object Instantiation

Open huunghia98er opened this issue 6 months ago • 4 comments

Introduction

In Java applications, object instantiation for different types is often handled through separate factory classes. While this approach works, it can become verbose and hard to maintain — especially when the set of object types is known and fixed.

To improve clarity and maintainability, we propose adopting a cleaner pattern that combines factory logic and type mapping using enums.

Current Problem

  • Object creation logic is scattered and not centralized.
  • Requires additional boilerplate factory classes.
  • No guarantee of singleton or controlled instance creation.
  • Becomes harder to maintain as more types are added.
  • Type safety is not enforced at compile-time in traditional factory implementations.

Proposed Solution

Adopt the Factory Enum Pattern — a temporary term to describe a technique that combines the Factory Pattern and Enum Singleton pattern.

In this approach:

  • Each enum constant represents a known object type.
  • Each constant is associated with a corresponding singleton instance.
  • Object creation is centralized and type-safe.
  • Lazy-loaded singletons can be used via InstanceHolder.

Benefits:

  • Cleaner and more readable instantiation logic.
  • Eliminates the need for external factory classes.
  • Guarantees singleton per type.
  • Compile-time safety using enums.
  • Naturally thread-safe due to enum and static initialization behavior.

Implementation Plan

  • Define an enum containing constants for each object type.
  • Each constant holds a reference to its singleton instance.
  • Use a private static InstanceHolder class if lazy-loading is desired.
  • Update client code to retrieve instances via the enum rather than constructors or factory methods.

Related Pull Request

This issue is related to PR: #3300 Apologies for submitting the pull request before filing this issue. This aims to provide additional context and rationale.

huunghia98er avatar Jul 08 '25 05:07 huunghia98er

This issue is stale because it has been open 60 days with no activity.

github-actions[bot] avatar Sep 07 '25 02:09 github-actions[bot]

assign pls assign this to me Hi, I’d like to contribute to this issue by refactoring one or more factory-based patterns using the proposed Factory Enum Pattern. I’ll follow the same structure as in PR #3300 and ensure backward compatibility. Please confirm if this approach is okay before I start working. 🙌

Prasoon31Pandey avatar Oct 05 '25 12:10 Prasoon31Pandey

Is this issue resolved ?

RitikaMathur19 avatar Oct 15 '25 03:10 RitikaMathur19

yes

On Wed, Oct 15, 2025 at 8:43 AM RitikaMathur19 @.***> wrote:

RitikaMathur19 left a comment (iluwatar/java-design-patterns#3301) https://github.com/iluwatar/java-design-patterns/issues/3301#issuecomment-3404349867

Is this issue resolved ?

— Reply to this email directly, view it on GitHub https://github.com/iluwatar/java-design-patterns/issues/3301#issuecomment-3404349867, or unsubscribe https://github.com/notifications/unsubscribe-auth/BBBCNYKZRL2S5SV32HVY7HL3XW3VPAVCNFSM6AAAAACBAA4AF2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTIMBUGM2DSOBWG4 . You are receiving this because you commented.Message ID: @.***>

Prasoon31Pandey avatar Oct 15 '25 03:10 Prasoon31Pandey