简体   繁体   中英

Safe polymorphism practice?

I am abit unsure what to title this question as, so other people might have use of it, but I have this simple example of polymorphism from my teacher, which I attempted to modify abit.

But I am unsure whether my modification is "safe" or not.

public class AppSystem {
   ...
   private DataPersistenceInterface DataDAO;
   private DataController DataController;
   ...

   public void createConnection(String username, String password) 
                                throws ClassNotFoundException, SQLException {

      if(username.isEmpty() || password.isEmpty()) {
           DataDAO = new DataDAO();
           DataController = new DataController(DataDAO);
         } else {
           DataDAO = new DataDAO(url, username, password, driver);
           DataController = new DataController(DataDAO);
         }
      }

   public void closeConnection() {
        DataDAO.closeConnection();
   }

We have a controller and an DAO. The DAO implements an interface called DataPersistenceInterface, which hosts a few methods needed to do some communication with the database. Since the datacontroller handles all the logic, and we don't want it to know about anything else, we pass it a reference of the DAO in the type of the interface.

This is what my teacher did. However, the below method "closeConnection" would not work because of this, since the reference to DataDAO does not point to any "closeConnection" method in the DAO class...

[closeConnection method does not work in the above code, obviously]

Now, my idea was to just change...

   private DataPersistenceInterface DataDAO;

to

   private DataDAO DataDAO;

Since the datacontroller takes an argument of DataPersistenceInterface in it's constructor, it doesn't get to know about anything else from the DAO object. And now I would be able to call closeConnection on the DAO.

But I am not sure if this is "safe" to do? Something just tells me it isn't.

Thank you for your time.

first of all, for code clarity, instead of writing

   private DataPersistenceInterface DataDAO;

write :

   private DataPersistenceInterface dataPersistenceInterface;

Always use camel case for java properties and with the same name of the class you are using. A best pratice would also be to name the implementation the same name of the interface plus a Impl:

DataPersistenceInterfaceImpl

It is a pattern problem, if you want to create a connection using the constructor of the Dao, you have three choices:

1) You can add a closeConnection method to the DataPersistenceInterface interface by adding it to the interface signature:

public void closeConnection();

2) But because the AppSystem class does know which implementation it uses to create the DataPersistenceInterface (DataDao), you can cast back the interface to the implementation. You will have your closeConnection() back. This would be my best guest.

   public void closeConnection() {
        ((DataDAO)dataPersistenceInterface).closeConnection();
   }

3) Depending of the usage of your AppSys, don't care about closing the connection. Put the close connection method in the finalize() method of the DataDAO implementation. The connection will be close when the AppSys application will end.

protected void finalize() throws Throwable {
try{
connection.close;
}catch(Exception e){
...
}
}

The best pratice would be to use a ConnectionPool like c3po, or Apache ConnectionPool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM