zoukankan      html  css  js  c++  java
  • The Singleton of Design Pattern单态模式

    1 Singleton Definition 单态模式定义
           The main purpose is to gurantee that the instance of class has only one in the application.  some operations, such as connecting to database, are userful for only single instance.
            单态模式的主要作用是保证类的实例只有一个。在很多操作中,比如数据库连接等,这种单实例是很有用的。
     
    2 singleton model单态模型


    3 related singleton model codes:相关单态模式代码
    namespace singleton
    {
        public class singleton{
        private int _counter;
        private static singleton _instance;
        private static object _classLock=typeof(singleton);
        private singleton()
        {
            _counter = 0;
        }
        public static singleton CreateInstance()//global point to service
        {
            lock (_classLock)
            {
                if (_instance == null)
                    instance =
    new singleton();
            }
                return _instance;
          }

        public void Counte()
        {
            lock (_classLock)//exclusive lock
                {
                    _counter++;
                }
        }

         public int Counter
        {    
            get
                {
                    return _counter;
                }
        }
      }

        class Program
        {
            static void Main(string[] args)
            {
                singleton hiron = singleton.CreateInstance();
                hiron.Counte();
                singleton hrg = singleton.CreateInstance();
                hrg.Counte();
                Console.WriteLine(hrg.Counter);
            }    
        }
    }
    excution result:
    instance hiron and hrg will refer to the same instance actually, and the output is 2

  • 相关阅读:
    9.Java通过axis调用WebService
    8.AXIS1基础
    7.Web Service 调用天气代码
    Exception in thread "main" java.lang.NoClassDefFoundError: org.jaxen.NamespaceContext
    webservices 服务器未能识别 HTTP 头 SOAPAction 的值:.
    几种流行Webservice框架性能对比
    6. 使用Axis开发WebService程序
    5. webservice通信调用天气预报接口实例
    4.菜鸟教你一步一步开发 web service 之 axis 客户端创建
    3.菜鸟教你一步一步开发 web service 之 axis 服务端创建
  • 原文地址:https://www.cnblogs.com/Winston/p/1183077.html
Copyright © 2011-2022 走看看