Let's say I have multiple servlets with minor differences in doPost() implementation. Example code here. I want to be able to define function just once and avoid duplication of code among different servlets.
One way is to put common behaviour in a different class altogether and have object of that class in every Servlet to use common behaviour but would that be a good idea?
What are some other alternatives to tackle this issue?
You might create an abstract class that implements Httpservlet and defines all the common methods, but leaves your modifiable (doGet) method abstract.
I think this:
One way is to put common behaviour in an altogether different class and have object of that class in every Servlet to use common functionality but that is not very elegant
is actually quite elegant. It sound like you're describing composition. It reduces code duplication, extracts common functionality into a single component and mitigates the issues you'd get through implementation inheritance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With