• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the best pattern in this case to use

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all
i have this simple case . i have generic functions that are empty now
they will be invoked before business method and after this method ( yes im using AOP here ... )
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 ?
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds to me like you are looking for a policy-based design. Policy-based design emerged in the C++ community once support of templates between compliers became much more consistent. Its not something widely practiced in Java circles as Java Generics aren't as permissive as C++ templates. Basically the template parameter classes are used to inject policies (basically strategy objects) into the class template to generate the overall class at compile time. In C++ this works well because the template parameter classes don't have to implement any specific interface - everything works as long as all the methods that are used by the class template are supported by the applied policy classes. In Java you would still have to define an interface for each policy (strategy) that you intend to accept - so you still have to define the overall class, the policy interfaces accepted and the implementing policy classes.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me it sounds like Intercepting Filter Pattern.
Or no?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me maybe Decorator pattern is useful

You can define an interface which will be configured into your AOP config xml. Then you can implement the interface by different class like Validation, Security. For each class the there is a constructor with the parameter of this interface.

public interface IAdvice
public void execute()

public class ValidationAdvice()
public ValidationAdvice(IAdvice advice)

.....other implement class.....

Finally you can define a List just like the InterceptFilter config, or simply wrap the impl class like new FinalAdvice(new SecurityAdvice(new ValidationAdvice()))

At last you can add as much decorator class as you want and config the classname into the xml files for your AOP.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. Filter and decorator both give you some kind of chaining. My first thought was a collection of strategies, which is iterating.

Now we have three ways to execute any combination of actions. The mechanics of constructing them might influence which you use. Any of them sound useful?
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic