I have a function that creates URLs for requests as follows:
public String createUrl(String path, String filename) {
return new StringBuilder(path).append("?filename=").append(filename).toString();
}
Now, what I want to do is to add another parameter to the URL called transactionId. A naive implementation would be:
public String createUrl(String path, String filename,String transactionId) {
return new StringBuilder(path).append("?filename=").append(filename).append("?transactionId=").append(transactionId).toString();
}
or I can have a static method somewhere that returns a transactionID and modify the code as follows:
public String createUrl(String path, String filename) {
return new StringBuilder(path).append("?filename=").append(filename).append("?transactionId=").append(TransactionMonitor.getTransactionId()).toString();
}
Both of these approaches makes me modify the preexisting code. Is there an approach that I can take to decorate the url returned by the method with new parameters. Also, it will be great if the solution is configurable.
Thanks
You can create UrlBuilder class that builds url for you:
and use it in your code
new UrlBuilder().path("http://google.com").params("q", "java").build()