Lesson 15 โข Advanced
Advanced OOP Patterns ๐๏ธ
Master the Factory, Singleton, Strategy, and Repository patterns โ the building blocks of professional PHP architecture.
What You'll Learn in This Lesson
- โข Singleton: guarantee one instance of a class across your app
- โข Factory: create objects without hardcoding which class to instantiate
- โข Strategy: swap algorithms at runtime without changing client code
- โข Repository: abstract data access behind a clean interface
- โข When to use (and when NOT to use) each pattern
Singleton & Factory Patterns
The Singleton ensures only one instance of a class exists โ perfect for database connections and configuration managers. The Factory pattern lets you create objects without specifying their exact class, making your code flexible and extensible. Both are "creational" patterns from the Gang of Four (GoF) book.
Try It: Singleton & Factory
See how Singleton shares state and Factory creates different notification types
// PHP Design Patterns: Singleton & Factory
console.log("=== SINGLETON PATTERN ===");
console.log();
console.log("Purpose: Ensure a class has only ONE instance globally");
console.log("Use case: Database connections, config managers, loggers");
console.log();
// Simulate Singleton in JS
class Database {
static instance = null;
constructor() {
if (Database.instance) return Database.instance;
this.connection = "MySQL connection established";
this.queryCount = 0;
Database.insta
...Strategy & Repository Patterns
The Strategy pattern encapsulates algorithms so you can swap them at runtime โ ideal for pricing rules, sorting methods, or validation strategies. The Repository pattern abstracts data access behind an interface, so your business logic doesn't care whether data comes from MySQL, an API, or a file. Both patterns make your code testable and maintainable.
Try It: Strategy & Repository
Apply different pricing strategies and use a Repository to manage users
// PHP Design Patterns: Strategy & Repository
console.log("=== STRATEGY PATTERN ===");
console.log();
console.log("Purpose: Define a family of algorithms and make them interchangeable");
console.log("Use case: Sorting, pricing rules, discount calculations");
console.log();
// Different pricing strategies
const strategies = {
regular: (price) => price,
member: (price) => price * 0.9,
vip: (price) => price * 0.8,
bulk: (price, qty) => qty >= 10 ? price * 0.7 : price,
};
let items = [
{
...โ ๏ธ Common Mistakes
๐ Quick Reference โ OOP Patterns
| Pattern | Type | When to Use |
|---|---|---|
| Singleton | Creational | One shared instance (DB, config) |
| Factory | Creational | Create objects by type string |
| Strategy | Behavioral | Swap algorithms at runtime |
| Repository | Structural | Abstract data access layer |
๐ Lesson Complete!
You now know four essential design patterns! Next, learn about Dependency Injection Containers โ the key to building testable, decoupled PHP applications.
Sign up for free to track which lessons you've completed and get learning reminders.