设计模式-单例模式

来源:互联网 发布:linux 多线程结束 编辑:程序博客网 时间:2024/05/16 10:50
//经典单例模式
public class Singleton {private static Singleton uniqeInstance=null;private Singleton(){};public static Singleton getInstance(){if(uniqeInstance==null){uniqeInstance=new Singleton();}return uniqeInstance;}}
//一个巧克力工厂的例子
<pre name="code" class="java">public class ChocolateFactory {private boolean empty;private boolean boiled;public volatile static ChocolateFactory uniqueInstance = null;private ChocolateFactory() {empty = true;boiled = false;}public static ChocolateFactory getInstance() {if (uniqueInstance == null) {synchronized (ChocolateFactory.class) {if (uniqueInstance == null) {uniqueInstance = new ChocolateFactory();}}}return uniqueInstance;}public void fill() {if (empty) {// 添加原料巧克力动作empty = false;boiled = false;}}public void drain() {if ((!empty) && boiled) {// 排出巧克力动作empty = true;}}public void boil() {if ((!empty) && (!boiled)) {// 煮沸boiled = true;}}}


//多线程环境下的代码
<pre name="code" class="java">public class ChocolateFactory {private boolean empty;private boolean boiled;public volatile static ChocolateFactory uniqueInstance = null;private ChocolateFactory() {empty = true;boiled = false;}//加入synchronized关键字保证同一时间只有一个线程访问函数 缺点是大量访问时synchronized效率低public static synchronized ChocolateFactory getInstance() {if (uniqueInstance == null) {uniqueInstance = new ChocolateFactory();}return uniqueInstance;}public void fill() {if (empty) {// 添加原料巧克力动作empty = false;boiled = false;}}public void drain() {if ((!empty) && boiled) {// 排出巧克力动作empty = true;}}public void boil() {if ((!empty) && (!boiled)) {// 煮沸boiled = true;}}}
//代码优化-急切创建实例
public class ChocolateFactory {<span style="white-space:pre"></span>private boolean empty;<span style="white-space:pre"></span>private boolean boiled;
//此处直接new一个对象;<span style="white-space:pre"></span>public volatile static ChocolateFactory uniqueInstance = new ChocolateFactory();<span style="white-space:pre"></span>private ChocolateFactory() {<span style="white-space:pre"></span>empty = true;<span style="white-space:pre"></span>boiled = false;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public static  ChocolateFactory getInstance() {//直接返回创建好的对象,但缺点是每次初始化类时,无论是否需要,都会创建该实例<span style="white-space:pre"></span>return uniqueInstance;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void fill() {<span style="white-space:pre"></span>if (empty) {<span style="white-space:pre"></span>// 添加原料巧克力动作<span style="white-space:pre"></span>empty = false;<span style="white-space:pre"></span>boiled = false;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void drain() {<span style="white-space:pre"></span>if ((!empty) && boiled) {<span style="white-space:pre"></span>// 排出巧克力动作<span style="white-space:pre"></span>empty = true;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void boil() {<span style="white-space:pre"></span>if ((!empty) && (!boiled)) {<span style="white-space:pre"></span>// 煮沸<span style="white-space:pre"></span>boiled = true;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}
//双重枷锁检查
<pre name="code" class="java">public class ChocolateFactory {private boolean empty;private boolean boiled;public volatile static ChocolateFactory uniqueInstance =null;private ChocolateFactory() {empty = true;boiled = false;}public static  ChocolateFactory getInstance() {if (uniqueInstance == null) {
//此处synchronized修饰 只执行一次即可 提升效率synchronized (ChocolateFactory.class) {if (uniqueInstance == null) {uniqueInstance = new ChocolateFactory();}}}return uniqueInstance;}public void fill() {if (empty) {// 添加原料巧克力动作empty = false;boiled = false;}}public void drain() {if ((!empty) && boiled) {// 排出巧克力动作empty = true;}}public void boil() {if ((!empty) && (!boiled)) {// 煮沸boiled = true;}}}


                                             
0 0
原创粉丝点击