我了解面向对象编程,并且写了很长一段时间的面向对象程序。人们似乎在谈论面向方面的编程,但我从来没有真正了解过它是什么或如何使用它。基本的范式是什么?

这个问题是相关的,但并没有完全问出来:

面向方面编程与面向对象编程


当前回答

复制自Spring in Action

AOP is often defined as a technique that promotes separation of concerns in a software system. Systems are composed of several components, each responsible for a specific piece of functionality. But often these components also carry additional responsibilities beyond their core functionality. System services such as logging, transaction management, and security often find their way into components whose core responsibilities is something else. These system services are commonly referred to as cross-cutting concerns because they tend to cut across multiple components in a system.

其他回答

AOP是一种更好地模块化应用程序以实现跨越多个边界的功能的方法。AOP是封装这些特性的另一种方式,通过将这些横切关注点(日志记录、错误处理等)移出应用程序的主要组件来遵循单一职责。如果使用得当,随着时间的推移,AOP可以在应用程序中带来更高级别的可维护性和可扩展性。

复制自Spring in Action

AOP is often defined as a technique that promotes separation of concerns in a software system. Systems are composed of several components, each responsible for a specific piece of functionality. But often these components also carry additional responsibilities beyond their core functionality. System services such as logging, transaction management, and security often find their way into components whose core responsibilities is something else. These system services are commonly referred to as cross-cutting concerns because they tend to cut across multiple components in a system.

为了完整性,从副本中复制(蜂鸣器):

Class and method attributes in .NET are a form of aspect-oriented programming. You decorate your classes/methods with attributes. Behind the scenes this adds code to your class/method that performs the particular functions of the attribute. For example, marking a class serializable allows it to be serialized automatically for storage or transmission to another system. Other attributes might mark certain properties as non-serializable and these would be automatically omitted from the serialized object. Serialization is an aspect, implemented by other code in the system, and applied to your class by the application of a "configuration" attribute (decoration) .

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();
       }
    }
 }

然后使用方面编织器将代码编译成这样:

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;
} 

有一个AOP的例子,它用spring AOP作为例子。这个例子很容易理解。

Spring AOP(面向方面编程)框架用于模块化方面中的横切关注点。简单地说,它只是一个拦截器来拦截一些进程,例如,当一个方法被执行时,Spring AOP可以劫持正在执行的方法,并在方法执行之前或之后添加额外的功能。

参考:http://www.mkyong.com/spring/spring-aop-examples-advice/