A good practice is to has business logic inside the close method (AutoCloseable interface) and not invoke the logic manually but via try-with-resource ?
I have a scenario where I write stream way to XML file with use of jax-b and I need to manually write start tag element and start end element. Business logic is in close method but I think is not good practice and what is your opinion?
My example
public class PhoneBookWriter implements AutoCloseable {
public void open(Path filePath) {
// open file
}
public void write(PhoneBookInfo phoneBookInfo) {
// write phone book pojo to xml file
}
public void close() {
// check if stream is not null
// --> part of business logic -> writeEndElement via XmlOutputWriterStream
// close stream
}
What is you opinion about open and close methods? May the method has business logic or only open stream (open method) and close stream (close method)? Should another logic be extracted to dedicated methods?