Originally posted by Stan James:My first thought was a collection of strategies, which is iterating.[CODE]
Well - if you go looser than collection (as in
a number of) then you are moving towards the idea of policies. In other domains the term "traits" is used (which have a different meaning in connection with C++ templates).
Traits are a language feature in Squeak and Smalltalk. Traits provide sets of behavior (methods - not state) that establish a
unit of reuse on a finer level than that of a class. Each trait in a class is supposed to deal with its own responsibility - a responsibility that has to be orthogonal to any other responsibility handled by other traits in the class (the idea is similar to that of a mix-in - but mix-ins are allowed to keep state).
The problem with a
Chain of Responsibility or even a collection of
Strategies is that each unit of behavior has to implement exactly the same interface. If the responsibilities of each unit of behavior are in fact orthogonal then it stands to reason that they will have differing interface requirements. To create the "single interface" your are going to have to throw all the requirements into one interface - which probably violates the interface segregation principle.
Traits also tend to deal with two interfaces: the methods that they add to the class and the methods that they require (depend on; most of them depend on something as they do not have their own state). That translates to a lot of explicit interfaces in Java as it doesn't support latent typing of any kind. One interface for each family of traits that a class wants to take advantage of and a complimentary interface that a class has to implement to support that family of traits.
However you can emulate traits in Java by using AspectJ's inter-type declaration (formerly introduction) feature as outlined in
Traits Programming with AspectJ (PDF).
Is it worth it? I don't know and it probably depends ...
Originally posted by Meir Yan:
now in those after/before methods i will like to be able to insert all kind of methods some times validations some times security and so on. how can i make this generic as possible ?
I wonder ... are you simply asking for some guidelines on how to "organize" your code, e.g. what and how much code to place in the advice/aspect and when to move code into stand-alone classes?