Here is my take wherein I pass reader and writer instances to read()
and write()
methods, this seems to achieve good level of decoupling and yet provides flexibility to pick various readers and writers.
Code uses Java-like syntax
Declare a Reader
interface, we will assuming multiple implementation such KMLReader
,
ShapeFileReader
, etc
interface Reader {
GpsFeatureCollection read();
}
Declare a Writer
interface, we will assuming multiple implementation such KMLWriter
, ShapeFileWriter
, etc
interface Writer {
void write(GpsFeatureCollection c);
}
Let's declare GpsFeatureCollection
class to have read
and write
methods which accept respective interfaces as parameter to perform the job.
class GpsFeatureCollection {
...
public static GpsFeatureCollection read(Reader r) {
return r.read();
}
public static void write(Writer w) {
w.write(this);
}
}
Some example of usage using different readers and writers.
// Reading data
GpsFeaureCollection data = GpsFeatureCollection.read(new ShapeFileReader("/tmp/shapefile"));
// Writing data
data.write(new KMLWriter("/tmp/kmlfile"));