16.7. Creator

Assign class B the responsibility to create an instance of class A if one or more of the following is true:

  • B aggregates A objects.

  • B contains A objects.

  • B records instances of A objects.

  • B closely uses A objects.

  • B has the initializing data that will be passed to A when it is created (thus B is an Expert with respect to creating A).

Solution

B is a creator of A objects.

If more than one option applies, prefer a class B which aggregates or contains class A.

Who should be responsible for creating a new instance of some class?

Problem

The creation of objects is one of the most common activities in an object-oriented system. Consequently, it is useful to have a general principle for the assignment of creation responsibilities. Assigned ...

Get Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and the Unified Process, Second Edition 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.