Creator

Problem definition

Creation of objects is one of the most common activities in an object-oriented system. It is also one of the first problems developers have to face. Which class is responsible for creating objects is a fundamental property of the relationship between objects of particular classes. Simply creator pattern is responsible for creating an object of class. In general, a class B should be responsible for creating instances of class A if one, or preferably more, of the following apply:

  • Instances of B contains or compositely aggregates instances of A
  • Instances of B record instances of A
  • Instances of B closely use instances of A
  • Instances of B have the initializing information for instances of A

Analysis

The Creator pattern helps in deciding where to delegate responsibility for creating new objects. The basic idea of this pattern is to find such a creator which is already related with created objects – thanks to that the number of connections is decreasing. The object which contains, aggregate or contains an informations about created objects is good candidate for being a creator.  Sometimes the creator is an object containing informations used during an initialization of new instance of created class – in such situation Information Expert pattern is also used.

Contraindication

Sometimes the creation of objects is more complicated – for example in order to increase efficiency program reuse not needed instances, or in runtime program choose which concrete class use. In such  situation responsibility of creating new objects should be delegated to the other class ( for example Factory pattern by GoF).

Advantages

Decreasing number of connections makes that code is easier to maintain and is more reusable. There are fewer connections since as creator we use class which is already linked to the created object.

Connected Patterns

Follow

Get every new post delivered to your Inbox.