Reputation: 5240
I am working on a bespoken framework with a specific APIs, and now I am working on re-factoring it.
One of the issues I have to solve is to create a route like approach to redirect queries to different classes based on depth of queries and other attributes. Following is the method :
class Blah {
public Object query1(){...}
public Object query2(){...}
}
and I have to change it to something like
class Blah implements BeforeFilter{
@Override
protected void filter()
{
//decide to call which query method
//then redirect the request to query
}
public Object query1(){...}
public Object query2(){...}
}
A very traditional way of doing above idea is to add an if/else inside each query() method to decide to go further or not.
To be honest, I don't like this approach. That's why I'm looking for something more elegant.
P.S : the reason of doing that is not to change public APIs. I had implemented the proxy design pattern but that one would change some of the APIs.
Upvotes: 0
Views: 127
Reputation: 14307
This sounds to me like the Chain-of-Responsibility which is exactly a "route-like" approach, the filters or chain elements comply to a common interface and you assemble them in different ways similar to a linked list. The queries/requests get passed to the first chain element and in turn it passes the request to its "next" chain element and so on. Each Chain element or filter decides whether to do something with the request/SQL etc.
This way you have the filtering mechanism where each filter (or chain element) is decoupled from the others and there is no need for an if-then-else ladder.
Upvotes: 2