Fundamentals 7 min read

Mastering Java’s Simple Factory Pattern: Definition, Structure, and Pros & Cons

This article explains Java's Simple Factory design pattern, covering its definition, class structure, implementation steps with code examples, advantages and disadvantages, and suitable application scenarios, helping developers understand when and how to use this creational pattern effectively.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mastering Java’s Simple Factory Pattern: Definition, Structure, and Pros & Cons
Simple Factory Pattern (Factory Pattern) is one of the most commonly used design patterns in Java, belonging to creational patterns. This article analyzes a concrete example step by step to understand its application scenarios and pros/cons.

Definition of Simple Factory Pattern

Simple Factory, also called "static factory method pattern", is a class‑creation pattern. Other creational patterns include Factory Method, Abstract Factory, Singleton, Builder.

Using a real‑life analogy: a factory produces products; similarly, the simple factory pattern has a "factory class" that creates objects.

Structure of Simple Factory Pattern

1. Factory Class

The factory role implements the internal logic for creating all instances.

2. Abstract Product

The abstract product role is the parent class of all created objects, defining the common interface.

3. Concrete Product

The concrete product role represents the actual product; each created object is an instance of a concrete subclass.

Implementation of Simple Factory Pattern

Main steps (three):

1. Create Abstract Product Class

Purpose: define the common interface for concrete products.

package com.mikechen.java.design.factory.simple;

/**
 * Abstract product
 */
public abstract class Product {
    public abstract void Create();
}

2. Create Concrete Product Class

Purpose: inherit abstract product and define the specific product.

package com.mikechen.java.design.factory.simple;

/**
 * Concrete product
 */
public class ConcreteProduct extends Product {
    @Override
    public void Create() {
        System.out.println("生产具体的产品");
    }
}

3. Create Factory Class

Purpose: provide a static method to create instances of concrete products based on parameters.

package com.mikechen.java.design.factory.simple;

/**
 * Factory
 */
public class Factory {
    public static Product newInstance() {
        return new ConcreteProduct();
    }
}

This example is simplified; in practice the factory can decide which concrete product to instantiate based on input parameters.

Advantages and Disadvantages

Advantages

Separates object creation from usage, achieving decoupling.

Encapsulates initialization in the factory, making code easier to maintain.

Disadvantages

The factory class centralizes all creation logic; if it fails, the whole system is affected.

Violates the Open‑Closed Principle; adding new products requires modifying the factory, increasing complexity.

Static factory methods cannot be inherited or overridden, preventing hierarchical extension.

Application Scenarios

1. When the client only knows the parameters to pass to the factory and does not care about the creation logic.

2. When the client does not need to know creation details or even the class name, only the type corresponding to the parameter.

3. When the number of concrete products is small, keeping the factory logic simple.

Summary

1. Simple Factory Pattern involves three roles: factory class, abstract product, and concrete product.

Factory class implements the internal logic to create all instances.

Abstract product defines the common interface for all instances.

Concrete product is the actual class instantiated.

2. Its biggest advantage is separating object creation from usage, but its main drawback is inflexibility; adding new products requires modifying the factory, and with many products the factory code becomes complex.

3. Suitable when the factory creates few objects and the client only knows the parameters, not the creation details.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSoftware Architecturedesign patternCreationalSimple Factory
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.