Singleton

Intent

Ensure a class only has one instance, and provide a global point of access to it.

Class diagram of the original GoF pattern

structure-GoF:singleton

Another solution in MixJuice

Solved Problem(s)

Complexity problem
The original GoF pattern is complicated and error prone. (Although this problem is not mentioned in the GoF book, the demerits of static methods are mentioned at p.128, line.14-16 that implies static methods are preferable if these demerits do not matter.)

Used programming technique(s)

Method extension of static methods solve the problem of static methods, limitation of extensibility. (Note that the current version of MixJuice, version 1.0, does not support method extension of static methods; however the future version will support it.)

Consequences

Structure and pseudo code

structure:singleton_sample1

module original {
  define class C {
    define static void operation() {...}
    define static int data;
  }
}

module extension extends original {
  class C {
    static void operation() { ... original() ...}
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top