[Gdal-dev] GDALDataset as a Base Class
Matt Hanson
mhanson at photon.com
Tue Aug 29 22:50:34 EDT 2006
Interesting, my original implementation had GDALDataset as a private member as you suggest. As I spent more time thinking about it I thought that a derived class would be better in the long run, so I started looking into what was required for that. As it turns out though I had already come to the conclusion that my GSBand class would have to use GDALRasterBand as a private member because of course you can't create raster objects directly. I suppose at the very least it makes sense to have them use the same design, thus GDALRasterBand and GDALDataset as private members.
Thanks for the explanations, it's been a tremendous help!
-matt
________________________________
From: gdal-dev-bounces at lists.maptools.org on behalf of Mateusz Loskot
Sent: Tue 8/29/2006 10:26 PM
To: gdal-dev at lists.maptools.org
Subject: Re: [Gdal-dev] GDALDataset as a Base Class
Mateusz Loskot wrote:
> Matt Hanson wrote:
>> Any ideas on what my derived class
>> constructor should look like then?
>
> I would use "named constructor idiom" and factory method:
> Here is the concept in very short:
>
> class GSImage : public GDALDataset
> {
> public:
> static GSImage* Create(const char* file)
> {
> return static_cast<GSImage*>( GDALOpen(file, GA_ReadOnly ) );
> }
> };
>
> GSImage* pImage = GSImage::create("myfile.png");
Let me unsay it ;-)
The solution above forces you to still play with pointers.
So, with current design of GDALDataset, it's much better to encapsulate
GDALDataset as a private member of GSImage class than inheritance.
Certainly, composition with its direct lifetime control.
GSImage
{
public:
GSImage(const char* filename) : ds(0)
{
ds = GDALOpen(filename, ...);
}
// Add your own or replicate GDALDataset interface
private:
GDALDataset* ds;
}
Certainly, here you need to (re)design your own interface,
as minimal as needed to use GSImage, or replicate whole
interface after GDALDataset <--- may seem as a redundant work
in comparison of encapsulation, but composition is much better
in wide terms of flexibility.
Side note, inheritance is always easier to use, but
composition/aggregation is much more flexible.
Are you sure GSImage is a GDALDatase?
This is the question from the same family like
Is a circle an elipse but with two radiuses of the same length?
If you're interested in considering this design deeply,
I'd suggest you to look at the Liskov Substitution Principle (LSP)
that's pretty simple to understand but helps much with design
decisions :-)
Cheers
--
Mateusz Loskot
http://mateusz.loskot.net <http://mateusz.loskot.net/>
_______________________________________________
Gdal-dev mailing list
Gdal-dev at lists.maptools.org
http://lists.maptools.org/mailman/listinfo/gdal-dev
More information about the Gdal-dev
mailing list