We have an inhouse C++ SDK that exposes fixed interface to read and write JPEG, TIFF & PDF files. There is a sniffer class that tells us which type of file type it is and based on that we create the instance of FileFormat (JPPEG, TIFF, PDF) class using a factory. The problem is that for PNG we have taken open source implementation and it doesn’t conform to our existing C++ interface. Please suggest what you believe is the best approach.
Is it possible to add PNG file type also to the existing factory since we already have the implementation for the same? Pls don't ask me the code, i don't know... :)
ReplyDeleteWe can have a PNG class derived from the base class as in other cases and wrap the PNG library functionality with in this class i.e. having PNG library object/pointer as an member varaiable of this class and use this object to do the actual tasks basically providing a adapter class as per new hierarchy of ours and underlying delegate the tasks to PNG library object/pointer.
ReplyDeleteWrap an existing class with a new interface.
ReplyDeleteAdapter design pattern.
Add PNG class to existing factory.
ReplyDeleteSo now based on type we can get PNG obj,
Call Read & Write of PNG class( as SDK is not supporting for PNG type)
This is a case of Adapter.
ReplyDeleteclass IInterface
{
ReadImage() = 0;
WriteImage() = 0;
}
class PNGInterface : IInterface
{
private:
PNGAdaptor *m_pPNGAdaptor;
public:
ReadImage()
{
m_pPNGAdaptor->ReadImage();
}
WriteImage()
{
m_pPNGAdaptor->WriteImage();
}
};
class PNGAdapter
{
private:
PNGImpl *m_pImpl;
public:
ReadImage()
{
m_pImpl->ReadPNG();
}
WriteImage()
{
m_pImpl->WritePNG();
}
}