父类与其派生类进行通信的设计模式
我额外说的是派生类而不是子类。
我有一个基类和许多派生类。一个派生类应该调用基类上的方法,而基类又调用每个派生类上的方法。
这怎么可能? 通信
我应该采用什么设计模式来实现使用 MVVM 设计模式驱动 UI 的控制器之间的 “通道”?我知道中介者
模式,但不太喜欢它,因为它模糊了架构的边界。
I extra said derived classes and NOT children classes.
I have a base class and many derived classes. One derived class should call a method on the base class which again is calling a method on each derived class.
How is that possible? What design pattern should I go for implemention a communication
"channel" between my Controllers driving the UI using MVVM design pattern? I know of mediator
pattern but do not like it much as it blurs the borders of an architecture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它并不是真正的设计模式,而是真正对虚拟方法调用的理解。假设我有一些东西:
并且我有一个派生类:
在上面的示例中,对基类上的
DoFoo
的任何调用都会对基类的DoFooInternal
方法进行虚拟调用派生类。如果我想提供一个基线实现,我还可以将我的DoFooInternal
定义为虚拟:对于您的控制器,这将是相同的,您可以在
中指定一些常见的共享逻辑ControllerBase
实例并派生一个子控制器,例如PeopleController
,它可以将方法调用分派给基类,而基类又可以将调用分派回派生类中的虚拟方法...It's not really a design pattern, but really an understanding of virtual method calls. Let's say I have something:
And I have a derived class:
In the above example, any call to
DoFoo
on the base class makes a virtual call to theDoFooInternal
method of the derived class. I could also define myDoFooInternal
as virtual, if I wanted to provide a baseline implementation:In the case of your controllers, this would be the same, you can specify some common shared logic in a
ControllerBase
instance and derive a child controller, e.g.PeopleController
which can despatch method calls to the base class, which can in turn despatch calls back to virtual methods in the derived class...要将责任从超类“委托”给其子类,您可能正在寻找模板方法模式< /a>.
它基本上形式化了使用抽象函数(当然,子类必须实现)的概念,以允许基类调用其具体子类上的函数。
For "delegating" responsibility from a superclass to its subclass(es), you might be looking for the Template Method Pattern.
It's basically formalizing the concept of using abstract functions (which subclasses must implement, of course) to allow a base class to call a function on its concrete subclass.
我认为命令模式最适合您的问题。如果您的场景是这样的:
那么最好的模式是命令模式。
这篇文章可以给你一些想法
但我的建议是使用接口而不是基础-派生类。
I think Command Pattern suits best for your problem. If you scenario is this:
then the best pattern is Command Pattern.
This article can give you some idea
But my suggestion is to use interfaces instead of base-derived classes.
您可以使用事件在视图模型之间进行通信。或者
MVVM 中介模式
You can use Events to communicate between the viewmodels. OR
MVVM Mediator Pattern