跪拜 Guibai
← All articles
Android

Factory Patterns in Practice: Rebuilding an Android Cache from Simple to Abstract

By 雨白 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Design pattern choices carry real maintenance costs; this cache example shows exactly when each factory pattern pays off and when it becomes overkill.

Summary

The factory pattern moves the work of instantiating objects behind an abstraction, so callers depend on an interface instead of concrete classes. An Android storage cache makes the case concrete: the initial SpUtil wraps SharedPreferences and exposes a singleton, forcing every consumer to know that concrete type. Switching cache strategies means touching every call site. A simple factory adds an IStorage interface and a StorageFactory that returns either a SharedPreferences-backed SpStorage or an in-memory MemoryStorage; callers only need IStorage and a StorageType enum. The factory can reuse a singleton instance, as the MemoryStorage lazy singleton does, so it doesn't force a fresh object per call.

The simple factory violates the open-closed principle when a new storage type arrives: you edit the enum and the when branch. The factory method pattern abstracts the factory itself with an IStorageFactory interface, letting each product have its own factory and deferring creation to subclasses. Adding DiskStorage means adding DiskStorage and DiskStorageFactory with no changes to existing code, but factory classes multiply as products grow. The abstract factory handles product families: storage plus storage transaction, with a shared cache. It guarantees compatible products from the same factory, but extending an existing family requires modifying the factory interface and every concrete subclass. The selection guidance is pragmatic: start with a simple factory, move to factory method as product variety grows, reserve abstract factory for clearly matched product sets.

Takeaways
— The factory pattern separates object creation from object usage, letting callers depend on an interface rather than a concrete class.
— A simple factory violates the open-closed principle when adding a new product requires editing the enum and the when branch.
— The factory method defers creation to subclasses and allows adding new products without touching existing code, but factory classes proliferate as product types increase.
— The abstract factory guarantees that related products from the same factory are compatible, at the cost of modifying the factory interface and all subclasses when adding a new product to the family.
— Selection guidance: start with the simple factory, move to factory method when product variety and change frequency demand it, and reserve abstract factory for clearly defined product families.
Conclusions

The advice to prefer the simple factory until change pressure emerges runs against the common habit of applying the most elaborate pattern upfront.

Factories and singletons coexist: the factory returns a lazy singleton for memory storage, showing that creation management doesn't mean a new object every call.

The real gain of the abstract factory is consistency, not flexibility; the example shows how a transaction and a storage share the same cache only when created from the same factory.

Concepts & terms
Factory Pattern
A creational pattern that centralizes object instantiation in a factory, separating creation from usage so callers depend on abstractions.
Simple Factory
A simplified creation approach, not one of the original GoF patterns, where a single factory method returns different concrete classes based on a parameter. It's easy to use but violates the open-closed principle when adding new types.
Factory Method
A GoF pattern that abstracts the factory itself; subclasses implement the creation method, so each product has its own factory. It preserves the open-closed principle but can cause class explosion.
Abstract Factory
A GoF pattern for creating families of related products. A factory interface has methods for each product type, ensuring compatible combinations, but extending an existing family requires modifying the interface and all factories.
Open-Closed Principle
A design principle stating that software should be open for extension but closed for modification; adding new behavior should not require changing existing, working code.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗