GofPatterns GofPatterns


Singleton Pattern   «Prev  Next»

Singleton Pattern - Exercise Result

Using the Singleton Pattern


You entered:


What are the characteristics of the Panda Class?
The Panda class can be written by simply changing a few names in one of the two implementations in the lesson.

Java Code C++ Code

Here is the Java version:
public class Panda {
 static Panda thePanda = new Panda ();
 protected Panda () {
 }
 public static Panda getThePanda() {
  return thePanda;
 }
}

Here's the C++ version:
class Panda {
  public:
    static Panda* getThePanda(){
       if (thePanda == 0) {
           thePanda = new Panda;
        }
          return thePanda;
   };
  protected:
    Panda(){};
  private:
    static Panda* thePanda;
};
Panda* Panda::thePanda = 0;
Notice that the word "Singleton" no longer appears in the class.