AspectJ实现设计模式(六)—单例模式 (转)

AspectJ实现设计模式(六)—单例模式 (转)[@more@]

本文介绍使用ASPectJ实现设计模式之单例模式,文章会实现一个AspectJ版本的登记式单例类。

 XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

示例说明

单例模式有以下一些基本特点

·单例类只能有一个实例

·单例类必须自己创建自己的唯一实例

·单例类必须给所有其他对象提供这一实例

 

Java中我们实现单例类一般需要使用私有构造子和一个静态实例变量,还要提供一个方法如getInstance()来构造并返回这个实例变量。而使用AspectJ实现单例模式,可以完全将这些细节交由一个具体方面处理,客户端仍可以使用new操作符创建对象的实例,而且该方面将利用错误声明(declare error)机制联合pointcut捕捉单例类子类的构造行为并将其禁止,以保证单例类的实例不会被其子类构造。下面就让我们来通过使用AspectJ实现一个登记式的单例类,来看看到底具体方面变了怎样的魔术。

2003-7-191530490.jpg" align=baseline border=0>  图1:AspectJ实现登记式单例类的UML图

 

如图所示,抽象方面RegSingletonProtocol使用inter-type声明定义了一个Singleton标识接口,具体单例类需要实现此接口。私有变量singletons用来登记各种单例类的实例变量。抽象pointcut由子方面实现,而Advice around则实现具体的单例类构造和登记入表的逻辑。

 

具体方面RegSingletonImpl则继承了抽象方面,它定义了具体的subClasSCOnstructor的行为并声明了一个RegSingleton类,声明它实现Singleton接口,它就是具体的单例类。还声明了一个编译时错误,它当客户代码调用RegSingleton子类的构造子时发生。

 

图左侧的类就是用来测试单例类的功能以及单例类的子类能否构造单例类,所以逻辑很简单只包含构造子。

 

  源代码

抽象方面RegSingletonProtocol.java

import java.util.*;

public abstract aspect RegSingletonProtocol {

  private Hashtable singletons=new Hashtable();

  public interface Singleton{}

  abstract pointcut subClassConstructor();

 

  Singleton around() : call((Singleton+).new(..)){//捕捉Singleton子类的构造子调用

  //获取类变量

  Class singleton=thisJoinPoint.getSignature().getDeclaringType();

  if(singletons.get(singleton)==null){

  singletons.put(singleton,proceed());//若没有此类变量,则将其实例登记入表

  }

  return (Singleton)singletons.get(singleton);//返回指定类别的实例

  }

}

值得一提的是around,它首先利用thisJoinPoint实例的方法获得类变量,然后搜索表,若该类型不存在则利用该类型本身的构造子创建实例(使用proceed()方法执行捕捉到的方法调用的原有逻辑,这里表示Singleton子类的构造函数)并将其存入表中利用,然后返回该类型的实例。

 

具体方面RegSingletonImpl..java

public aspect RegSingletonImpl extends RegSingletonProtocol{

  declare parents : RegSingleton implements Singleton;

  pointcut subClassConstructor() : call((RegSingletonChild+).new(..));

  declare error : subClassConstructor() : "Sub class can't access the singleton instance";

}

具体方面声明具体的单例类,让它实现Singleton接口subClassConstructor捕捉所有RegSingletonChild及其子类的构造子调用(”+”表示当前类和它的子类),declare error当捕捉到此调用时产生编译错误。

 

单例类RegSingleton..java

public class RegSingleton {

  private static int count=0;

  public RegSingleton() {

  count++;

  System.out.println("Class No. "+count);

  }

}

 

单例类子类RegSingletonChild.java

public class RegSingletonChild extends RegSingleton{

  public RegSingletonChild() {

  super();

  }

}

 

客户端演示Demo.java

public class Demo {

  public static void main(String [] args){

  System.out.println(new RegSingleton());

//  System.out.println(new RegSingletonChild());

  System.out.println(new RegSingleton());

  }

}

 

结果分析

Demo输出的结果如下

Class No. 1

aopsingleton.RegSingleton@6e1408

aopsingleton.RegSingleton@6e1408

表明类RegSingleton只创建了一次,两次构造子调用返回同样的实例,这符合单例类的特点。注意如果将Demo中被注释掉的RegSingletonChild创建打开并编译就会得到如下错误

1  error

"D:JavaDevjbuilderAOPSingletonsrcDemo.java":

Sub class can't access the singleton instance at line 15, column 0

这样就克服了在Java实现登记式单例类子类创建其他实例的缺点。

 

读者可以很容易的将这个例子改为非登记的单例类。只要在RegSingletonProtocol中加入如下语句即可

  private Singleton instance=null;

然后修改around中的逻辑如下

  Singleton around() : call((Singleton+).new(..)){

  if(instance==null){

  try{

  instance=(Singleton)proceed();

  }catch(Exception e){

  throw new RuntimeException("Construction failed");

  }

  }

return instance;

  }

 

声明

本文由starchu1981保留版权,如果需要转贴请写明作者和出处。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-959449/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10748419/viewspace-959449/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值