We have implemented a typical DAO/Abstract Factory pattern. The design is like this:
DAOFactory – that returns either an instance of MySQLFactory / SQLiteFactory
- MySQLFactory – returns DAOs that talks to MySQL DB
- SQLiteFactory – returns DAOs that talks to SQLite DB
Things are fine. However, we need to create two executables: The one that is provided to customers uses the SQLiteFactory instance and relevant DAOs. In that executable, we don’t want to include any class related to MySQLFactory. If I delete those classes then we see a ClassNotFoundException at run time when DAOFactory class is being loaded by class loader.
How can we implement our DAOFactory so that MySQLFactory is not required at runtime ? The same problem also exists for certain other classes i.e. certain classes are required only for in-house version of the app. What is a good way of implementation so that we can exclude classes from the software that is shipped to customers ?
Thanks
Deep
You can use
Class.forNameto load classes at runtime. For example:Then you can call the right method for the specific version of your software, i.e.
loadMySQLFactorywhen you bundle the MySQL implementation andloadSQLiteFactorywhen you bundle the SQLite implementation.You can even pass the class name as a JVM property, for eg.
-DdaoFactory=your.package.MySQLFactoryand then use this to load the right class: