C# 单例设计模式
来源:互联网 发布:加速器有mac 编辑:程序博客网 时间:2024/05/29 11:06
单例类:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _04单例设计模式{ class SingleObject { private SingleObject() //私有的构造函数 { } private static SingleObject _single = null; //私有静态的对象 public static SingleObject GetSingle() //获取对象的静态方法 { if (_single == null) { _single = new SingleObject(); } return _single; } public Form3 FrmThree //单例对象中的属性 { get; set; } public Form4 FrmFour //单例对象中的属性 { get; set; } public Form5 FrmFive { get; set; } public void CreateForm() { } }}单例类的使用:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace _04单例设计模式{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { if (SingleObject.GetSingle().FrmThree == null) //先通过静态方法获取单例对象(GetSingle()),然后获取单例对象的单例属性。 { SingleObject.GetSingle().FrmThree = new Form3(); //单例对象的属性也是单例的。 } SingleObject.GetSingle().FrmThree.Show(); } private void button3_Click(object sender, EventArgs e) { if (SingleObject.GetSingle().FrmFour == null) { SingleObject.GetSingle().FrmFour = new Form4(); } SingleObject.GetSingle().FrmFour.Show(); } }}
阅读全文
0 0
- C#单例设计模式
- C#单例设计模式
- C#设计模式(单例
- C# 单例设计模式
- C# 单例设计模式
- c# 设计模式:单例模式
- c# 设计模式:单例模式
- C#设计模式之单例模式
- C#设计模式-单例模式
- C#设计模式(1)单例模式
- C#设计模式_单例模式
- C#设计模式04-单例模式
- 【C#设计模式-单例模式】
- c#设计模式-单例模式
- C#设计模式-单例模式
- c#设计模式-单例模式
- c#设计模式之单例模式
- C#设计模式-单例模式
- Bjarne Stroustrup's FAQ(中文版)
- JVM--详解虚拟机字节码执行引擎之静态链接、动态链接与多态性实现机制
- Django2.0官方文档--概览
- spark学习-56-源代码:SparkUI界面
- 编译安装PHP,解决问题 Don't know how to define struct flock on this system, set --enable-opcache=no
- C# 单例设计模式
- 6-2 学生成绩链表处理(10 分) 本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。 函数接口定义: struct stud_node *
- Java线程池原码分析
- Python基础-函数-定义函数
- 穷举算法(鸡兔同笼)
- Mysql
- scanf();为什么要用取地址符
- 激活函数-relu
- Mac安装sublime后怎么在网页测试