单例设计模式

Java code:

public class Singleton {
    private static Singleton single = null;
    
    private Singleton() {
        System.out.println("create a singleton class.");
    }
    
    public synchronized static Singleton getInstance() {
        if(null == single) {
            single = new Singleton();
        }
        return single;
    }
    
    public void dosth() {
        System.out.println("do somethings.");
    }

}

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Singleton single = Singleton.getInstance();
        single.dosth();
    }

 

Code in C#:

class Program
    {
        static void Main(string[] args)
        {
            Singleton single = Singleton.getInstance();
            single.dosth();
            Console.Read();
        }
    }

    class Singleton
    {
        private static Singleton single;

        private Singleton()
        {
            Console.Out.WriteLine("create a Singleton class instance.");
        }

        public static Singleton getInstance()
        {
            if (null == single)
            {
                single = new Singleton();
            }
            return single;
        }

        public void dosth()
        {
            //do some things
            Console.Out.WriteLine("do some things.");
        }
    }

 

Code in c++

Before

  A global variable is default initialized – when it is declared – but it is not initialized in earnest until its first use. This requires that the initialization code be replicated throughout the application.

class GlobalClass
{
    int m_value;

public:
    GlobalClass(int v = 0): m_value(v)
    {

    }

    int get_value()
    {
        return m_value;
    }

    void set_value(int v)
    {
        m_value = v;
    }
};


// Default initialization
GlobalClass *global_ptr = 0;

void foo(void)
{
    if(!global_ptr)
    {
        global_ptr = new GlobalClass();
    }
    global_ptr->set_value(1);
    cout << "bar: global_ptr is " << global_ptr->get_value() << '\n';
}
void bar(void)
{
  if (!global_ptr)
    global_ptr = new GlobalClass;
  global_ptr->set_value(2);
  cout << "bar: global_ptr is " << global_ptr->get_value() << '\n';
}

int _tmain(int argc, _TCHAR* argv[])
{
    if (!global_ptr)
        global_ptr = new GlobalClass;
    cout << "main: global_ptr is " << global_ptr->get_value() << '\n';
    foo();
    bar();
    return 0;
}

After

  Make the class responsible for its own global pointer and “initialization on first use” (by using a private static pointer and a public static accessor method). The client uses only the public accessor method.

class GlobalClass
{
private:
    int m_value;
    static GlobalClass *s_instance;
    GlobalClass(int v = 0)
    {
        m_value = v;
    }
  public:
    int get_value()
    {
        return m_value;
    }
    void set_value(int v)
    {
        m_value = v;
    }
    static GlobalClass *instance()
    {
        if (!s_instance)
          s_instance = new GlobalClass;
        return s_instance;
    }
};

// Allocating and initializing GlobalClass's
// static data member.  The pointer is being
// allocated - not the object inself.
GlobalClass *GlobalClass::s_instance = 0;

void foo(void)
{
  GlobalClass::instance()->set_value(1);
  cout << "foo: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

void bar(void)
{
  GlobalClass::instance()->set_value(2);
  cout << "bar: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

int main()
{
  cout << "main: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
  foo();
  bar();
}

 

    原文作者:wiessharling
    原文地址: http://www.cnblogs.com/wiessharling/archive/2013/09/21/3331501.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞