什么是设计模式?它如何使一个人的编程效率受益? [英] what are design patterns and how can it benefit one's programming productivity?

查看:75
本文介绍了什么是设计模式?它如何使一个人的编程效率受益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读XP等各种编程风格(首先编写测试),并且偶然发现了一本Java书籍,其中只有设计模式,看起来像是伪代码.

这些设计模式用于什么?编程上下文中的设计模式指的是什么?它的用途是什么?就像构建特定于域的应用程序的蓝图一样吗?

解决方案

设计模式是对常见设计问题的通用可重用解决方案.实际上,我们可以概括为:模式是对常见问题的通用可重用解决方案.不仅有设计模式,还有编码模式(尽管我们不称呼它们,我们称它们为成语),体系结构模式,用户体验模式,过程模式等.

但是,该定义存在一个小问题:在编程中,我们将常见问题的通用可重用解决方案称为 Program .因此,换句话说,如果您反复出现问题,则可以编写程序,只有不能编写程序,然后,您才需要模式. /p>

因此,另一种定义是:模式是您无法编写的程序,因为您的编程语言太弱而无法表达它.

因此,通常,在尝试模式之前,应尝试使用更好的编程语言.但是,编程语言设计全是权衡取舍:设计一种能够平等表达所有问题的编程语言根本是不可能的.因此,在代码中包含一些设计模式"是完全可以的,但是您不应拥有太多的设计模式",并且在核心业务逻辑中绝对不应包含任何模式".这表明您选择了错误的编程语言,没有任何一种模式可以解决该问题.

这里是一个例子:在汇编编程中,在程序的不同部分之间共享参数化的行为是一个常见的设计问题.有一种解决该问题的设计模式:称为子例程.但是在另一种语言中,实际上在几乎所有现代语言中,子例程(有时称为过程,函数,方法,例程,子例程)都直接内置在该语言中.它们不是模式,它们只是存在而没有人再考虑它们了.因此,如果您的代码中只有一个或两个子例程,那很好.但是,如果您有很多,那么组装可能不是正确的选择.

其他一些示例:在基于原型的OO语言中,内置了原型模式.在具有多调度的语言中,内置了Visitor模式.在Ruby中,没有构造函数,您始终使用Factory Methods而不使用考虑此事.在基于委托的OO语言中,内置了Decorator Pattern.在具有高阶过程的语言中,内置了Iterator Pattern.在没有可变状态的语言中,Iterator Pattern实际上是 Antipattern .

I've been reading various programming styles like XP (writing the test first), and came across a Java book that just has design patterns, what seems to be like pseudocodes.

What are these design patterns used for ? what does design apttern in the context of programming refer to and what are it's applications ? Is it like blue prints for building a domain specific application ?

解决方案

A Design Pattern is a general reusable solution to a commonly occuring design problem. Actually, we can generalize that: A Pattern is a general reusable solution to a commonly occuring problem. There are not only Design Patterns, there are also Coding Patterns (although we don't call them that, we call them Idioms), Architecture Patterns, User Experience Patterns, Process Patterns etc.

However, there is a small problem with that definition: in programming, we call a general reusable solution to commonly occuring problem a Program. So, in other words, if you have a recurring problem, you write a program, and only if you can't write a program, then you need a Pattern.

So, an alternative definition would be: A Pattern is a Program that you cannot write because your Programming Language is too weak to express it.

So, in general, you should try to use a better Programming Language before you try a Pattern. However, Programming Language Design is all about trade-offs: it's simply impossible to design a Programming Language which can express all problems equally well. So, having some Design Patterns in your code is perfectly fine, but you shouldn't have many of them and you definitely shouldn't have any Patterns in your core business logic. That's a sign that you chose the wrong programming language and no pattern can fix that.

Here's an example: in assembly programming, having a parametric piece of behavior shared between different parts of the program is a commonly occuring design problem. And there is a design pattern that solves that problem: it's called a Subroutine. But in a different language, and in fact in pretty much all modern languages, subroutines (sometimes called procedures, functions, methods, routines, subs) are built right into the language. They aren't a Pattern, they're just there and nobody even thinks about them anymore. So, if you have just one or two subroutines in your code, that's fine. But if you have many of them, assembly is probably not the right choice.

Some other examples: In a prototype-based OO language, the Prototype Pattern is built in. In a language with multiple dispatch, the Visitor Pattern is built in. In Ruby, there are no constructors, you always use Factory Methods without thinking about it. In delegation-based OO languages, the Decorator Pattern is built in. In languages with higher-order procedures, the Iterator Pattern is built in. In languages without mutable state, the Iterator Pattern is actually an Antipattern.

这篇关于什么是设计模式?它如何使一个人的编程效率受益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆