Factory Pattern, SOLID and YAGNI — Real Case: SavePages.jsx

PUBLISHED: 2026-07-18
AUTHOR: MANUEL PRIETO
Design Patterns

The Starting Problem

In the admin there was this file:

Six components with identical structure and only three differences between them:

  1. The label of the title ('Event', 'Hero Slide', 'Partner'…)
  2. The form to render (EventForm, HeroSlideForm…)
  3. The prop name that receives the id (eventId, slideId, partnerId…)

The Factory Pattern

What it is

A factory is a function whose only job is to create other things.

In JavaScript, when a factory creates React components, it is also known as a Higher-Order Component (HOC): a function that returns a component.

How it works

The factory receives the configuration that changes and returns a component with the logic that is always the same:

The result

What it solves: the DRY principle

DRY = Don't Repeat Yourself

Before: changing page design → touching 6 files.
After: changing page design → touching 1 function.

Adding a new page goes from copying and pasting to writing one line:

SOLID Principles Applied

SOLID is a set of 5 principles for writing maintainable code. In this refactoring, two apply primarily.

S — Single Responsibility Principle

Every module should have only one reason to change.

The file SavePages.jsx now has two responsibilities living together:

ResponsibilityReason to change
createSavePage (the factory)If the design or structure of the pages changes
The exports (EventSavePage, etc.)If a resource is added or removed

Should they be separated? Only if both reasons to change occur independently and frequently. See the YAGNI section below.

O — Open/Closed Principle

Code should be open for extension, but closed for modification.

Before, adding a new page meant modifying the existing code (copying one of the components and editing).

Now, adding a new page means extending without touching what already works: a new line calling createSavePage. Existing code remains untouched.

YAGNI — You Aren't Gonna Need It

Always implement things when you actually need them, never when you just foresee that you need them.

YAGNI is an XP (Extreme Programming) principle that combats over-engineering.

What over-engineering is

Adding complexity, abstractions, or structure just in case in the future, when there is no evidence that that future will arrive.

Applied to this case

After the refactoring, the question arises:

"Should I extract createSavePage to its own file utils/createSavePage.jsx?"

The SOLID + YAGNI answer:

Moving it without a real need would add:

  • One more file to manage
  • An additional import in SavePages.jsx
  • More indirection with no tangible benefit

Separate when you have two real reasons to change, not when you might have them.

When to actually extract it

Extracting createSavePage to its own module would make sense if:

  • Another file needs to import it
  • It grows and contains its own complex logic
  • You want to write specific unit tests for the factory

Visual Summary

ConceptWhat it saysHow it applies here
FactoryCreates components from configcreateSavePage({ label, Form, idProp })
DRYDon't repeat codeOne change affects all 6 components
SRPOne responsibility per moduleFactory + exports in the same file because they change together
OCPOpen to extension, closed to modificationNew page = new line, without touching the factory
YAGNIDon't build what you don't needNot extracting to utils/ without a real need
Over-engineeringComplexity without actual benefitCreating utils/ folder for an 18-line function used in one place