炒冷饭系列:设计模式 单例模式
来源:互联网 发布:科比00年总决赛数据 编辑:程序博客网 时间:2024/06/11 11:16
炒冷饭系列:设计模式 单例模式
摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢!
亲爱我,孝何难;亲恶我,孝方贤。
一、什么是单例模式
单例模式是一种对象创建型模式,使用单例模式, 可以保证为一个类只生成唯一的实例对象。也就是说, 在整个程序空间中,该类只存在一个实例对象。 其实,GoF对单例模式的定义是:保证一个类、 只有一个实例存在,同时提供能对该实例加以访 问的全局访问方法。
二、为什么要使用单例模式呢?
在应用系统开发中,我们常常有以下需求:
- - 在多个线程之间,比如servlet环境,共享同一个资源或者操作同一个对象
- - 在整个程序空间使用全局变量,共享资源
- - 大规模系统中,为了性能的考虑,需要节省对象的创建时间等等。
因为Singleton模式可以保证为一个类只生成唯一的实例 对象,所以这些情况,Singleton模式就派上用场了。
三、单例模式实现
1.饿汉式。
public class Person { public static final Person person = new Person(); private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } //构造函数私有化 private Person() { } //提供一个全局的静态方法 public static Person getPerson() { return person; }}
2.懒汉式。
public class Person2 { private String name; private static Person2 person; public String getName() { return name; } public void setName(String name) { this.name = name; } //构造函数私有化 private Person2() { } //提供一个全局的静态方法 public static Person2 getPerson() { if(person == null) { person = new Person2(); } return person; }}
3.双重检查。
public class Person3 { private String name; private static Person3 person; public String getName() { return name; } public void setName(String name) { this.name = name; } //构造函数私有化 private Person3() { } //提供一个全局的静态方法,使用同步方法 public static synchronized Person3 getPerson() { if(person == null) { person = new Person3(); } return person; }}
public class Person4 { private String name; private static Person4 person; public String getName() { return name; } public void setName(String name) { this.name = name; } //构造函数私有化 private Person4() { } //提供一个全局的静态方法 public static Person4 getPerson() { if(person == null) { synchronized (Person4.class) { if(person == null) { person = new Person4(); } } } return person; }}
MainClass:
public class MainClass { public static void main(String[] args) { Person2 per = Person2.getPerson(); Person2 per2 = Person2.getPerson(); per.setName("zhangsan"); per2.setName("lisi"); System.out.println(per.getName()); System.out.println(per2.getName()); }}
四、感谢知识来源和小结
来自:java设计模式
如以上文章或链接对你有帮助的话,别忘了在文章按钮或到页面右下角点击 “赞一个” 按钮哦。你也可以点击页面右边“分享”悬浮按钮哦,让更多的人阅读这篇文章。
亲有过 谏使更 怡吾色 柔吾声 谏不入 悦复谏 号泣随 挞无怨
0 0
- 炒冷饭系列:设计模式 单例模式
- 炒冷饭系列:设计模式 工厂模式
- 炒冷饭系列:设计模式 工厂方法模式
- 炒冷饭系列:设计模式 抽象工厂模式
- 炒冷饭系列:设计模式 原型模式
- 炒冷饭系列:设计模式 建造者模式
- 炒冷饭系列:设计模式 装饰模式
- 设计模式系列之单例模式
- 设计模式系列3-单例模式
- JAVA系列-设计模式-单例模式
- Android设计模式系列-单例模式
- Android设计模式系列-单例模式
- Android设计模式系列-单例模式
- Android设计模式系列-单例模式
- 设计模式系列之 单例模式
- 设计模式系列-----------单例模式
- Android设计模式系列-单例模式
- Android设计模式系列-单例模式
- ubuntu 安装gcc pcre
- Single Round Match
- HDU 4883 TIANKENG’s restaurant
- 使用rgba属性兼容IE8
- 九度OJ 1021统计字符----20140728
- 炒冷饭系列:设计模式 单例模式
- poj 2006 Litmus Test 【即zoj 2351:计算酸的PH】
- iOS的一些理解
- Spring MVC使用fastjson数据传到前台中文乱码解决
- Cloudera Manager 和 CDH 4 终极安装
- Passing address of non-local object to __autoreleasing parameter for write-back
- 电源管理之regulator机制流程
- python -> lambda与def的区别
- 一淘搜索之网页抓取系统分析与实现(3)—scrapy+webkit & mysql+django