How to Implement the Strategy Pattern in AOP

Can anyone tell me how to implement the strategy pattern in AOP?

An example using Spring-AOP or AspectJ would be very helpful.

+2


source to share


2 answers


The easiest way I've found is to implement your class with an empty interface.

Then you use AspectJ to inject the implementation into the interface.

Thus, if you need to change the algorithm, you can simply use a different aspect and solve the problem.

For this you can look at the tutorial on using intertype type: http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html#inter-type-declarations



Update: Because of the downside, I'm going to assume some people won't understand what I'm talking about, so the simplest way is an example. This article has some good examples of injecting input methods into an interface. http://ramnivas.com/blog/index.php?p=20

This does not implement the strategy pattern, but the basic concept is the same, which makes it easy to switch from one algorithm to another without changing any other part of the code. The only way I can do this is by using DI and just injecting a new concrete class, each with the same interface for Strategy, but that is beyond the scope of the question.

Update 2: Here are some links to show you what you can do with AOP: Getting rid of the density of the design pattern: http://www.ibm.com/developerworks/java/library/j-aopwork7/index.html Reinforcing design patterns with With AspectJ: http://www.ibm.com/developerworks/java/library/j-aopwork6/index.html AspectJ can be used for much more than just some basic cross-cutting concerns. Most GoF design patterns can be easily implemented or removed using AspectJ.

+5


source


I think you are confusing two things.



AOP is an implementation of various aspects around "real" code. Similar to logging and verification. The recording itself can be implemented with a DI container (like the one suggested by spring) to really delegate the logging to the correct implementation (strategy).

0


source







All Articles