MVP设计模式的STL实现
我正在尝试使用 STL 实现 MVP 模式,并且在具有重复引用时使用 *shared_ptr* 和 *weak_ptr* 来“打破循环”。
class i_model;
class i_view;
class i_view
{
public:
i_view() : myModel(NULL) {}
virtual ~i_view() {}
void set_model(const std::shared_ptr<i_model>& _model) { myModel = _model; }
virtual void fire_keyboard(unsigned char key, int x, int y) {}
virtual void on_model_changed() { };
virtual void render() const = 0;
protected:
std::shared_ptr<i_model> myModel;
};
class i_model
{
public:
i_model() : myView() {}
virtual ~i_model() {}
void set_view(const std::shared_ptr<i_view>& _view) { myView = _view; }
void fire_model_changed() { std::tr1::shared_ptr<i_view> p = myView.lock(); p->on_model_changed(); }
protected:
std::weak_ptr<i_view> myView;
};
我仍然有一个问题:如何从 this 指针中获取shared_ptr?我看到boost提出的解决方案但真心认为不是走那么远。问题是,设置 *weak_ptr* 的唯一方法是从共享指针,如果我必须在一个本身没有共享指针的类中执行此操作,那将会很困难。
所以这里基本上是视图创建模型,但模型需要引用视图来实现观察者模式。问题是我被困住了,因为我无法设置模型的weak_ptr 视图指针。
...
void MyView::Create()
{
std::shared_ptr<MyModel> model = std::make_shared<MyModel>();
i_view::set_model(model);
model->set_view(this); // error C2664: cannot convert parameter 1 from MyModel* to 'std::tr1::shared_ptr<_Ty>'
}
...
还有其他办法吗? :) 这就像说我不信任助推者,但事实并非如此。事实上,我的问题是是否有另一种方法可以实现 MVP,而不会首先陷入这种混乱。
PS:我正在尝试实现 MVP 监督控制器模式。在代码示例中,我排除了 i_presenter 接口,编译错误进一步出现。如果我尝试被动视图方法,结果会是一样的。您可以在模型-视图-演示者模式处了解有关它们的更多信息。
I'm trying to implement an MVP pattern using STL and I have used *shared_ptr* and *weak_ptr* for "breaking the cycle" when having recurrent references.
class i_model;
class i_view;
class i_view
{
public:
i_view() : myModel(NULL) {}
virtual ~i_view() {}
void set_model(const std::shared_ptr<i_model>& _model) { myModel = _model; }
virtual void fire_keyboard(unsigned char key, int x, int y) {}
virtual void on_model_changed() { };
virtual void render() const = 0;
protected:
std::shared_ptr<i_model> myModel;
};
class i_model
{
public:
i_model() : myView() {}
virtual ~i_model() {}
void set_view(const std::shared_ptr<i_view>& _view) { myView = _view; }
void fire_model_changed() { std::tr1::shared_ptr<i_view> p = myView.lock(); p->on_model_changed(); }
protected:
std::weak_ptr<i_view> myView;
};
Still I have one question: how can I get a shared_ptr out of a this pointer ? I saw the solution proposed by boost but sincerely think not going that far. The thing is that the only way to set a *weak_ptr* is from a shared_ptr and if I have to do this within a class that doesn't have a shared_ptr to itself, it's going to be hard.
So here basically the view creates the model but the model needs to reference back the view to implement the Observer pattern. The problem is that I'm stuck because I cannot set the weak_ptr view pointer for the model.
...
void MyView::Create()
{
std::shared_ptr<MyModel> model = std::make_shared<MyModel>();
i_view::set_model(model);
model->set_view(this); // error C2664: cannot convert parameter 1 from MyModel* to 'std::tr1::shared_ptr<_Ty>'
}
...
Is there any other way ? :) This is like saying that I don't trust the boost guys but it's not that. In fact, my question would be if there is another way to implement MVP without getting into this mess in the first place.
PS: I'm trying to implement the MVP Supervising Controller pattern. In the code sample I've excluded the i_presenter interface, the compiling error being further up. It would have been the same if I would have tried the Passive View approach. You can read more about them here Model-View-Presenter Pattern.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
shared_from_this
可能会有所帮助:http:// www.boost.org/doc/libs/1_40_0/libs/smart_ptr/enable_shared_from_this.htmlshared_from_this
may help: http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/enable_shared_from_this.html