About this chapter
This is your first chapter on named design patterns. It starts by explaining what a design pattern even is, then covers the creational patterns: different, well-tested answers to one question - how should your code create objects without becoming rigid and hard to change?
Lessons from courses
- 1 What are design patterns? Learn what software design patterns are, where the Gang of Four came from, and the three categories: creational, structural and behavioral.
- 2 Factory Method Learn the factory method pattern in PHP: move object creation behind a method so callers depend on an interface, not a specific new Concrete().
- 3 Abstract Factory Learn the abstract factory pattern: create whole families of related objects that belong together, and when the extra layer is worth it.
- 4 Builder Learn the builder pattern in PHP: assemble a complex object step by step with a readable fluent API instead of a huge, confusing constructor.
- 5 Singleton Learn the singleton pattern in PHP, then the honest reasons it's often an anti-pattern: global state, hard testing, and why DI usually beats it.
- 6 Prototype Learn the prototype pattern in PHP: copy an existing configured object with clone instead of rebuilding it from scratch, and handle __clone safely.