Given I have
class IChunk
{
public:
const char* getRawData() = 0;
...
};
class Chunk
{
public:
const char* getRawData();
...
private:
char* data;
};
class IDatabase
{
public:
IChunk* getChunk( int index ) = 0;
...
};
class Database : public IDatabase
{
public:
IChunk* getChunk( int index );
...
private:
std::vector< Chunk > m_chunks;
};
I would like to interpret the raw data returned by getRawData() differently with a number of accessors. The accessors use both IChunk and IDatabase interfaces.
-
What will be the best placement for accessor creation?
Would it be correct to extendIDatabaseinterface with a creation function like this:IAccessor* a = database->createAccessor( ACC_ID_1, index ); IAccessor1* a1 = dynamic_cast< IAccessor1* >a; IAccessor* a = database->createAccessor( ACC_ID_2, index ); IAccessor1* a2 = dynamic_cast< IAccessor2* >a;where
indexis chunk’s index andACC_ID_xis an accessor interface ID. -
The dynamic cast will be required in this case, which might be not safe and the design is not very good. Can I improve it?
-
Which design pattern should I look at to implement accessors’ factory? My intention is to permit adding accessors (and corresponding accessor ID) dynamically.
-
Would it be OK to implement accessor creation by something like this:
IAccessor3* a3; database->createAccessor( ACC_ID_3, index, (void**)&a3 );
Why not pass the database object and chunk index as constructor parameters to the accessors?
and then: