WordPress Design Patterns: Singleton, Factory, Observer, and Repository

How to apply common software design patterns to WordPress plugin and theme development for cleaner, more maintainable code.

Design patterns are reusable solutions to common software development problems. They provide a shared vocabulary for discussing architecture, reduce code duplication, and make your codebase easier to understand and maintain. While WordPress doesn’t enforce any particular design pattern, applying proven patterns to your plugin or theme code improves its quality significantly.

This guide covers the most useful design patterns for WordPress development and how to implement them.

The Singleton Pattern

The Singleton pattern ensures a class has only one instance and provides a global access point to it. In WordPress development, singletons are commonly used for the main plugin class. Each plugin needs exactly one main instance that loads sub-components, registers hooks, and manages the plugin’s lifecycle.

Implement a singleton with a private constructor, a private static instance variable, and a public static get_instance() method. The constructor contains important initialization logic like defining constants, loading dependencies, and registering hooks.

The Factory Pattern

The Factory pattern creates objects without specifying the exact class to instantiate. This is useful when your plugin needs to create different types of objects based on context — different payment gateway handlers, different content types, or different export formats.

A factory class contains a create() method that accepts a type parameter and returns an instance of the appropriate class. Each specific class implements a common interface, ensuring consistent behavior across all types.

The Observer Pattern (Hooks)

The Observer pattern is already built into WordPress through its hooks system — actions and filters. WordPress plugins extend functionality by observing events (actions) and modifying data (filters) without modifying core code. This is the most fundamental design pattern in the WordPress ecosystem.

Think of add_action() and add_filter() as subscribing an observer to an event. WordPress fires events (do_action, apply_filters) at specific points, notifying all registered observers. This decoupled architecture allows hundreds of plugins to coexist without directly interacting.

The Repository Pattern

The Repository pattern separates data access logic from business logic. Instead of scattering WP_Query calls and get_post_meta() calls throughout your code, create a repository class that encapsulates all data access for a specific entity type.

A BookingRepository class might provide methods like find_by_id(), find_upcoming(), find_by_customer(), and save(). Internally, these methods use WordPress APIs to fetch and store data, but the calling code doesn’t need to know how data is stored. This makes your business logic cleaner and easier to test.

The Dependency Injection Pattern

Dependency Injection passes dependencies to a class rather than having the class create them internally. Instead of a class instantiating a database connection or API client directly, those dependencies are passed as constructor parameters or setter methods.

Dependency injection makes your code more testable because you can substitute mock dependencies during testing. It also makes dependencies explicit — looking at a class’s constructor tells you everything it needs to function.

The Bottom Line

Design patterns make your WordPress code more maintainable, testable, and understandable. Start with the Singleton pattern for your main plugin class and the Observer pattern (WordPress hooks) for extensibility. Add the Repository pattern for complex data access and the Factory pattern for creating different object types. These patterns have stood the test of time in software engineering and apply equally well to WordPress development.

Leave a Reply

Your email address will not be published. Required fields are marked *