zoukankan      html  css  js  c++  java
  • singleton模式 在软件开发中的运用

    singleton模式可以保证这个类只有一个实例。

    下面是这个模式的一种典型的写法

    public class Singleton
    {
        
    protected Singleton() { }

        
    private static Singleton instance;
        
    private static object objLock = new object();
        
    public static Singleton Instance
        
    {
            
    get
            
    {
                
    if (instance == null)
                
    {
                    
    lock (objLock)
                    
    {
                        
    if (instance == null)
                            instance 
    = new Singleton();
                    }

                }

                
    return instance;
            }

        }

    }


    为了保证多线程环境下也只能有一个实例,所以在这里使用了锁(lock).如果每次获取的时候,都要将lock一次,这会对性能产生影
    响,所以在lock之前,我们又进行了一次判断。
    一般情况下,同一个运用程序都是使用同一个配置。所以配置就是一个Singleton的运用了。



    public class Settings
        
    {
            
    private Settings() { }

            
    private decimal _Tax = 0;
            
    public decimal Tax
            
    {
                
    get return _Tax; }
                
    set { _Tax = value; }
            }

        
    private decimal _Discount = 0;
            
    public decimal DefaultDiscount
            
    {
                
    get return _Discount; }
                
    set { _Discount = value; }
            }



            
    private void LoadSettings()
            
    {
                SettingsProvider.Load(
    this);  
            }


            
    private static Settings instance = null;
            
    private static object lockObj = new object();

            
    public static Settings CurrentSettings
            
    {
                
    get
                
    {
                    
    if (instance == null)
                    
    {
                        
    lock (lockObj)
                        
    {
                            
    if (instance == null)
                
    {    
                                instance 
    = new Settings();
                    instance.LoadSettings();
                }

                        }

                    }

                    
    return instance;
                }

            }

        }



    有些人习惯把配置写在数据库中,然后每次都读取一次数据库中的信息。在多用户环境下,或者类似WebForm运用程序,这样做浪费
    了一次数据库连接。而且不能保证修改后的数据马上被运用。
    有些业务逻辑需要使用单例模式。在我开发的一个餐厅管理软件中,当前的上下文中只能为一个客户点菜。这个时候就需要使用单例
    模式了,而且使用了单例模式后,在别的类中就可以很放心的使用这个类的实例,降低了开发的难度。



    private void Bind()
            
    {
                
    this.Reset();
                
    if (currentButton != null)
                    currentButton.BackColor 
    = currentButton.Parent.BackColor;
                Dish currentDish 
    = Context.CurrentContext.CurrentDish;
                
    bool verdict = (currentDish != null);

                List
    <Dish> lst = Paging.GetList(Context.CurrentContext.DishList, pageIndex, pageSize);
                
    for (int i = 0; i < dishButtons.Length && i < lst.Count; i++)
                
    {
                    dishButtons[i].OrderDish 
    = lst[i];
                    
    if (verdict && currentDish.Equals(lst[i]))
                    
    {
                        currentButton 
    = dishButtons[i];
                        currentButton.BackColor 
    = Color.SkyBlue;
                    }

                }

                
    this.Refresh();
            }

  • 相关阅读:
    Pyinstaller打包程序,运行时提示ModuleNotFoundError: No module named ‘pikepdf._cpphelpers’的解决办法
    Tkinter设置askopenfilename通过filetypes指定只能打开某一种格式的文件时,不能打开文件选择器
    Tkinter设置的回调函数程序运行自动执行,点击按钮没有执行回调函数
    写Python爬虫遇到的一些坑
    【Golang】【Lite IDE】Go语言环境安装及开发工具Lite IDE的安装
    VUE--当前页面请求定时器,其他页面不需要
    Cascader 级联选择器-------------子级全选则传父级, 子级未全选则传子级
    数组去重
    上传头像后导航栏中头像同步(Vue中监听sessionStorage)
    ui-app打包创建新证书
  • 原文地址:https://www.cnblogs.com/Linjianyu/p/1250406.html
Copyright © 2011-2022 走看看