设计模式--单例模式
来源:互联网 发布:nginx不同域名跳转 编辑:程序博客网 时间:2024/06/05 06:21
看了单例模式,写下个人的理解:
单例模式的目的保证在某一环境下,该类只拥有一个实例,且该实例是否已经创建由该类自己判断。由上面两点可知:一、这个类拥有自己的一个实例的引用。二、自己判断是否已有自身实例。写一个例子如下方便理解:
顺便分享一个知识点,==和equals的区别,==用来判断引用类型时,比较的是对象的内存地址是否相同。而equals比较的是“内容”是否相同。
package example;
public class SingletonTest
{
private static SingletonTest instance = null;
private SingletonTest()
{
}
public static SingletonTest getInstance()
{
if(instance == null)
new SingletonTest();
return instance;
}
public static void main(String[] args)
{
SingletonTest instance1 = SingletonTest.getInstance();
SingletonTest instance2 = SingletonTest.getInstance();
if(instance1 == instance2)
System.out.println("获得的两个实例的内存地址是相同的!");
}
}
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 设计模式-单例模式
- [设计模式] 单例模式
- 设计模式--单例模式
- 设计模式---单例模式
- 设计模式--单例模式
- 设计模式 -----单例模式
- 设计模式:单例模式
- 字符串匹配的KMP算法
- windown下安装meteor
- Spring的事务配置
- 1004 Toxophily
- 《iOS Human Interface Guidelines》——Creating Resizable Images
- 设计模式--单例模式
- 设计模式(一)代理模式
- Python正则表达式指南
- iOS 开发中常见的BUG
- android permission权限与安全机制解析
- 朝九晚五的程序员如何提高开发技能
- C#界面设计之单选按钮的使用
- 2016阿里笔试摇号买车
- 在eclipse中查看class源码