谁能给我解释一下什么是软件框架?为什么我们需要一个框架?框架做了什么来简化编程?
当前回答
维基百科(软件框架)的摘要(第一个谷歌点击顺便说一下)解释得很好:
A software framework, in computer programming, is an abstraction in which common code providing generic functionality can be selectively overridden or specialized by user code providing specific functionality. Frameworks are a special case of software libraries in that they are reusable abstractions of code wrapped in a well-defined Application programming interface (API), yet they contain some key distinguishing features that separate them from normal libraries. Software frameworks have these distinguishing features that separate them from libraries or normal user applications: inversion of control - In a framework, unlike in libraries or normal user applications, the overall program's flow of control is not dictated by the caller, but by the framework.[1] default behavior - A framework has a default behavior. This default behavior must actually be some useful behavior and not a series of no-ops. extensibility - A framework can be extended by the user usually by selective overriding or specialized by user code providing specific functionality. non-modifiable framework code - The framework code, in general, is not allowed to be modified. Users can extend the framework, but not modify its code.
您可能“需要”它,因为它在开发应用程序时为您提供了一个很好的快捷方式,因为它包含了许多已经编写和测试过的功能。原因与我们使用软件库的原因非常相似。
其他回答
除了定义(有时只有在你已经理解的情况下才能理解)之外,还有一个例子帮助了我。
I think I got a glimmer of understanding when loooking at sorting a list in .Net; an example of a framework providing a functionality that's tailored by user code providing specific functionality. Take List.Sort(IComparer). The sort algorithm, which resides in the .Net framework in the Sort method, needs to do a series of compares; does object A come before or after object B? But Sort itself has no clue how to do the compare; only the type being sorted knows that. You couldn't write a comparison sort algorithm that can be reused by many users and anticipate all the various types you'd be called upon to sort. You've got to leave that bit of work up to the user itself. So here, sort, aka the framework, calls back to a method in the user code, the type being sorted so it can do the compare. (Or a delegate can be used; same point.)
我算对了吗?
在最低的层次上,框架是一个环境,在其中您可以使用一组工具
这些工具以库、配置文件等形式出现。
这个所谓的“环境”为您提供了基本的设置(错误报告、日志文件、语言设置等)……它可以被修改,扩展和构建。
人们实际上不需要框架,这只是想要节省时间的问题,而其他人只是个人喜好的问题。
人们会证明有了框架,你就不必从头开始编码了。但这些只是人们混淆了库和框架。
我在这里没有偏见,实际上我现在使用的是一个框架。
已经有很多很好的答案了,但让我看看我是否能给你另一个观点。
为了简化事情,您可以将框架视为一个除了实际功能之外完整的应用程序。你插入功能和PRESTO!你有一个应用程序。
Consider, say, a GUI framework. The framework contains everything you need to make an application. Indeed you can often trivially make a minimal application with very few lines of source that does absolutely nothing -- but it does give you window management, sub-window management, menus, button bars, etc. That's the framework side of things. By adding your application functionality and "plugging it in" to the right places in the framework you turn this empty app that does nothing more than window management, etc. into a real, full-blown application.
对于web应用程序,服务器端应用程序等也有类似类型的框架。在每种情况下,框架提供了大量乏味、重复的代码(希望如此),而您提供了实际的问题域功能。(这是理想状态。当然,在现实中,框架的成功有很大的变数。)
我再次强调,这是对框架的简化观点。我没有使用像“控制反转”之类的可怕术语,尽管大多数框架都内置了这样可怕的概念。既然你是初学者,我想我就不给你行话了,用一个简单的比喻。
框架为特定的问题领域提供功能/解决方案。 定义来自wiki:
计算机中的一个软件框架 编程,是一种抽象 哪个公共代码提供泛型 功能可以选择性地 由用户代码覆盖或专门化 提供特定的功能。 框架是一种特殊情况 它们是软件库 代码包装的可重用抽象 在定义良好的应用程序中 编程接口(API),然而他们 包含一些关键区别 区分它们的特征 普通的图书馆。
从技术上讲,您不需要框架。如果你想做一个非常非常简单的网站(想想1992年的网页),你可以用硬编码的HTML和一些CSS来完成。
如果你想做一个现代的网络应用,你实际上也不需要使用框架。
You can instead choose to write all of the logic you need yourself, every time. You can write your own data-persistence/storage layer, or - if you're too busy - just write custom SQL for every single database access. You can write your own authentication and session handling layers. And your own template rending logic. And your own exception-handling logic. And your own security functions. And your own unit test framework to make sure it all works fine. And your own... [goes on for quite a long time]
然后,如果您确实使用了一个框架,您将能够从优秀的、通常是同行评审的、经过很好测试的其他开发人员的工作中受益,他们可能比您更好。您可以快速构建想要的内容,而不必花费时间构建或过于担心上面列出的基础设施项目。
您可以在更短的时间内完成更多的工作,并且知道您正在使用或扩展的框架代码很可能比您自己做得更好。
那么成本呢?花点时间学习框架。但是,正如几乎每个web开发人员都会证明的那样,花时间学习从使用你选择的任何框架中获得大量(真的,大量)好处是绝对值得的。