Java中的DRY原理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18831919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DRY principle in java
提问by beinghuman
I have been reading DRY principle.Though it seems so easy but I am having difficulty in understanding how we actually achieve it in any Project be it Web Application or Swing? May be few examples will give me lead and help me in developing my thought process. I have tried to search on Google but could not find any thing suitable.
我一直在阅读 DRY 原则。虽然它看起来很简单,但我很难理解我们如何在任何项目中实际实现它,无论是 Web 应用程序还是 Swing?可能很少有例子可以引导并帮助我发展我的思维过程。我试图在谷歌上搜索,但找不到任何合适的东西。
采纳答案by gview
DRY is nothing that requires examples. If you are copying and pasting blocks of code from one method, function or block of code to another, then you are in violation of DRY.
DRY 不需要示例。如果您将代码块从一个方法、函数或代码块复制并粘贴到另一个方法、函数或代码块,那么您就违反了 DRY。
Very simply, DRY tells you to move that code into a reusable unit: either into a function or method or macro, or other mechanism relevant to your programming language. If you have places in your codebase where you clearly have very similar code (even with minor variations) then you should be looking to refactor that code to make it DRY.
很简单,DRY 告诉您将该代码移动到一个可重用单元中:或者放入一个函数、方法或宏,或者与您的编程语言相关的其他机制中。如果您的代码库中有一些地方显然有非常相似的代码(即使有细微的变化),那么您应该寻求重构该代码以使其干燥。
"Don't repeat yourself!" == "Don't write the same code repeatedly"
“不要重复自己!” ==“不要重复写相同的代码”