zoukankan      html  css  js  c++  java
  • 设计模式之单例模式代码示例

    using System;
    namespace SingletonFunc{
        public class Singleton{
    
            private static object obj = new object();
            private static Singleton singleton;
    
            public static Singleton CreateSingleton()
            {
                if(singleton == null){
                    lock(obj)
                    {
                        if(singleton == null)
                        {
                            singleton = new Singleton();
                        }
                    }
                }
                return singleton;
            }
            
            public override bool Equals(object obj)
            {
                if(obj == null) return false;
                if(singleton == obj as Singleton) return true;
                else 
                    return false;
            }
        }
    
        public class EntryPoint{
            
            public static void Main(string[] args){
                Singleton singleton1 = Singleton.CreateSingleton();
                Singleton singleton2 = Singleton.CreateSingleton();
    //            bool compare = System.Object.ReferenceEquals(singleton1,singleton2);
                bool compare = System.Object.Equals(singleton1,singleton2);
                compare = singleton1.Equals(singleton2);
                if(compare){
                    Console.Write("true");
                }
                else{
                    Console.Write("false");
                }
                Console.Read();
            }
        }
    }
  • 相关阅读:
    基于C++CJAVA的python入门
    雁栖湖健身计划
    显存的一些知识
    Cuda_bank-conflict
    翻译文章进展
    一些CV界的好资源
    how processor caches work
    LINQ-进阶的扩展方法
    LINQ-基础
    CTFHUB-技能树 基础知识 ctf练习平台
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2651558.html
Copyright © 2011-2022 走看看