3 回答

TA貢獻1796條經驗 獲得超7個贊
AOP解決了橫切關注點的問題,橫切關注點可能是在不同方法中重復的任何類型的代碼,通常無法像日志記錄或驗證那樣被完全重構為自己的模塊。因此,使用AOP,您可以將這些內容排除在主要代碼之外,并按如下方式垂直定義:
function mainProgram()
{
var x = foo();
doSomethingWith(x);
return x;
}
aspect logging
{
before (mainProgram is called):
{
log.Write("entering mainProgram");
}
after (mainProgram is called):
{
log.Write( "exiting mainProgram with return value of "
+ mainProgram.returnValue);
}
}
aspect verification
{
before (doSomethingWith is called):
{
if (doSomethingWith.arguments[0] == null)
{
throw NullArgumentException();
}
if (!doSomethingWith.caller.isAuthenticated)
{
throw Securityexception();
}
}
}
然后使用aspect-weaver將代碼編譯為以下內容:
function mainProgram()
{
log.Write("entering mainProgram");
var x = foo();
if (x == null) throw NullArgumentException();
if (!mainProgramIsAuthenticated()) throw Securityexception();
doSomethingWith(x);
log.Write("exiting mainProgram with return value of "+ x);
return x;
}

TA貢獻1797條經驗 獲得超4個贊
為了完整性而從副本中復制(愛因斯坦):
經典示例是安全性和日志記錄。與其在您的應用程序內編寫代碼以記錄x的出現或檢查對象z的安全訪問控制,不如使用普通代碼的“帶外”語言措辭,它可以系統地注入安全性或登錄沒有天真地將其包含在其中的例程這樣一種方式,即使您的代碼不提供它,它也會得到照顧。
一個更具體的示例是操作系統提供對文件的訪問控制。一個軟件程序不需要檢查訪問限制,因為底層系統可以完成該工作。
如果您認為根據我的經驗需要AOP,則實際上確實需要投入更多的時間和精力來進行系統內適當的元數據管理,并著重考慮周全的結構/系統設計。
- 3 回答
- 0 關注
- 518 瀏覽
添加回答
舉報