T
Terry
Hello. I'm beginning to work with patterns and would really appreciate
some of the group's feedback regarding a problem I'm solving. I'm
looking for a way to abstract a persistence layer interface in a way
that would require little or no modification of the rest of the
application if the underlying storage medium were to change. Pretty
standard, no?
Currently, I've implemented the persistence layer as a package of three
interface files. The main data store interface holds all the usual
methods such as getBizObject(), removeBizObject( id ), saveBizObject(
bizObj ), etc. A second interface is a BizObject iterator, and the
third contains misc utilities. I've implemented a DB store by extending
these three interfaces, the idea being that when the data store medium
changes, we have only to extend the interface files again and the
application has no knowledge that its data is persisted in another
medium.
This works of course, but it doesn't strike me as a particularly elegant
solution.
My requirements are these:
- The application is currently a web app, but could be moved to SOAP or
EJB so I don't want to rely on a container.
- The number of classes comprising the persistence layer may change
- The underlying data store will change often.
What I'm after is this:
- I'd like for changes to the store medium be completely transparent and
unobstructive to the rest of the application code.
- I'd like to have implemented persistence "packages" (implementations
of the above mentioned interface files) be swappable - perhaps bundled
in a JAR file which is then placed in the app's classpath.
- Changing persistence "packages" should be configured outside the
application, such as in a properties file with no changes to application
code.
I think I'm close, but I'm still missing a few key pieces. For example,
how do I get an instance of one of the persistence classes? In the case
of the main data store, I'd want it to be a singleton so it doesn't
incur the overhead of setting up the connection each time I want to use
it, and I don't know what the class name will be ahead of time for
persistence "packages" I haven't written yet. Is this where the factory
pattern is useful? Can the factory get the appropriate class type from
the configuration file, instantiate a singleton and return it? Is there
another pattern well-suited to this situation?
I'm still pretty new to patterns and Java, so I'd really appreciate any
feedback you can offer.
Thanks!
Terry.
some of the group's feedback regarding a problem I'm solving. I'm
looking for a way to abstract a persistence layer interface in a way
that would require little or no modification of the rest of the
application if the underlying storage medium were to change. Pretty
standard, no?
Currently, I've implemented the persistence layer as a package of three
interface files. The main data store interface holds all the usual
methods such as getBizObject(), removeBizObject( id ), saveBizObject(
bizObj ), etc. A second interface is a BizObject iterator, and the
third contains misc utilities. I've implemented a DB store by extending
these three interfaces, the idea being that when the data store medium
changes, we have only to extend the interface files again and the
application has no knowledge that its data is persisted in another
medium.
This works of course, but it doesn't strike me as a particularly elegant
solution.
My requirements are these:
- The application is currently a web app, but could be moved to SOAP or
EJB so I don't want to rely on a container.
- The number of classes comprising the persistence layer may change
- The underlying data store will change often.
What I'm after is this:
- I'd like for changes to the store medium be completely transparent and
unobstructive to the rest of the application code.
- I'd like to have implemented persistence "packages" (implementations
of the above mentioned interface files) be swappable - perhaps bundled
in a JAR file which is then placed in the app's classpath.
- Changing persistence "packages" should be configured outside the
application, such as in a properties file with no changes to application
code.
I think I'm close, but I'm still missing a few key pieces. For example,
how do I get an instance of one of the persistence classes? In the case
of the main data store, I'd want it to be a singleton so it doesn't
incur the overhead of setting up the connection each time I want to use
it, and I don't know what the class name will be ahead of time for
persistence "packages" I haven't written yet. Is this where the factory
pattern is useful? Can the factory get the appropriate class type from
the configuration file, instantiate a singleton and return it? Is there
another pattern well-suited to this situation?
I'm still pretty new to patterns and Java, so I'd really appreciate any
feedback you can offer.
Thanks!
Terry.