谁能给我解释一下什么是软件框架?为什么我们需要一个框架?框架做了什么来简化编程?


当前回答

除了定义(有时只有在你已经理解的情况下才能理解)之外,还有一个例子帮助了我。

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.)

我算对了吗?

其他回答

我不确定“框架”是否有一个明确的定义。有时,一组很大的库被称为框架,但我认为这个词的典型用法更接近于aioobe带来的定义。

这篇非常好的文章总结了一组库和一个框架之间的区别:

框架可以定义为一组库,它们说“不要给我们打电话,我们会给你打电话”。

框架如何帮助您?因为您不需要从头开始编写一些东西,基本上只需扩展一个给定的工作应用程序。通过这种方式,您可以获得很高的生产率——有时最终得到的应用程序可能比您在相同时间内自己所做的要复杂得多——但您通常会牺牲很多灵活性。

维基百科(软件框架)的摘要(第一个谷歌点击顺便说一下)解释得很好:

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'm very late to answer it. But, I would like to share one example, which I only thought of today. If I told you to cut a piece of paper with dimensions 5m by 5m, then surely you would do that. But suppose I ask you to cut 1000 pieces of paper of the same dimensions. In this case, you won't do the measuring 1000 times; obviously, you would make a frame of 5m by 5m, and then with the help of it you would be able to cut 1000 pieces of paper in less time. So, what you did was make a framework which would do a specific type of task. Instead of performing the same type of task again and again for the same type of applications, you create a framework having all those facilities together in one nice packet, hence providing the abstraction for your application and more importantly many applications.

框架具有您可能需要的一些功能。您可能需要某种具有内置排序机制的数组。或者,您可能需要一个窗口,您想在其中放置一些控件,所有这些都可以在框架中找到。这是一种跨越你自己作品框架的作品。

编辑: 好吧,我将要挖掘你们试图告诉我的东西;)你可能没有注意到“跨越一个框架的工作……”这行之间的信息。 在它越陷越深之前。我试着给它一个地板,希望你优雅地: 我在这里找到了一个很好的解释“库和框架的区别”的问题 http://ifacethoughts.net/2007/06/04/difference-between-a-library-and-a-framework/

在最低的层次上,框架是一个环境,在其中您可以使用一组工具

这些工具以库、配置文件等形式出现。

这个所谓的“环境”为您提供了基本的设置(错误报告、日志文件、语言设置等)……它可以被修改,扩展和构建。

人们实际上不需要框架,这只是想要节省时间的问题,而其他人只是个人喜好的问题。

人们会证明有了框架,你就不必从头开始编码了。但这些只是人们混淆了库和框架。

我在这里没有偏见,实际上我现在使用的是一个框架。