我试图将Mixin模式与我的问题相匹配,我有一个多态性问题,我不知道如何有效地解决.在尝试重新设计程序之前,我想问你一些建议(也许有一些我不知道的很酷的c ++功能).
我想以非常直接和简单的方式呈现它,因此这里的用例可能没有意义.
我只是一Window堂课
struct WindowCreateInfo {
std::string title;
int x, y;
int width, height;
};
class Window {
public:
Window(const WindowCreateInfo &createInfo) :
title(createInfo.title),
x(createInfo.x),
y(createInfo.y),
width(createInfo.width),
height(createInfo.height) {}
const std::string &getTitle() const { return title; }
int getX() const { return x; }
int getY() const { return y; }
int getWidth() const { return width; }
int getHeight() const { return height; }
public:
protected:
std::string title;
int x, y;
int width, height;
};
Run Code Online (Sandbox Code Playgroud)
然后我定义了两个mixins Resizable,Movable如下所示
template<class Base>
class Resizable : public Base {
public:
Resizable(const WindowCreateInfo &createInfo) : Base(createInfo) {}
void resize(int width, int height) {
Base::width = width;
Base::height = height;
}
};
template<class Base>
class Movable : public Base {
public:
Movable(const WindowCreateInfo &createInfo) : Base(createInfo) {}
void move(int x, int y) {
Base::x = x;
Base::y = y;
}
};
Run Code Online (Sandbox Code Playgroud)
接下来,我有一些业务层,我在其中处理实例 Window
class WindowManager {
public:
static void resize(Resizable<Window> &window, int width, int height) {
window.resize(width, height);
// any other logic like logging, ...
}
static void move(Movable<Window> &window, int x, int y) {
window.move(x, y);
// any other logic like logging, ...
}
};
Run Code Online (Sandbox Code Playgroud)
这里显而易见的问题是以下不编译
using MyWindow = Movable<Resizable<Window>>;
int main() {
MyWindow window({"Title", 0, 0, 640, 480});
WindowManager::resize(window, 800, 600);
// Non-cost lvalue reference to type Movable<Window> cannot bind
// to a value of unrelated type Movable<Resizable<Window>>
WindowManager::move(window, 100, 100);
};
Run Code Online (Sandbox Code Playgroud)
我知道之间存在差异Movable<Window>,Movable<Resizable<Window>>因为后者Movable可以使用Resizable.在我的设计中,mixin是独立的,它们混合的顺序无关紧要.我想这种mixins的使用很常见.
有没有什么方法可以在尽可能保持设计的同时编译代码?
有没有什么方法可以在尽可能保持设计的同时编译代码?
您可以简单地让窗口管理器接受任意版本Resizable<>并Movable<>通过模板化方法:
class WindowManager {
public:
template<typename Base>
static void resize(Resizable<Base> &window, int width, int height) {
window.resize(width, height);
// any other logic like logging, ...
}
template<typename Base>
static void move(Movable<Base> &window, int x, int y) {
window.move(x, y);
// any other logic like logging, ...
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73 次 |
| 最近记录: |