DRY 是否能无误地引导您走上 OOP 设计模式的正道?

发布于 2024-10-03 08:30:09 字数 223 浏览 7 评论 0原文

假设:你的干感官是绝对正确的。代码中的重复行为会让你感到厌烦;这是黑板上的钉子。

问题:将 DRY 放在您的脑海中是否可以保证您会注意到何时应该寻求设计模式?

从我所看到的设计模式介绍来看,似乎一直在解决“需要”重复代码。这是 OOP 的真理吗?

可能更简单的问题:是否有一天,干燥会导致您远离 OOP 设计模式?

Assume: Your DRY senses are infallible. Repeated behavior in your code reeks to you; it's nails upon the chalkboard.

Question: Does keeping DRY in the forefront of your mind guarantee that you will notice when you should be seeking a design pattern?

It seems from what introductions to design patterns I've seen has been working around supposedly "needing" to repeat code. Is this an OOP truth?

The possibly more easy question: Is there ever a time when dry will lead you away from OOP design patterns?

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

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

发布评论

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

评论(4

夏天碎花小短裙 2024-10-10 08:30:09

恕我直言,这取决于具体的模式。如果您心中有 GoF 模式:其中很多模式都旨在“关注点分离”。通过分解出特定的关注点,例如对象创建(工厂模式)或对象克隆(原型模式),您将把处理此关注点的代码部分带到中心位置,使代码更加干燥。

其他 GoF 模式(例如 Flyweight 或 Proxy)具有不同的性质,它们旨在提高效率或降低复杂性。这些模式大多与 DRY 原则正交。

IMHO that depends on the specific pattern. If you have the GoF patterns in mind: lot's of them are aimed at "separation of concerns". By factoring out a specific concern like object creation (Factory pattern) or object cloning (Prototype pattern) you will bring that part of your code dealing with this concern to a central place, making the code more DRY.

Other GoF patterns like Flyweight or Proxy have a different nature, they are aimed at more efficiency or less complexity. Those patterns are mostly orthogonal to the DRY principle.

等风来 2024-10-10 08:30:09

DRY 和 OOP 是正交的。无论是否使用 OO 语言进行编码,都应该练习 DRY。请记住,DRY 不仅仅适用于代码。

DRY and OOP are orthogonal. DRY should be practiced whether or not one is coding in a OO language. Keep in mind that DRY doesn't just apply to code.

携余温的黄昏 2024-10-10 08:30:09

正如其他人所注意到的,DRY 和 OOP 是正交的概念。 DRY 不会让您远离 OOP 设计模式的最终证明是,您可以在使用本身包含(某些)OOP 设计模式的语言时应用 DRY。

As others noticed, DRY and OOP are orthogonal concepts. The ultimate proof that DRY doesn't lead you away from OOP design patterns is that you can apply DRY when using languages which incorporate (some of the) OOP design patterns in themselves.

Oo萌小芽oO 2024-10-10 08:30:09

为什么考虑 DRY 会让您远离 OOP 设计模式。设计模式的存在是为了让你不会重复别人多次犯过的错误,并针对特定问题提出解决方案。因此,没有 DRY 不会让您远离 OOP 设计模式。

Why thinking about DRY lead you away from OOP design patterns. Design patterns are there so that you do not repeat the mistakes other has done many times and come up with solutions for specific problem. So, no DRY does not lead you away from OOP design patterns.

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