什么时候打电话给*? [何时使用设计模式?]

时间:2023-02-05 14:15:33

In The Guerilla Guide to Interviewing Joel says that guys who want to get things done, but are not smart will do stupid things like using a visitor design pattern where a simple array would be sufficient.

在The Guerilla采访指南Joel说,想要完成任务但不聪明的人会做一些愚蠢的事情,比如使用一个简单的阵列就足够的访客设计模式。

I find it hard to detect, if the design pattern suggested by the Gang of Four should be applied.

如果应该使用Gang of Four建议的设计模式,我发现很难检测到。

Therefore, I would like some examples from Your work experience

因此,我想从您的工作经验中得到一些例子

  • When is a simple approach (fixed size array) sufficient?
  • 什么时候一个简单的方法(固定大小数组)足够?

  • What is the minimum size of a piece of software that justifies the use of the GoF patterns?
  • 一个软件的最小尺寸是多少,证明使用GoF模式是正确的?

  • When to refactor from simple-minded to GoF? Can this be done in a sensible way?
  • 什么时候从简单的重构到GoF?这可以以合理的方式完成吗?

7 个解决方案

#1


18  

I often find that using test driven development helps guide me when faced with these questions.

我经常发现,在面对这些问题时,使用测试驱动开发有助于指导我。

  • When is a simple approach sufficient? It is always sufficient to use the simplest approach to get the next test to pass. But knowing when/how to refactor is the real art form.
  • 什么时候一个简单的方法足够?使用最简单的方法来完成下一个测试总是足够的。但知道何时/如何重构是真正的艺术形式。

  • What is the minimum size of a piece of software that justifies the use of the GoF patterns? A rule of thumb I once read is that when you code something once, fine, when you duplicate that code somewhere a second time, make a note and move on. When you find a need for the same code a third time, it's time to refactor to remove duplication and simplify, and often that involves moving to a design pattern.
  • 一个软件的最小尺寸是多少,证明使用GoF模式是正确的?我曾经读过的一条经验法则是,当你对某些东西进行一次编码时,很好,当你再次在某处复制该代码时,请记下并继续前进。当您第三次发现需要相同的代码时,是时候重构以消除重复和简化,并且通常涉及转移到设计模式。

  • When to refactor from simple-minded to GoF? I like what @anopres said - it's time when you feel the pain of not having the design pattern in place. The pain (or code "smell") may manifest itself in several ways. Code duplication is the most obvious. Refactoring books like Fowler's Refactoring or Kerievsky's Refactoring to Patterns list many such pain points/code stenches.
  • 什么时候从简单的重构到GoF?我喜欢@anopres所说的 - 现在是时候感受到没有设计模式的痛苦了。疼痛(或代码“气味”)可能以多种方式表现出来。代码重复是最明显的。重构像Fowler的Refactoring或Kerievsky的Refactoring to Patterns等书籍列出了很多这样的痛点/代码。

  • Can this [refactoring] be done in a sensible way? The trick to refactoring is to have a suite of unit tests in place which you have confidence in, and then to refactor without causing any of those tests to fail. Refactoring, by definition, does not change the functionality of your code. Therefore, if your tests continue to pass, you can have a pretty good feeling that you didn't break anything. Although it can be difficult, I actually enjoy this part of TDD, it's almost like a game to make changes without breaking any tests.
  • 这种[重构]能否以合理的方式完成?重构的技巧是让你有信心的一套单元测试,然后重构而不会导致任何这些测试失败。根据定义,重构不会改变代码的功能。因此,如果您的测试继续通过,您可以非常好地感觉到您没有破坏任何东西。虽然这可能很难,但实际上我喜欢TDD的这一部分,它几乎就像一个游戏,在不破坏任何测试的情况下进行更改。

In summary, I would say that TDD helps guide me to write the code that is sufficient at the time, and perhaps more importantly helps me to make the changes later when inevitably requirements change, more functionality is required, etc.

总之,我会说TDD有助于指导我编写当时足够的代码,或许更重要的是帮助我在以后不可避免地需求变更,需要更多功能等时进行更改。

#2


11  

Design Patterns are a consequence, not an objective. You don't think today I shall use Strategy Patterns, you just do it. Halfway through doing the third nearly identical class you stop and use a paper notebook to figure out the general case and knock up a base class that describes the shared context. You refactor the first two classes to be descendants, and this gives you a reality check and quite a few changes to your base class. Then the next thirty are a walk in the park.

设计模式是一种结果,而不是目标。你今天不认为我会使用策略模式,你只是这样做。在完成第三个几乎完全相同的类的过程中,你停下来并使用纸质笔记本来找出一般情况并敲掉描述共享上下文的基类。你重构前两个类是后代,这给你一个现实检查和你的基类的一些变化。然后接下来的三十个人在公园散步。

It's only the next day at the team meeting that you save everyone thirty minutes of boredom by saying "I used strategy pattern. They all work the same so there's only one test program, it takes parameters to change the test case."

只是在团队会议的第二天,你通过说“我使用了策略模式来节省每个人30分钟的无聊。他们都工作相同所以只有一个测试程序,它需要参数来改变测试用例。”

Intimate familiarity with patterns makes you use them reflexively, whenever the situation demands it. When people treat the use of patterns as an objective in its own right, you get stilted, ugly code that speaks of mechanism rather than purpose; the how rather than the why.

对模式的熟悉使您可以在情况需要时反复使用模式。当人们将模式的使用本身视为一个目标时,你就会得到笨拙,丑陋的代码,它们讲的是机制而不是目的;怎么而不是为什么。


Most patterns address recurring fundamental problems like complexity mitigation and the need to provide extensibility points. Providing extensibility points when its clear they won't be needed pointlessly complicates your code and creates more failure points and test cases. Unless you're building a framework for release into the wild, solve only the problems you actually face.

大多数模式解决了复杂的缓解和提供可扩展性点的需要等重复出现的基本问题。提供可扩展性点时,无需明确它们就会使代码变得复杂,并创建更多的故障点和测试用例。除非你构建一个框架以便释放到野外,否则只解决你实际面临的问题。

#3


10  

Patterns are only tools and vocabulary. You write the code to be as simple, understandable and maintainable as you know how. By knowing patterns you have more allternatives at your disposal, and you have a language to discuss pros and cons of the approach before implementing it.

模式只是工具和词汇。您可以将代码编写为您知道的简单,易懂和可维护的代码。通过了解模式,您可以使用更多的替代品,并且在实施之前,您可以使用一种语言来讨论该方法的优缺点。

In either case you dont just "switch" to "using a pattern". You just keep doing what you allways do, write the code the best way you know how.

无论哪种情况,你都不要只是“切换”到“使用模式”。你只是继续做你总是做的事情,以你知道的最佳方式编写代码。

#4


7  

This is similar to any other design decision. Ultimately, it depends. You should learn those patterns that are useful in your language (many GoF patterns aren't needed in Lisp or Smalltalk, for example), learn their advantages and disadvantages, understand the constraints of your system, and make the choice that best fits your needs.

这与任何其他设计决策类似。最终,这取决于。您应该学习那些对您的语言有用的模式(例如,Lisp或Smalltalk中不需要许多GoF模式),了解它们的优点和缺点,理解系统的约束,并做出最适合您需求的选择。

The best advice that I can give is to learn, learn, learn.

我能给出的最好的建议是学习,学习和学习。

#5


3  

Switching from a simple approach to a formal design pattern is usually something that happens fairly naturally for me as a problem increases in complexity. The key is to be familiar enough with the patterns that you can recognize the tipping point and switch from the simple approach to a design pattern when it will bring the most benefit for current and future development.

从简单的方法切换到正式的设计模式通常是我自然而然地发生的事情,因为问题会增加复杂性。关键是要熟悉可以识别引爆点的模式,并从简单的方法切换到设计模式,这将为当前和未来的开发带来最大的好处。

For a larger, more complex project, the tipping point should be fairly early on; in many cases, before you even start coding. For smaller projects, you can afford to wait before deciding to implement a pattern.

对于更大,更复杂的项目,引爆点应该是相当早的;在许多情况下,在你开始编码之前。对于较小的项目,您可以在决定实施模式之前等待。

One of the best things you can do to increase your ability to recognize when a pattern should be used is to take some time after completing a project to examine how complex your "simple" approach has become. If it would have taken you less time and effort to implement a pattern, or if the pattern would clarify what you were trying to do, you can file that knowledge away for the next time you encounter a similar problem.

为了提高识别应该使用模式的能力,您可以做的最好的事情之一是在完成项目后花一些时间来检查“简单”方法的复杂程度。如果它花费您更少的时间和精力来实现模式,或者如果模式会澄清您尝试做什么,那么您可以在下次遇到类似问题时提交该知识。

#6


2  

When you have a problem that one of the patterns solves. The GoF book has a section in each chapter that explains what types of scenarios each pattern is appropriate for. You should not analyze each problem you have, then go look up what pattern to use. You should become familiar with the patterns so that you learn to recognize what situations call for them.

当您遇到其中一个模式解决的问题时。 GoF书中的每一章都有一节介绍了每种模式适合的场景类型。您不应该分析每个问题,然后查看要使用的模式。您应该熟悉这些模式,以便学会识别哪些情况需要它们。

#7


0  

One of the nice things about the the original GoF book is that there is discussion of the scenarios where the pattern would best solve problem. Reviewing these discussions can help you determine if "it's time".

关于原始GoF书籍的一个好处是,讨论了模式最能解决问题的场景。查看这些讨论可以帮助您确定“是时候”了。

Another good place to start is with Head First Design Patterns. The exercises that illustrate the use of different design patterns are elaborate enough to offer a good learning experience. In addition the exercises are also grounded in real world scenarios, so it's never a stretch to see when the appropriate times to apply design patterns.

另一个好的起点是Head First Design Patterns。演示使用不同设计模式的练习非常精细,足以提供良好的学习体验。此外,练习也基于现实世界的场景,因此在适当的时候应用设计模式并不是一件容易的事。

#1


18  

I often find that using test driven development helps guide me when faced with these questions.

我经常发现,在面对这些问题时,使用测试驱动开发有助于指导我。

  • When is a simple approach sufficient? It is always sufficient to use the simplest approach to get the next test to pass. But knowing when/how to refactor is the real art form.
  • 什么时候一个简单的方法足够?使用最简单的方法来完成下一个测试总是足够的。但知道何时/如何重构是真正的艺术形式。

  • What is the minimum size of a piece of software that justifies the use of the GoF patterns? A rule of thumb I once read is that when you code something once, fine, when you duplicate that code somewhere a second time, make a note and move on. When you find a need for the same code a third time, it's time to refactor to remove duplication and simplify, and often that involves moving to a design pattern.
  • 一个软件的最小尺寸是多少,证明使用GoF模式是正确的?我曾经读过的一条经验法则是,当你对某些东西进行一次编码时,很好,当你再次在某处复制该代码时,请记下并继续前进。当您第三次发现需要相同的代码时,是时候重构以消除重复和简化,并且通常涉及转移到设计模式。

  • When to refactor from simple-minded to GoF? I like what @anopres said - it's time when you feel the pain of not having the design pattern in place. The pain (or code "smell") may manifest itself in several ways. Code duplication is the most obvious. Refactoring books like Fowler's Refactoring or Kerievsky's Refactoring to Patterns list many such pain points/code stenches.
  • 什么时候从简单的重构到GoF?我喜欢@anopres所说的 - 现在是时候感受到没有设计模式的痛苦了。疼痛(或代码“气味”)可能以多种方式表现出来。代码重复是最明显的。重构像Fowler的Refactoring或Kerievsky的Refactoring to Patterns等书籍列出了很多这样的痛点/代码。

  • Can this [refactoring] be done in a sensible way? The trick to refactoring is to have a suite of unit tests in place which you have confidence in, and then to refactor without causing any of those tests to fail. Refactoring, by definition, does not change the functionality of your code. Therefore, if your tests continue to pass, you can have a pretty good feeling that you didn't break anything. Although it can be difficult, I actually enjoy this part of TDD, it's almost like a game to make changes without breaking any tests.
  • 这种[重构]能否以合理的方式完成?重构的技巧是让你有信心的一套单元测试,然后重构而不会导致任何这些测试失败。根据定义,重构不会改变代码的功能。因此,如果您的测试继续通过,您可以非常好地感觉到您没有破坏任何东西。虽然这可能很难,但实际上我喜欢TDD的这一部分,它几乎就像一个游戏,在不破坏任何测试的情况下进行更改。

In summary, I would say that TDD helps guide me to write the code that is sufficient at the time, and perhaps more importantly helps me to make the changes later when inevitably requirements change, more functionality is required, etc.

总之,我会说TDD有助于指导我编写当时足够的代码,或许更重要的是帮助我在以后不可避免地需求变更,需要更多功能等时进行更改。

#2


11  

Design Patterns are a consequence, not an objective. You don't think today I shall use Strategy Patterns, you just do it. Halfway through doing the third nearly identical class you stop and use a paper notebook to figure out the general case and knock up a base class that describes the shared context. You refactor the first two classes to be descendants, and this gives you a reality check and quite a few changes to your base class. Then the next thirty are a walk in the park.

设计模式是一种结果,而不是目标。你今天不认为我会使用策略模式,你只是这样做。在完成第三个几乎完全相同的类的过程中,你停下来并使用纸质笔记本来找出一般情况并敲掉描述共享上下文的基类。你重构前两个类是后代,这给你一个现实检查和你的基类的一些变化。然后接下来的三十个人在公园散步。

It's only the next day at the team meeting that you save everyone thirty minutes of boredom by saying "I used strategy pattern. They all work the same so there's only one test program, it takes parameters to change the test case."

只是在团队会议的第二天,你通过说“我使用了策略模式来节省每个人30分钟的无聊。他们都工作相同所以只有一个测试程序,它需要参数来改变测试用例。”

Intimate familiarity with patterns makes you use them reflexively, whenever the situation demands it. When people treat the use of patterns as an objective in its own right, you get stilted, ugly code that speaks of mechanism rather than purpose; the how rather than the why.

对模式的熟悉使您可以在情况需要时反复使用模式。当人们将模式的使用本身视为一个目标时,你就会得到笨拙,丑陋的代码,它们讲的是机制而不是目的;怎么而不是为什么。


Most patterns address recurring fundamental problems like complexity mitigation and the need to provide extensibility points. Providing extensibility points when its clear they won't be needed pointlessly complicates your code and creates more failure points and test cases. Unless you're building a framework for release into the wild, solve only the problems you actually face.

大多数模式解决了复杂的缓解和提供可扩展性点的需要等重复出现的基本问题。提供可扩展性点时,无需明确它们就会使代码变得复杂,并创建更多的故障点和测试用例。除非你构建一个框架以便释放到野外,否则只解决你实际面临的问题。

#3


10  

Patterns are only tools and vocabulary. You write the code to be as simple, understandable and maintainable as you know how. By knowing patterns you have more allternatives at your disposal, and you have a language to discuss pros and cons of the approach before implementing it.

模式只是工具和词汇。您可以将代码编写为您知道的简单,易懂和可维护的代码。通过了解模式,您可以使用更多的替代品,并且在实施之前,您可以使用一种语言来讨论该方法的优缺点。

In either case you dont just "switch" to "using a pattern". You just keep doing what you allways do, write the code the best way you know how.

无论哪种情况,你都不要只是“切换”到“使用模式”。你只是继续做你总是做的事情,以你知道的最佳方式编写代码。

#4


7  

This is similar to any other design decision. Ultimately, it depends. You should learn those patterns that are useful in your language (many GoF patterns aren't needed in Lisp or Smalltalk, for example), learn their advantages and disadvantages, understand the constraints of your system, and make the choice that best fits your needs.

这与任何其他设计决策类似。最终,这取决于。您应该学习那些对您的语言有用的模式(例如,Lisp或Smalltalk中不需要许多GoF模式),了解它们的优点和缺点,理解系统的约束,并做出最适合您需求的选择。

The best advice that I can give is to learn, learn, learn.

我能给出的最好的建议是学习,学习和学习。

#5


3  

Switching from a simple approach to a formal design pattern is usually something that happens fairly naturally for me as a problem increases in complexity. The key is to be familiar enough with the patterns that you can recognize the tipping point and switch from the simple approach to a design pattern when it will bring the most benefit for current and future development.

从简单的方法切换到正式的设计模式通常是我自然而然地发生的事情,因为问题会增加复杂性。关键是要熟悉可以识别引爆点的模式,并从简单的方法切换到设计模式,这将为当前和未来的开发带来最大的好处。

For a larger, more complex project, the tipping point should be fairly early on; in many cases, before you even start coding. For smaller projects, you can afford to wait before deciding to implement a pattern.

对于更大,更复杂的项目,引爆点应该是相当早的;在许多情况下,在你开始编码之前。对于较小的项目,您可以在决定实施模式之前等待。

One of the best things you can do to increase your ability to recognize when a pattern should be used is to take some time after completing a project to examine how complex your "simple" approach has become. If it would have taken you less time and effort to implement a pattern, or if the pattern would clarify what you were trying to do, you can file that knowledge away for the next time you encounter a similar problem.

为了提高识别应该使用模式的能力,您可以做的最好的事情之一是在完成项目后花一些时间来检查“简单”方法的复杂程度。如果它花费您更少的时间和精力来实现模式,或者如果模式会澄清您尝试做什么,那么您可以在下次遇到类似问题时提交该知识。

#6


2  

When you have a problem that one of the patterns solves. The GoF book has a section in each chapter that explains what types of scenarios each pattern is appropriate for. You should not analyze each problem you have, then go look up what pattern to use. You should become familiar with the patterns so that you learn to recognize what situations call for them.

当您遇到其中一个模式解决的问题时。 GoF书中的每一章都有一节介绍了每种模式适合的场景类型。您不应该分析每个问题,然后查看要使用的模式。您应该熟悉这些模式,以便学会识别哪些情况需要它们。

#7


0  

One of the nice things about the the original GoF book is that there is discussion of the scenarios where the pattern would best solve problem. Reviewing these discussions can help you determine if "it's time".

关于原始GoF书籍的一个好处是,讨论了模式最能解决问题的场景。查看这些讨论可以帮助您确定“是时候”了。

Another good place to start is with Head First Design Patterns. The exercises that illustrate the use of different design patterns are elaborate enough to offer a good learning experience. In addition the exercises are also grounded in real world scenarios, so it's never a stretch to see when the appropriate times to apply design patterns.

另一个好的起点是Head First Design Patterns。演示使用不同设计模式的练习非常精细,足以提供良好的学习体验。此外,练习也基于现实世界的场景,因此在适当的时候应用设计模式并不是一件容易的事。