It seems that if I strictly adhere to the Single Responsibility principle, then I may have to add public getters in order to split functionalities that may change. To me, this seems to violate encapsulation by exposing the objects internal structure.
For example, assume I have a class that represents an uploaded file. In the past I have used methods like toHtml() in order to avoid getters and maintain encapsulation:
public class UploadedFile() {
private String filename;
private String uri;
public String toHtml() {
return <html string>;
}
But I could see where one could argue that, based on SRP, you might want to include getters for filename and uri and generate the html elsewhere.
In this situation, filename
and uri
would be properties of your UploadedFile()
class that it would be illogical to keep hidden, so providing getters for those doesn't violate encapsulation at all, in my opinion.
The encapsulation violation would occur if you made filename
and uri
public fields and just accessed them directly.
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