惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
小众软件
小众软件
P
Proofpoint News Feed
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threat Research - Cisco Blogs
K
Kaspersky official blog
V
V2EX
博客园 - Franky
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Scott Helme
Scott Helme
量子位
SecWiki News
SecWiki News
博客园 - 叶小钗
S
SegmentFault 最新的问题
L
LINUX DO - 最新话题
Attack and Defense Labs
Attack and Defense Labs
T
Tailwind CSS Blog
Google DeepMind News
Google DeepMind News
T
Tor Project blog
N
News and Events Feed by Topic
The Cloudflare Blog
Help Net Security
Help Net Security
Forbes - Security
Forbes - Security
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
P
Privacy & Cybersecurity Law Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
Arctic Wolf
L
LangChain Blog
Latest news
Latest news
S
Schneier on Security
C
CERT Recently Published Vulnerability Notes
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
T
The Blog of Author Tim Ferriss
Schneier on Security
Schneier on Security
S
Security @ Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
Blog — PlanetScale
Blog — PlanetScale
O
OpenAI News

博客园 - BigRain

小学生学习汉字,汉字抓取 c# regex regextools cache 访问频率的思考 python 百度cpc点击 20130118SQL记录 PPT辅助工具 检查外链的方法 webService启用cookie的另一种方法 百度调价HttpWebRequest 装饰者模式 第一个android程序闪亮登场 SQL 的一些批量插入和修改 职责链模式的运用 State Pattern 程序人生 SerialPort实现modem的来电显示 利用枚举进行状态的设计 我对当前项目的一些看法 闲话闲说——关于异常
singleton模式 在软件开发中的运用
BigRain · 2008-07-24 · via 博客园 - BigRain

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();
        }