java 什么是组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/196087/
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
What is a component
提问by Scott James
I listen to the podcast java posse, on this there is often discussion about components (note components are not (clearly) objects). They lament the fact that Java does not have components, and contrast with .NET that does. Components apparently makes developing applications (not just GUI apps) easier.
我听播客 java posse,关于这个,经常有关于组件的讨论(注意组件不是(显然)对象)。他们感叹 Java 没有组件这一事实,并与 .NET 相比。组件显然使开发应用程序(不仅仅是 GUI 应用程序)更容易。
I can figure from the discussion certain qualities that a component has, its something to-do with decoupling (substituting one component for another is just a matter of plumbing). It has something to-do with properties, it definitely has something to-do with events and delegates.
我可以从讨论中得出一个组件具有的某些特性,它与解耦有关(用一个组件替换另一个组件只是管道问题)。它与属性有关,它肯定与事件和委托有关。
So to the questions:
所以对于问题:
./ can anyone explain to me what a component is. (and why java beans are not components).
./ 任何人都可以向我解释什么是组件。(以及为什么 java bean 不是组件)。
./ can anyone explain how they help development.
./ 任何人都可以解释他们如何帮助开发。
./ can anyone explain why java does not have them if they are so useful.
./ 如果它们如此有用,任何人都可以解释为什么 java 没有它们。
采纳答案by Greg Hewgill
Software Engineering Radio has an episode on exactly this topic: http://se-radio.net/podcast/2008-02/episode-87-software-components
软件工程电台有一集关于这个主题:http: //se-radio.net/podcast/2008-02/episode-87-software-components
The general idea is that a software component can describe what its own dependencies and services are, in the form of metadata. I don't know why you might have heard that Java does not have components, since you can certainly imagine an architecture in Java where components do describe themselves through metadata. I suppose it's just that the Java platform definition by itself doesn't really have a component architecture.
一般的想法是软件组件可以以元数据的形式描述它自己的依赖项和服务是什么。我不知道为什么您可能听说 Java 没有组件,因为您当然可以想象 Java 中的一种架构,其中组件确实通过元数据来描述自己。我想只是 Java 平台定义本身并没有真正的组件架构。
Update: Indeed, one need not imagine such an architecture because as others pointed out, Java Beans or Servlets could certainly be considered component-based architectures.
更新:确实,人们不需要想象这样的架构,因为正如其他人指出的那样,Java Beans 或 Servlets 当然可以被视为基于组件的架构。
回答by Garth Gilmour
The term component is one of the most ambiguous and overused ones in OO.
术语组件是面向对象中最模糊和过度使用的术语之一。
Most people would agree that a component is made up of a group of classes, which collaborate together to implement one or more interfaces. One of the classes takes on the role of the 'front-end' i.e. it implements the interface but delegates the work to the other classes within the group. As you say components should be replaceable without the rest of the system knowing.
大多数人会同意一个组件由一组类组成,这些类协同工作以实现一个或多个接口。其中一个类承担“前端”的角色,即它实现接口但将工作委托给组内的其他类。正如您所说,组件应该是可更换的,而系统的其余部分不知道。
A great example of a component based architecture was COM. Its a great example because it was so heavily used and rigidly specified. But note that the need for this architecture was based on the inflexibility of the C++ compilation and deployment model.
基于组件的体系结构的一个很好的例子是 COM。这是一个很好的例子,因为它被大量使用和严格指定。但请注意,对这种架构的需求是基于 C++ 编译和部署模型的不灵活性。
in Java you can do an awful lot to a class without breaking binary compatability with the rest of the system. So there is not as much need to build rigid, component based architectures. But it all depends on how you define the term, e.g any project built using dependency injection could count as 'component based'.
在 Java 中,您可以对一个类做很多事情,而不会破坏与系统其余部分的二进制兼容性。所以没有那么多需要构建刚性的、基于组件的架构。但这完全取决于您如何定义该术语,例如,使用依赖注入构建的任何项目都可以算作“基于组件”。
回答by Miguel Gamboa
Although in Java beginnings the notion of component was many times related with Gui components, the generic sense of component in software engineering goes beyond that notion.
尽管在 Java 开始时,组件的概念多次与 Gui 组件相关,但软件工程中组件的一般意义超出了这个概念。
Simply put, a component is a piece of software, which is reusable. Like bricks, we combine and join them to build a whole application. The key insight of software components in modern environments is the Metadata, which describes the component's content and enables reuse.
简单地说,组件就是一个软件,它是可重用的。就像积木一样,我们将它们组合起来,构建一个完整的应用程序。现代环境中软件组件的关键洞察是元数据,它描述了组件的内容并支持重用。
In 1996, the JDK 1.0 was the first managed runtime environment providing components with metadata. In this case, components are .classfiles containing bytecodesand metadata. Yet, and according to the Java Specification, a .classfile only contains one type definition. So, to deploy a bundle of types as a component we may use a Jar archive containing several .classfiles.
1996 年,JDK 1.0 是第一个为组件提供元数据的托管运行时环境。在这种情况下,组件是.class包含字节码和元数据的文件。然而,根据 Java 规范,一个.class文件只包含一个类型定义。因此,要将一组类型部署为组件,我们可能会使用包含多个.class文件的 Jar 存档。
On the other hand, in the .Net platform, which provides the same idea of reusable components, a component may contain more than one type definition. In this case a component (aka assemblyin .Net) is a .dllor a .exefile.
另一方面,在 .Net 平台中,它提供了相同的可重用组件思想,一个组件可能包含多个类型定义。在这种情况下,组件(也就是.Net 中的程序集)是一个.dll或.exe文件。
回答by Jason Baker
It depends on what you mean by "component". The term can mean lots of different things in a lot of different contexts, so it can easily get confusing. With that said, here's my understanding of the subject:
这取决于您所说的“组件”是什么意思。该术语在许多不同的上下文中可能意味着许多不同的事物,因此很容易混淆。话虽如此,这是我对这个主题的理解:
A component is different from an object (although objects are often used to represent and build components). The difference being a couple of things:
组件不同于对象(尽管对象通常用于表示和构建组件)。区别在于以下几点:
- Objects tend to just be "things" whereas components are actors. The difference being that a component is a part of a process, while an object represents some kind of abstract idea.
- Components help ensure code re-use and pluggability because they are basically small "sub-programs" (or sometimes programs in their own right) that ideally can be adapted to work well with other components.
- You tend to see components more in "nothing shared" message-passing systems like Erlangor Kamaelia, mainly because those types of frameworks tend to be best suited for component-oriented design.
- 对象往往只是“事物”,而组件是演员。区别在于组件是过程的一部分,而对象代表某种抽象的想法。
- 组件有助于确保代码重用和可插拔性,因为它们基本上是小的“子程序”(或者有时是它们自己的程序),理想情况下可以适应与其他组件一起工作。
- 您更倾向于在“无共享”消息传递系统(如Erlang或Kamaelia )中看到更多组件,主要是因为这些类型的框架往往最适合面向组件的设计。
There are a lot of good examples of component-oriented design, but my first choice would be UNIX. The basic idea behind UNIX is that it is more a set of small programs designed to work together rather than being made up of several more monolithic programs.
面向组件的设计有很多很好的例子,但我的第一个选择是 UNIX。UNIX 背后的基本思想是,它更像是一组旨在协同工作的小程序,而不是由多个单一的程序组成。
回答by S.Lott
Software comes in several groupings. Here are the Java chunks.
软件分为几组。这是 Java 块。
- Statements.
- Method functions. Multiple statements.
- Class. Multiple attributes and method functions.
- File. One or more classes. One class is the public class in the file, other classes are hidden in the file.
- Package. Multiple classes. These form a hierarchy.
- 声明。
- 方法函数。多个陈述。
- 班级。多个属性和方法函数。
- 文件。一节或多节课。一类是文件中的公共类,其他类隐藏在文件中。
- 包裹。多班。这些形成了一个层次结构。
"Component", "Layer", "Tier" and other philosophical groupings are -- generally -- notional. The VB COM environment had a formalism for components. Everyone else treats them as just ideas.
“组件”、“层”、“层”和其他哲学分组——通常是——概念化的。VB COM 环境具有组件的形式主义。其他人都将它们视为只是想法。
Beans are classes. Can be single class be a component? Maybe. A component is usually a bunch of classes. Sometimes as few as two - a formal interface and an implementation.
Bean 是类。单个类可以是一个组件吗?或许。一个组件通常是一堆类。有时只有两个——一个正式的接口和一个实现。
Components help you focus on a logical grouping of classes, packages, groupings, etc.
组件可帮助您专注于类、包、分组等的逻辑分组。
Since a component is notional, every language more-or-less has them. There are few language formalisms for components. They aren't really needed. It's an idea or a principle you use to structure your thinking.
由于组件是概念性的,因此每种语言或多或少都有它们。组件的语言形式很少。他们并不是真正需要的。这是您用来构建思维的想法或原则。
You can, with some care, define an approach to "components" with an interface and metadata and numerous other features.
您可以谨慎地定义一种具有接口和元数据以及许多其他功能的“组件”方法。
回答by Martin Klinke
I don't know .NET components particularly, but from the Java POV, I'd say that a component is some functional unit that should have a defined interface/usage principle. While Java does not have components as a language concept, there are IMHO components in Java. Technical components would be e.g.:
我不是特别了解 .NET 组件,但从 Java POV 来看,我会说组件是一些功能单元,应该具有定义的接口/使用原则。虽然 Java 没有组件作为语言概念,但 Java 中有恕我直言的组件。技术组件将是例如:
- EJBs
- Servlets
- EJB
- 小服务程序
Functional components would be e.g.:
功能组件将是例如:
- Automatic Update for an application
- Formula mechanism that enables calculations on a data model
- 应用程序的自动更新
- 支持对数据模型进行计算的公式机制
Architectural components could be JAR files or OSGi bundles.
架构组件可以是 JAR 文件或 OSGi 包。
Of course, there is always room for interpretation ;)
当然,总有解释的余地;)

