如何减少代码中的重复 - If 语句与单独的类

发布于 2024-09-25 14:45:27 字数 564 浏览 10 评论 0原文

假设您有一个网站,可让您与最多三个不同的帐户所有者建立帐户。所有者的每个入口页面都是一个单独的 aspx 页面。第一个人的业务规则与第二和第三所有者有很大不同。第一个也将有更多的字段,并且它们将需要不同的字段,但其他字段则不需要。我正在使用 MVP(模型视图演示者)模式

我可以做

A. 制作三个单独的视图,并让它们全部使用一个具有特殊 IF 语句或 Switch 语句的 Presenter,以查看您是否正在与某个所有者打交道,如果是,则以某种方式执行操作。

或者

B. 创建三个单独的视图,并让它们都使用自己的 Presenter,该 Presenter 将按照该所有者的预期方式执行,而不需要任何 IF 语句。

我感到很矛盾,因为 A 似乎是正确的做法,但可能会弄乱到处的所有 IF 语句,人们可以删除或修改它们,而没有意识到后果。尽管 B 看起来重复代码太多,而且看起来很丑。您是否在某个时刻说这些类差异太大而无法共享,并且存在太多用于异常的 IF 语句?

Let's say you have a website that lets you build an account with up to three different account owners. Each entry page for the owner is a separate aspx page. The first person has very different business rules than the second and third owners. The first will have more fields as well and different ones will be required for them but not for the others. I am using the MVP (Model View Presenter) pattern.

I can either do

A. Make three separate Views and have them all use one Presenter that has special IF statements or Switch statements to see if you are dealing with a certain owner and if so to act a certain way.

OR

B. Make three separate Views and have them all use their own Presenter which will just execute the way it is intended for that owner without any IF statements needed.

I feel conflicted because A seems the right thing to do but could get messy with all of the IF statements everywhere and people could delete or modify them without realizing the repercussions. Although B seems like far too much duplication of code and just looks ugly. Is there a point at which you say these classes are too different to be shared and there's too many IF statements for exceptions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2024-10-02 14:45:27

我同意上面的评论。将演示者的通用代码放在基类中,并让三个特定的子演示者继承它。这遵循设计系统时的基本原则之一:“确定应用程序中不同的方面,并将它们与保持不变的方面分开。”

祝你好运。

I would agree with the comment above. Put the common code for your presenter in a base class and have the three specific child presenters inherit from it. This follows one of the basic principals when designing a system: "Identify the aspects of your application that vary and separate them from what stays the same."

Good luck.

迷爱 2024-10-02 14:45:27

不要 A.

按照 Serguei 的建议进行操作,或者使用您正在讨论的重复代码创建一个通用逻辑类。

Don't A.

Do what Serguei suggested or make a common logic class with the duplicated code you are talking about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文