Separate Use from Creation

In object-oriented programming, there’s the creation of the object—we call it instantiation—and then there’s the usage of the object. It makes a lot of sense to separate out those two phases so that one group of objects are only dealing with creating objects and another group of objects are only dealing with using objects. The objects that create objects are called factories. Factories simply encapsulate the new keyword. The new keyword is the way we create runnable objects from their class definitions. new has a requirement, which is that it has to know what is newing up.

Suppose I said to you, “I would like you to do something for me.”

And you reply, “Fine, what?”

Then I say, “Something. Now do it.”

You’re probably ...

Get Beyond Legacy Code 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.