最愛Java
書山有路勤為徑,學海無涯苦作舟
《AspectJ Cookbook》讀書筆記十七: 實現(xiàn)創(chuàng)建型面向?qū)ο笤O(shè)計模式
一.實現(xiàn)單件模式
單件模式允許把類定義成具有應(yīng)用程序內(nèi)的一個運行時實例。通常,通過不給特定類提供默認構(gòu)造函數(shù)來實現(xiàn)單件。
package
com.aspectj;
import
java.util.Hashtable;
public
abstract
aspect SingletonPattern issingleton()
{
private
Hashtable singletons
=
new
Hashtable();
public
interface
Singleton
{}
public
interface
NonSingleton
{}
//
Pointcut to define specify an interest in all creations
//
of all Classes that extend Singleton
pointcut selectSingletons() : call((Singleton
+
).
new
(..));
//
Pointcut to ensure that any classes in the Singleton inheritance tree
//
that are marked as Non Singletons are not included in the Singleton
//
logic.
pointcut excludeNonSingletons():
!
call((NonSingleton
+
).
new
(..));
Object around() : selectSingletons()
&&
excludeNonSingletons()
{
Class singleton
=
thisJoinPoint.getSignature().getDeclaringType();
synchronized
(singletons)
{
if
(singletons.get(singleton)
==
null
)
{
singletons.put(singleton, proceed());
}
}
return
(Object) singletons.get(singleton);
}
}
SingletonPattern抽象方面定義了兩個角色:Singleton和NonSingleton。這些角色被實現(xiàn)為接口,使得抽象方面可以處理單件,而無需關(guān)注實現(xiàn)細節(jié)。
Singleton接口由SingletonPattern抽象方面的子方面應(yīng)用于目標應(yīng)用程序內(nèi)將被視作單件的任何類。類似地,NonSingleton接口被應(yīng)用于可能通過繼承從其父類獲得單件行為的類。如果決定子類不是一個單件,那么就可以使用NonSingleton接口,使得重寫父類的單件特征。
聲明兩個切入點來捕獲何時實例化具有Singleton接口的類。selectSingletons()切入點定義用于選擇對擴展Singleton接口的類上構(gòu)造函數(shù)的調(diào)用。為了支持單件子類的關(guān)閉單件行為的要求,聲明了excludeNonSingletons()切入點。當需要阻止超類的單件行為影響子類時,可以通過單定的方面重寫這個切入點。
around()通知用于捕獲對應(yīng)用于Singleton接口的類上構(gòu)造函數(shù)的調(diào)用。around()通知會重寫構(gòu)造函數(shù),以檢查尚未創(chuàng)建正在實例化的對象的類型。
使用thisJoinPoint變量提供的類的信息,在單件散列表上查詢正在創(chuàng)建的對象的類。如果類的類型在散列表中不存在,那么就添加它的類,并通過調(diào)用proceed()來構(gòu)造那個類的一個對象,這會執(zhí)行原始的構(gòu)造函數(shù)邏輯。process()調(diào)用會返回構(gòu)造的對象,并把這個對象與類對象一起添加到散列表中。
如果散列表中存在類的類型,那么就不需要創(chuàng)建新對象。依據(jù)它的類從散列表映射檢索單件對象,并作為構(gòu)造函數(shù)調(diào)用的結(jié)果從around()通知返回這個對象。
以下為如何把SingletonPattern抽象方面應(yīng)用于特定的應(yīng)用程序中。
package
com.aspectj;
public
aspect PrinterSingleton
extends
SingletonPattern
{
declare parents:Printer
implements
Singleton;
declare parents:SpecializedPrinter
implements
NonSingleton;
}
二.實現(xiàn)原型模式
原型模式支持基于原始對象創(chuàng)建復制的對象。
package
com.aspectj;
public
abstract
aspect PrototypePattern
{
protected
interface
Prototype
{}
public
Object Prototype.clone()
throws
CloneNotSupportedException
{
return
super
.clone();
}
public
Object cloneObject(Prototype object)
{
try
{
return
object.clone();
}
catch
(CloneNotSupportedException ex)
{
return
createCloneFor(object);
}
}
protected
Object createCloneFor(Prototype object)
{
return
null
;
}
}
clone()方法是用于實現(xiàn)對象深復制的Java機制。有些基類可能不支持被復制;createCloneFor(Prototype)方法,可以被子方面重新,以執(zhí)行通用方面不知道的特定復制操作。
package
com.aspectj;
public
aspect GraphicPrototypes
extends
PrototypePattern
{
declare parents:MyClass
implements
Prototype;
declare parents:AnotherClass
implements
Prototype;
//
declare parents:Staff implements Prototype;
protected
Object createCloneFor(Prototype object)
{
if
(object
instanceof
MyClass)
{
return
null
;
}
else
if
(object
instanceof
AnotherClass)
{
return
null
;
}
else
{
return
null
;
}
}
}
三.實現(xiàn)抽象工廠模式
抽象工廠模式支持對一組相關(guān)類進行實例化,同時把工廠模式的客戶與準確的實現(xiàn)隔離開。
package
com.aspectj;
public
interface
ComputerFactory
{
public
Computer createPentiumProcessorComputer();
public
Computer createPentiumProcessorComputerWithHardDisk(HardDisk hardDisk);
}
package
com.aspectj;
public
aspect DefaultComputerFactoryImplementation
{
public
Computer createPentiumProcessorComputer()
{
Processor processor
=
new
Processor(
"
Pentium 4:9089085043
"
);
Motherboard motherboard
=
new
Motherboard(
"
019283
"
, processor);
HardDisk hardDisk
=
new
HardDisk(
"
739874
"
);
FloppyDisk floppyDisk
=
new
FloppyDisk(
"
93746
"
);
Computer computer
=
new
Computer(
"
12345
"
, motherboard,hardDisk,floppyDisk);
return
computer;
}
public
Computer createPentiumProcessorComputerWithHardDisk(HardDisk hardDisk)
{
Processor processor
=
new
Processor(
"
Pentium Standard:123478
"
);
Motherboard motherboard
=
new
Motherboard(
"
434244
"
, processor);
FloppyDisk floppyDisk
=
new
FloppyDisk(
"
434234
"
);
Computer computer
=
new
Computer(
"
56789
"
, motherboard,hardDisk,floppyDisk);
return
computer;
}
}
四.實現(xiàn)工廠方法模式
package
com.aspectj;
public
aspect DefaultComputerCreatorImplementation
{
public
void
ComputerCreator.createComputerAndPrintInventory(String serial)
{
System.out.println(
"
Inventory of computerparts:
"
);
System.out.println(
this
.createComputer(serial).toString());
}
}
package
com.aspectj;
public
aspect DefaultComputerCreatorImplementation
{
public
void
ComputerCreator.createComputerAndPrintInventory(String serial)
{
System.out.println(
"
Inventory of computerparts:
"
);
System.out.println(
this
.createComputer(serial).toString());
}
}
五.實現(xiàn)生成器模式
生成器模式用于捕獲在創(chuàng)建對象時可能需要的復雜步驟。這些步驟被實現(xiàn)為生成器類上的方法;在完成每個必須的步驟之后,就可以調(diào)用生成器來創(chuàng)建所得到的生成對象。
package
com.aspectj;
public
interface
TextPhraseBuilder
{
public
void
buildHeader(String title);
public
void
buildContent(String content);
public
void
buildFooter(String closingContent);
public
String getResult();
}
package
com.aspectj;
public
aspect TextPhraseBuilderDefaultImplementation
{
public
StringBuffer TextPhraseBuilder.result
=
new
StringBuffer();
public
String TextPhraseBuilder.getResult()
{
return
result.toString();
}
/** */
/**
* Declares a compiler error that gets reported if other classes
* (except Builders or this aspect) try to access the result variable.
*/
declare error:(
set(
public
StringBuffer TextPhraseBuilder
+
.result)
||
get(
public
StringBuffer TextPhraseBuilder
+
.result))
&&
!
(within(TextPhraseBuilder
+
)
||
within(TextPhraseBuilderDefaultImplementation)) :
"
variable result is aspect protected. use getResult() to access it
"
;
}
posted on 2008-08-27 11:00
Brian
閱讀(1129)
評論(0)
編輯
收藏
所屬分類:
《AspectJ Cookbook》讀書筆記
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
《AspectJ Cookbook中文版》的附帶示例下載
《AspectJ Cookbook》讀書筆記六: 捕獲通知上的連接點
《AspectJ Cookbook》讀書筆記二十二: 應(yīng)用企業(yè)級方面
《AspectJ Cookbook》讀書筆記二十一: 應(yīng)用應(yīng)用程序級方面
《AspectJ Cookbook》讀書筆記二十: 應(yīng)用類和組件級方面
《AspectJ Cookbook》讀書筆記十九: 實現(xiàn)行為型面向?qū)ο笤O(shè)計模式
《AspectJ Cookbook》讀書筆記十八: 實現(xiàn)結(jié)構(gòu)型面向?qū)ο笤O(shè)計模式
《AspectJ Cookbook》讀書筆記十七: 實現(xiàn)創(chuàng)建型面向?qū)ο笤O(shè)計模式
《AspectJ Cookbook》讀書筆記十六: 增強類和編譯器
《AspectJ Cookbook》讀書筆記十五: 定義方面的關(guān)系
公告
導航
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
<
2008年8月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
統(tǒng)計
隨筆 - 52
文章 - 0
評論 - 34
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(4)
給我留言
查看公開留言
查看私人留言
隨筆分類
《AspectJ Cookbook》讀書筆記(24)
(rss)
EXTJS(1)
(rss)
Jakarta Commons筆記(3)
(rss)
JScript(11)
(rss)
Struts2(4)
(rss)
數(shù)據(jù)結(jié)構(gòu)與算法(2)
(rss)
自編小工具(1)
(rss)
隨筆檔案
2010年11月 (2)
2010年10月 (2)
2009年10月 (13)
2009年1月 (3)
2008年12月 (1)
2008年8月 (18)
2008年7月 (2)
2008年6月 (4)
收藏夾
Java中的字符集編碼入門(6)
(rss)
搜索
最新評論
1.?re: Struts2學習筆記——輸入校驗(二)
ValidatorType.FIELD是什么意思呢?
--caipc
2.?re: ExtJs----彈出窗口
dsfsdfsdfsdf
--dgd
3.?re: javascript面向?qū)ο蠹夹g(shù)基礎(chǔ)(二)
@zx
什么意思?
--cxs
4.?re: javascript面向?qū)ο蠹夹g(shù)基礎(chǔ)(二)
rtwtwatwatst
--zx
5.?re: 《AspectJ Cookbook中文版》的附帶示例下載[未登錄]
謝謝
--jacky
閱讀排行榜
1.?ExtJs----彈出窗口(5592)
2.?ExtJs----Grid筆記(4777)
3.?ExtJs----拖放(3119)
4.?ExtJs----Ext支持的控件(2967)
5.?ExtJs----布局(2833)
評論排行榜
1.?《AspectJ Cookbook中文版》的附帶示例下載(12)
2.?插入排序思路與泛型版本的實現(xiàn)(4)
3.?歸并排序思路與泛型版本的實現(xiàn)(3)
4.?自編的"個人求職管理"小工具(2)
5.?《AspectJ Cookbook》讀書筆記四: 捕獲方法上的連接點(2)
Powered by:
BlogJava
Copyright © Brian
主站蜘蛛池模板:
欧亚精品一区三区免费
|
污视频网站在线观看免费
|
中文日本免费高清
|
日韩亚洲国产综合久久久
|
亚洲国产精品无码久久98
|
免费精品国产自产拍在线观看图片
|
久久er国产精品免费观看2
|
精品亚洲综合在线第一区
|
在线观看免费视频一区
|
亚洲av成人无码久久精品
|
国产精品免费观看调教网
|
亚洲国产精品张柏芝在线观看
|
免费影院未满十八勿进网站
|
日韩精品无码免费专区午夜不卡
|
亚洲视频一区二区在线观看
|
福利免费观看午夜体检区
|
美女黄色毛片免费看
|
亚洲精品成人片在线观看
|
中文字幕免费视频精品一
|
精品特级一级毛片免费观看
|
国产亚洲精品久久久久秋霞
|
暖暖免费日本在线中文
|
ww在线观视频免费观看w
|
亚洲狠狠狠一区二区三区
|
免费a级毛片视频
|
a毛片免费播放全部完整
|
一级毛片大全免费播放
|
香蕉视频免费在线
|
色噜噜的亚洲男人的天堂
|
亚洲AV午夜成人片
|
成人黄动漫画免费网站视频
|
在线观看的免费网站无遮挡
|
亚洲精品美女久久久久久久
|
亚洲人成日本在线观看
|
亚洲国产精品嫩草影院久久
|
嘿嘿嘿视频免费网站在线观看
|
精品在线视频免费
|
激情婷婷成人亚洲综合
|
小说专区亚洲春色校园
|
免费人妻精品一区二区三区
|
eeuss影院免费直达入口
|