开发者

Accessors' design

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() different开发者_如何学JAVAly with a number of accessors. The accessors use both IChunk and IDatabase interfaces.

  1. What will be the best placement for accessor creation? Would it be correct to extend IDatabase interface 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 index is chunk's index and ACC_ID_x is an accessor interface ID.

  2. 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?

  3. Which design pattern should I look at to implement accessors' factory? My intention is to permit adding accessors (and corresponding accessor ID) dynamically.

  4. 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?

class IAccessor
{
protected:
  IAccessor (IDatabase *database, int index) : m_database (database), m_index (index) {}
  IDatabase *m_database;
  int m_index;
};

class Accessor1 : public IAccessor
{
public:
  Accessor1 (IDatabase *database, int index) : IAccessor (database, index) {}
};

and then:

Accessor1 a1 (database, index);
some_value = a1.SomeFunction ();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜