4.1. Problem and Solution

When an object is instantiated, it is technically a complete object. Some objects can be used in this capacity and are ready to propagate throughout the code flow. However, other objects are more complex in nature. They may require additional public methods to execute to be considered "complete" and available for the rest of the application.

It is important to understand what the word complex means in this context. Generally, complexity is the actual logic contained within methods of a class. However, when I refer to a complex object in connection to object instantiation, it means the level of steps that are required to create that complete object. The actual logical steps in each of the executed methods have no bearing on whether these classes are complex.

When creating complex objects, a common architecture decision revolves around the creation of the constructor. Some programmers think that any constructor should execute the proper logic to create the whole object. Others recognize that it makes sense to break up some of that logic into additional methods. Constructors designed in that fashion are basically a list of methods to call on instantiation. Neither of these solutions are very flexible. In fact, they are fundamentally wrong solutions.

It may be necessary to construct an object based on the results of a set of business logic. In this particular example, the base business decision rules have already been written and tested. Because of the results, ...

Get Professional PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.