O'Reilly logo

Professional PHP Design Patterns by Aaron Saray

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

14.1. Problem and Solution

As applications become more advanced, the core objects seem to follow suit. While it still may be a fully modular approach to creating these standard objects, the path to build a proper object has perhaps grown tenfold. Other times the code base is not as perfectly streamlined as the programmer would wish. Either way, some objects start to become expensive to create. The extra time needed to construct this object or the amount of memory it consumes is the expense that should try to be avoided. A good way to recognize this is that it seems like it might be simpler to create a new type of object that mimics the behaviors of a core object without all of the complexity. When it starts becoming obvious that the cost for creating these objects is becoming prohibitive, there are two ways to move forward. The first is refactoring. As alluded to earlier, there might become a point where refactoring is no longer practical or even possible. The other path to investigate involves modifying the objects so that they can be duplicated using the Prototype Design Pattern.

The Prototype Design Pattern maps out a way to reduce the expense of creating complicated objects. It does this not by removing the intricacies of the object itself but by creating an interface to preserve those properties through duplication. The Prototype Design Pattern is useful, especially when complex objects will not change their state throughout the code stream. That is to say, the initial state ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required