MVP中的组合与继承 [英] Composition vs Inheritance in MVP

查看:167
本文介绍了MVP中的组合与继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVP模式开发大型应用程序。在开发工作中,我提出了是否应该使用组合或继承的问题。例如:假设我有一个名为 Foo 的表单,字段为 A B 。在应用程序的其他部分,我有一个表单 Bar 具有相同的字段 A B ,但另外一个字段 C



目前,我的代码是用继承方法编写的,其中视图形式 Bar 继承自 Foo 即可。演示者然后处理与模型有些不同的数据。这样做很简单,但是打败我是否遵循是A的经验法则,因为即使形式不同,他们处理常用输入(A和B)。



然而,在这里,我一直在想组合继承和 Liskov替代原则,并认为我应该使用组合而不是继承。然而,由于我使用MVP比预期更复杂,因为我必须要有一个表单 Foo 的演示者,其中字段 A B 然后是的演示者,其中包含 C ,并引用 Foo 的演示者,以便它可以注入字段一个 B



问题是它已被证明是更多的代码,因为我必须添加在 Foo 的演示者中,可以将数据传递到,某些排序的getter和setter。这样的感觉就像是打破MVP以提供组合。



所以我的问题是:



对于我的案例来说,使用组合是否比继承更好?为什么?



使用组合breakMVP?

解决方案


我的情况是否比使用组合继承更好?为什么?


是的。因为组合在更大的应用程序中更可靠,更安全,更易于维护,更易于发现,更易于记录,更易于理解。恕我直言。 :


使用组合breakMVP?


是的。它打破了你现在正在做的简单MVP。组合可以让您选择如何耦合您的代码,这对于较大的应用程序来说非常有用。它确实使用更多的代码,因为您必须具体了解如何耦合。



一个简单的应用程序成长是非常合理的,并成为一个很好的候选人从简单的MVP继承转变为更复杂的组合。这是一个解耦步骤,可以以新的方式进行重新配置。



这类似于许多简单的网络应用程序正在转换为前端/后端API驱动的应用程序。这实质上是前端用户视图与后端存储模型的解耦。


I'm using MVP pattern to develop a large scale application. While working in the development I have come up with the question whether if composition or inheritance should be used. For example: Let's assume that I have a form called Foo with fields A and B. In other part of the application I have a form Bar that has the same fields A and B but an additional field C.

Currently, I the code is written with the inheritance approach in where the view the form Bar inherits from form Foo. The presenters then handle the data a little different with the model. This works out pretty simply but beats me whether if follows the rule of thumb of "is A" since even when the forms are different they handle common inputs (A and B).

However, here I've been thinking of "composition over inheritance" and the Liskov Substitution Principle and come to think that I should be using composition instead of inheritance. However since I'm using MVP it have been more complicated than expected because I'll have to have a presenter for form Foo with fields A and B then a presenter for Bar with with field C and a reference to the presenter of Foo so that it can inject the fields A and B into it.

The problem is that it it has proven to be more code since I will have to add some sort getters and setters in the presenter of Foo for it to be able to pass the data to Bar. This feels somehow like if I am breaking MVP in order to provide composition.

So my questions are:

Is it really better for my case to use composition over inheritance? Why?

Does using composition "break" MVP?

解决方案

Is it really better for my case to use composition over inheritance? Why?

Yes. Because composition is more reliable, more secure, more maintainable, more discoverable, more documentable, and more comprehensible in larger apps. IMHO. :)

Does using composition "break" MVP?

Yes. It breaks the kind of simple MVP you're doing now. Composition lets you choose how to couple your code, and this is very good for larger apps. It does use more code because you have to become specific about how you're coupling.

It is very reasonable for a simple app to grow, and to become a good candidate for a transition from simple MVP inheritance to more sophisticated composition. This is a decoupling step that enables recoupling in new ways.

This is similar to how many simple web apps are transitioning to become front/back API-driven apps. This is essentially a decoupling of the front-end user views from the back-end storage models.

这篇关于MVP中的组合与继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆