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

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

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


当前回答

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

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) .

其他回答

复制自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.

为了完整性,从副本中复制(爱因斯坦):

经典的例子是安全性和日志记录。不是在应用程序中编写代码来记录x的发生或检查对象z的安全访问控制,而是有一种正常代码的“带外”语言装置,它可以系统地将安全性或登录到本地没有它们的例程中,即使您的代码没有提供它——它也会得到照顾。

一个更具体的例子是操作系统提供对文件的访问控制。软件程序不需要检查访问限制,因为底层系统会为它做这些工作。

根据我的经验,如果您认为您需要AOP,那么实际上您确实需要在系统中投入更多的时间和精力进行适当的元数据管理,并将重点放在经过深思熟虑的结构/系统设计上。

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

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的例子,它用spring AOP作为例子。这个例子很容易理解。

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

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

不幸的是,让AOP在一个中等规模的组织中真正有用似乎非常困难。(编辑器的支持,控制感,事实上你从不那么重要的事情开始导致代码崩溃,人们回家找家人,等等。)

我把希望寄托在面向组合的编程上,这是越来越现实的东西。它连接了许多流行的想法,给你一些非常酷的东西。

这里有一个即将实现的实现:qi4j.org/

实际上,我认为AOP的优点之一也是它的致命弱点:它是非侵入性的,如果可以的话,可以让人们忽略它,因此在大多数组织中它将被视为次要的关注点。