Context:

See Reference Counting idiom.

Challenges:

  • To encapsulate the deletion timing mechanism.
  • To separate the association and its management code from both Client and Resource.
  • (In strong-typed languages that support genericity:) To separate association management code from Resource type.
  • (See also Reference Counting Idiom.)

Skill:

Participants:

Smart Pointer:

Contained by the Client. Represents pointer to Resource (by overloading the dereferencing operator).

Resource:

The object referenced (type insignificant). Its sole responsibility here is to allow deletion.

Smart-Pointer Counter:

Referenced by Smart Pointer. Manages the reference count. Responsible for actually deleting the Resource.

Signature:

Smart Pointers share Smart Pointer Counter, which contains the Resource. Smart Pointer and Smart Pointer Counter are parameterized on Resource. (Smart Pointer Counter's declaration may nest in Smart Pointer). Nothing is required of the Resource (besides enabling deletion). Client contains Smart Pointer (rather than Resource).

Used patterns and idioms:

Reference Counting idiom.

Scope:

Languages that do not support automatic garbage collection and support value addressing.