设计模式——单键模式

名称Singleton
结构 single.gif
意图保证一个类仅有一个实例,并提供一个访问它的全局访问点。
适用性
  • 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
  • 当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。

 1 None.gif //  Singleton
 2 None.gif
 3 None.gif //  Intent: "Ensure a class only has one instance, and provide a global
 4 None.gif //  point of access to it". 
 5 None.gif
 6 None.gif //  For further information, read "Design Patterns", p127, Gamma et al.,
 7 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
 8 None.gif
 9 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
10InBlock.gif * If it makes sense to have only a single instance of a class (a so-called
11InBlock.gif * singleton), then it makes sense to enforce this (to elimintate potential 
12InBlock.gif * errors, etc). 
13InBlock.gif * 
14InBlock.gif * A class based on the singleton design pattern protects its constructor, 
15InBlock.gif * so that only the class itself (e.g. in a static method) may instantiate itself. 
16InBlock.gif * It exposes an Instance method which allows client code to retrieve the 
17InBlock.gif * current instance, and if it does not exist to instantiate it.  
18ExpandedBlockEnd.gif */

19 None.gif 
20 None.gif namespace  Singleton_DesignPattern
21 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
22InBlock.gif    using System;
23InBlock.gif
24InBlock.gif    class Singleton 
25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
26InBlock.gif        private static Singleton _instance;
27InBlock.gif        
28InBlock.gif        public static Singleton Instance()
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            if (_instance == null)
31InBlock.gif                _instance = new Singleton();
32InBlock.gif            return _instance;
33ExpandedSubBlockEnd.gif        }

34ExpandedSubBlockStart.gifContractedSubBlock.gif        protected Singleton()dot.gif{}
35InBlock.gif
36InBlock.gif        // Just to prove only a single instance exists
37InBlock.gif        private int x = 0;
38ExpandedSubBlockStart.gifContractedSubBlock.gif        public void SetX(int newVal) dot.gif{x = newVal;}
39ExpandedSubBlockStart.gifContractedSubBlock.gif        public int GetX()dot.gif{return x;}        
40ExpandedSubBlockEnd.gif    }

41InBlock.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
43InBlock.gif    ///    Summary description for Client.
44ExpandedSubBlockEnd.gif    /// </summary>

45InBlock.gif    public class Client
46ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
47InBlock.gif        public static int Main(string[] args)
48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
49InBlock.gif            int val;
50InBlock.gif            // can't call new, because constructor is protected
51InBlock.gif            Singleton FirstSingleton = Singleton.Instance(); 
52InBlock.gif            Singleton SecondSingleton = Singleton.Instance();
53InBlock.gif
54InBlock.gif            // Now we have two variables, but both should refer to the same object
55InBlock.gif            // Let's prove this, by setting a value using one variable, and 
56InBlock.gif            // (hopefully!) retrieving the same value using the second variable
57InBlock.gif            FirstSingleton.SetX(4);
58InBlock.gif            Console.WriteLine("Using first variable for singleton, set x to 4");        
59InBlock.gif
60InBlock.gif            val = SecondSingleton.GetX();
61InBlock.gif            Console.WriteLine("Using second variable for singleton, value retrieved = {0}", val);        
62InBlock.gif            return 0;
63ExpandedSubBlockEnd.gif        }

64ExpandedSubBlockEnd.gif    }

65ExpandedBlockEnd.gif}

66 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/06/25/180834.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值