处理桥接模式之间的依赖性

时间:2018-09-02 02:09:49

标签: c++ image user-interface bridge

我正在尝试设计一个简单的棋盘游戏,该游戏应该与可以在运行时更改的不同GUI库一起使用。因此,我的想法是我想拥有多个类,每个类都具有一个表示该类对象的图像对象。但是由于每个库都选择将图像包装在特定于该库的类中,因此我必须编写一个桥接层次结构来抽象任何实现细节。像这样

class myImageInt{
    // common pure virtual methods that I want my image implementation to define
}

class myImageImpA : myImageInt{
    libAImg img;
    libAImg getLibraryImp(){ return img;}
// provide definition for common interface in terms of libAImg functions
}

class myImageImpB: myImageInt{
    libBImg img;
    libBImg getLibraryImp(){ return img;}
// provide definition for common interface in terms of libBImg functions
}

然后我当然也必须对实际处理加窗和绘制的库类型使用桥层次结构

class myScreenInt{
     void drawImage(myImageInt& img) = 0;
// common methods for drawing onto a window
}

class myScreenImpA : myScreenint{
    libAScreen scrn;
    void drawImage(myImageInt& img){
    // pass the actual library specific object to the libAScreen  so that the image can be drawn
    }
// provide definition for common interface in terms of libAScreen functions
}

class myScreenImpB : myScreenint{
    libBScreen scrn;
   void drawImage(myImageInt& img){
       // pass the actual library specific object to the libBScreen  so that the image can be drawn
   }
// provide definition for common interface in terms of libBScreen functions
}

但是问题是要将图像绘制到窗口上,我将不得不在drawImage对象之一上调用scrn方法,并且这些对象需要特定于其实际实现图像对象(即libAImg和LibBImg类型的对象)。无论如何,是否能够实现这种形式的库独立性而又不会导致类型不安全的static_cast或昂贵的dynamic_cast来推卸继承树并使用一个{返回特定于该实现的类型?

getLibraryImp

0 个答案:

没有答案