Skip to main content
gamedev.net
🔒 Locked

interface design issues!

Started by Crypter 3 replies 550+ views
Original Post
Crypter
Crypter
Hey everyone, I ran into a design issue, and am looking for solutions. Lets say, I have four classes: SysVideo.h

class cSysPalette
{
// stuff..
}

class cSysSurface
{
 bool SetPalette (cSysPalette& );      // uses cSysPalette
}
// Video.h (interface classes)

class cPalette   // wraps up cSysPalette
{
// stuff..
}

class cSurface   // wraps up cSysSurface
{
 cSysSurface* lpSysSurface;
//...


// My problem...

 bool SetPalette (cPalette& palette)
 {
   lpSysSurface->SetPalette (/* takes cSysPalette not a cPalette!! */);
 }
}
How should I implement cSurface::SetPalette ()? This function calls lpSysPalette->SetPalette, which takes a cSysPalette not a cPalette--which is my problem. How should I convert the cPalette to a cSysPalette? Im trying to keep clean, nice interfaces, but this has got me stumped! Please help! Thanks in advance!
stylin
stylin
Look into the Adapter pattern, specifically created for this situation.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Crypter
Crypter
Thanks alot! That pattern looks like it will
help.

There is still a problem though..

bool cSurface::SetPalette (cPalette& palette){  lpSysSurface->SetPalette (/* takes cSysPalette not a cPalette!! */);}


cPalette has a cSysPalette pointer as private. ie; This is what I need to do:
bool cSurface::SetPalette (cPalette& palette){  lpSysSurface->SetPalette (palette.privateMemberPointer);}

The problem is I cant access this private member..

I dont want to provide a GetPalette() accessor method
in the cPalette interface.. ..Is there another way..?

Thanks!
Glak
Glak
Why not use inheritance? The system versions could be subclasses of the generic versions. Well I am not completely sure that I understand what you are doing, but it sounds like inheritance might be what you are looking for.
Crypter
Crypter

All my classes with sys contain system-specific
code.

I found a way to make it work using the Adapter pattern,
like this:
class cPalette{public:   // pure virtual interface functions};
// the adapter classclass cPaletteAdt : public cPalette{   cSysPalette* lpSysPalette;public:   operator cSysPalette& ()   {      return *lpSysPalette;// dont like doing this..   }//...};

From cPalettes view, the cSysPalette
doesnt exist. This hides cSysPalette, while still
providing a way to access it:

bool cSurface::SetPalette (cPalette& palette){   cPaletteAdt* pal;   pal = (cPaletteAdt*) &palette   return (lpSysSurface->SetPalette (*pal));}


Thanks alot for your help!! I got to clean up the
code a bit, but everythings working good!

ps; if anyone wants to comment on my current layout,
please do so..Im trying to get better at OOP, ADTs,
information hiding, etc.

Thanks a million!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.