OO php 中代码的改进

发布于 2024-10-26 17:29:58 字数 170 浏览 4 评论 0原文

我的项目使用 php OO。我的应用程序中有很多家庭在 7 左右。所以我必须使用 switch 并在每个需要检查家庭的文件中执行此操作。所以我觉得表演受到了影响。那么有什么方法可以让我消除这些开关盒并使用一些设计模式或类似的东西。目前的想法是为每个系列都有一个单独的代码库,我对此并不完全满意。因此寻找各种选择来解决这个问题。

I use php OO for my project. I have lots of families in my application around 7. So i have to use switch and do it in each file where i need to check the family. So i feel the performance is effected because of this. So is there any way so that i can eliminate these switch cases and use some design patters or something like that. The present idea is to have a separate code base for each family, which i am not satisfied with completely. So looking for various options to solve this issue.

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

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

发布评论

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

评论(2

逐鹿 2024-11-02 17:29:58

我只能引用 Martin Fowler 的《重构:改进现有代码的设计》一书中的内容。

切换语句

最明显的症状之一
面向对象的代码是它的
相对缺乏开关(或外壳)
声明。开关的问题
陈述本质上是
复制。很多时候你会发现同样的情况
switch 语句分散在 a 周围
程序在不同的地方。如果你
添加一个新的子句到开关,你
必须找到所有这些开关,
声明并更改它们。这
面向对象的概念
多态给你一种优雅的方式
来解决这个问题。

大多数时候你会看到 switch 语句
你应该考虑多态性。这
问题是多态性应该在哪里
发生。通常是 switch 语句
打开类型代码。你想要的
承载该类型的方法或类
代码值。所以使用提取方法
提取 switch 语句,然后
Move Method 将其放到类中
需要多态性的地方。在
到那时你必须决定是否
用子类替换类型代码
或将类型代码替换为
状态/策略。当你设置完成后
继承结构,你可以使用
用多态性替换条件。

考虑购买并阅读这本书,它是我读过的最珍贵的作品之一。

I can only quote Martin Fowler from his book Refactoring: Improving the Design of Existing Code.

Switch Statements

One of the most obvious symptoms of
object-oriented code is its
comparative lack of switch (or case)
statements. The problem with switch
statements is essentially that of
duplication. Often you find the same
switch statement scattered about a
program in different places. If you
add a new clause to the switch, you
have to find all these switch,
statements and change them. The
object- oriented notion of
polymorphism gives you an elegant way
to deal with this problem.

Most times you see a switch statement
you should consider polymorphism. The
issue is where the polymorphism should
occur. Often the switch statement
switches on a type code. You want the
method or class that hosts the type
code value. So use Extract Method to
extract the switch statement and then
Move Method to get it onto the class
where the polymorphism is needed. At
that point you have to decide whether
to Replace Type Code with Subclasses
or Replace Type Code with
State/Strategy. When you have set up
the inheritance structure, you can use
Replace Conditional with Polymorphism.

Consider buying and reading this book, it's one of the most precious works I have read.

天邊彩虹 2024-11-02 17:29:58

该模式将是使用多态性进行条件重构

我们的想法是将这些不同的家庭类型变成自己的对象。链接的文章摘自 Martin Fowler 的书重构:改进现有代码的设计< /a> 这已经在其他答案中引用了。事实上,链接的网站包含本书的全部内容。

更多信息请访问 http://www.refactoring.com/

That pattern would be Refactor Conditional With Polymorphism.

The idea is to make those various family types into objects of their own. The linked article is taken from Martin Fowler's book Refactoring: Improving the Design of Existing Code that was already quoted in that other answer. In fact, the linked site has the entire contents of the book.

Additional information can be found at http://www.refactoring.com/

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