• 单例设计模式


    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() << '
    ';
    }
    void bar(void)
    {
      if (!global_ptr)
        global_ptr = new GlobalClass;
      global_ptr->set_value(2);
      cout << "bar: global_ptr is " << global_ptr->get_value() << '
    ';
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        if (!global_ptr)
            global_ptr = new GlobalClass;
        cout << "main: global_ptr is " << global_ptr->get_value() << '
    ';
        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() << '
    ';
    }
    
    void bar(void)
    {
      GlobalClass::instance()->set_value(2);
      cout << "bar: global_ptr is " << GlobalClass::instance()->get_value() << '
    ';
    }
    
    int main()
    {
      cout << "main: global_ptr is " << GlobalClass::instance()->get_value() << '
    ';
      foo();
      bar();
    }
  • 相关阅读:
    Socket和ServerSocket学习笔记
    跨域资源共享的10种方式
    javascript执行环境(执行期上下文)详解
    js 自动插入分号
    原型对象prototype和原型属性[[Prototype]]
    构造自己的动画函数:animation,stop功能的实现
    sizzle源码分析 (4)sizzle 技术总结及值得我们学习的地方
    sizzle源码分析 (3)sizzle 不能快速匹配时 选择器流程
    sizzle源码分析 (2)ID 类 tag querySelectorAll 快速匹配
    sizzle源码分析 (1)sizzle架构
  • 原文地址:https://www.cnblogs.com/wiessharling/p/3331501.html
Copyright © 2020-2023  润新知