AOP programming paradiag

AOP

https://en.wikipedia.org/wiki/Aspect-oriented_programming

Typically, an aspect is scattered or tangled as code, making it harder to understand and maintain. It is scattered by virtue of the function (such as logging) being spread over a number of unrelated functions that might use its function, possibly in entirely unrelated systems, different source languages, etc. That means to change logging can require modifying all affected modules. Aspects become tangled not only with the mainline function of the systems in which they are expressed but also with each other. That means changing one concern entails understanding all the tangled concerns or having some means by which the effect of changes can be inferred.

Logging exemplifies a crosscutting concern because a logging strategy necessarily affects every logged part of the system. Logging thereby crosscuts all logged classes and methods.

http://www.cnblogs.com/beliefbetrayal/archive/2012/02/03/2337522.html

  AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。它是一种新的方法论,它是对传统OOP编程的一种补充。

  OOP是关注将需求功能划分为不同的并且相对独立,封装良好的类,并让它们有着属于自己的行为,依靠继承和多态等来定义彼此的关系;AOP是希望能够将通用需求功能从不相关的类当中分离出来,能够使得很多类共享一个行为,一旦发生变化,不必修改很多类,而只需要修改这个行为即可。

Lua AOP

http://lua.2524044.n2.nabble.com/Question-about-AOP-like-function-calls-interception-td7651107.html

The reason why I'm asking is to see how something like AOP (Aspect Oriented Programming) approach could be applied to Lua. For example, I'd like to be able to execute a custom code before or after a call, manipulate call arguments and results, etc. And I'd like to be able to specify what I want to intercept (e.g. using patterns like this: "all calls of functions that look like set* or libname.*") outside of the code, where interception is supposed to be applied. That is the source code should not be aware of interception if possible. Normally it is achieved by means of interception or instrumentation in most languages. I'm wondering how and if this can be done in Lua. 


local
_print = print print = function( … ) -- before... _print( … ) -- after... end

http://lua-users.org/lists/lua-l/2011-01/msg00187.html

as many things OO, most 'fun' things in aspects are trivial to do in
functional programming:

for example, it's common to enhance the built-on type() function:

do
  local oldtype = type
  function type(x)
    local mt = getmetatable(x)
    if mt and mt.__type then return mt.__type end
    return oldtype(x)
  end
end

or you could do some 'prepend' functions:

function prepend(f,pre)
  return function(...)
    return pre(f,...)
  end
end


and use like this:

type = prepend (type, function(old, x)
  local mt = getmetatable(x)
  if mt and mt.__type then return mt.__type end
  return old(x)
end

AOP lua库

http://luaforge.net/projects/aspectlua/

AspectLua is an extension of Lua for Aspect-Oriented Programming. It follows some concepts of AspectJ, and enables the creation of Aspects for modularize crosscutting concerns in objects written in pure Lua.

如下: 对account 的 deposit方法进行跟踪, 调用完后, 进行打印。

dofile("AspectLua.lua")

account = luaorb.createproxy(readIOR("account.ref"),"IDL:Account:1.0")
account:deposit(5)

function logfile(a)
    print("o valor depositado e",a)
end

asp = Aspect:new("aspect.ref")
asp:aspect({name = 'AccountLogging'} , {name = 'tracingMethods', designator = 'call', list = {'bank_impl.deposit'}}, {type ='after', action = logfile})
原文地址:https://www.cnblogs.com/lightsong/p/6243587.html