The Whens and Whys for PHP Design Patterns

Discussion about when and why, rather than the which and how of PHP patterns

There are plenty of articles that explain what design patterns are, and how to implement them; the web doesn’t need yet another one of those articles! Instead, in this article, Patkos Csaba will discuss the when and why, rather than the which and how. He will present different situations and use-cases for patterns, and will also provide short definitions to help those of you who are not so familiar with these specific patterns.


  • Creating Objects
  • Finding the Data You Need
  • Reusability
  • Discover-ability
  • Singularity
  • Controlling Different Objects
  • The Composite Pattern
  • The State Pattern
  • Decorate with the Decorator Pattern

Use a Factory Pattern
The factory pattern was invented to help programmers organize the information related to object creation. Objects sometimes have lot of constructor parameters; other times, they must be populated with default information immediately after their creation. These objects should be created in factories, keeping all the information regarding their creation and initialization contained within a single place.

When: Use a Factory Pattern when you find yourself writing code to gather information necessary to create objects.

Why: Factories help to contain the logic of object creation in a single place. They can also break dependencies to facilitate loose coupling and dependency injection to allow better testing.

Comments

Be the first to write a comment

You must me logged in to write a comment.