Posts tagged ‘AOP’
Aspected oriented programming seemed at first to strike me rather as a solution in search of a problem – and to a degree it still does – So I thought I might try to record various problems that aspect oriented programming might solve as they come to mind or I come across them.
The observer pattern tends to result in “notify” function calls being littered throughout the observed object. These can be replaced by a single aspect – thereby separating out the “being observered” behaviour of the observed object from the rest of its behaviour.
To clarify, given some object we can turn it into an observed object by adding a notify method and an aspect that calls this method whenever a modifier is called. If all the modifiers start with “set” or return void then this could be particularly easy to do and if one could be sufficiently consistent throughout one’s codebase – one could imagine having a “subject mixin”, that makes an object observed witout the object being otherwise modifed. All that would be required would be a standard way to identify all the modifiers such that an aspect could be applied to them.
Switching caching of function returns on and off at compile time
One could have a separate caching aspect that adds code before a pointcut to check if a value is in a dictionary, and if so return the stored value. Some care would have to be taken to create this dictionary and store it in an appropriate place.
One could then control caching throughout the application from one locatio.
Restricting access to certain functions based on user
Our aspect would add code that checks the users permissions and dies and raises exceptions if the permission is not set. (This idea is adapted from here
Doing anything that you might want to do with decorators in python
Decorators in python are quite similar to aspects – but decorators (usually) are applied when the function is defined. (I’m sure one can do crazy things with metaclasses so as to fit aspect oriented programming to python if one so desired… it would all be done at runtime however…)