首页 > Windows开发 > 详细

C#之设计模式

时间:2017-01-06 01:09:52      阅读:243      评论:0      收藏:0      [点我收藏+]

单例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 单例 {
    class Program {
        static void Main(string[] args) {
            Singleton instance1 = Singleton.Instance;
            Singleton instance2 = Singleton.Instance;
            bool result = object.ReferenceEquals(instance1, instance2);
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
    public sealed class Singleton {
        private static Singleton _instance;
        private static object obj = new object();
        private Singleton() { }
        public static Singleton Instance{
            get {
                if(_instance == null) {
                    lock (obj) {
                        if(_instance == null) {
                            _instance = new Singleton();
                        }
                    }
                }
                return _instance;
            }
        }
    }

}

代理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 代理 {
    class Program {
        delegate void MyDelegate(string str);
        static void Main(string[] args) {
            MyDelegate md = new MyDelegate(DelegateText.Function);
            md("刘冠");//调用
            Console.ReadLine();
        }
    }
    //一个类调用,另一个类实现
    public class DelegateText {
        public static void Function(string str) {
            Console.WriteLine(str);//实现
        }
    }
}

 

C#之设计模式

原文:http://www.cnblogs.com/liuguan/p/6254618.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!