There is no clear notion of the term »component«. You could use it in many different ways. But if you go to the root of the word (in terms of linguistics) you have »componere«, which is itself a »compositum« of »cum« (with) and »ponere« (put). So if you put something together you have a »composition« of individual parts.
There are two main aspects for components:
- Composition »put together«
- Compartmentalization »independent things«
So a software component is a unit or part of software, which in itself could be used independent from the actual composition - but on the other hand is used to build a larger system. Ideally you have a software in which each component is orthogonal, which means, no component is dependent on another component and they do each their job, where no functionality is duplicated (a form of DRY).
In most cases you could substitute »component« with »part«.
I am trying to figure out what components mean in the context of the composite pattern.
The point of the composite pattern is to put independent parts under a unique interface. You have the base component as a contract for each part to comply with. You have a leaf which is the part and you have the composite, which contains a collection of those parts.
1) What is the difference in purpose between a function and a component?
A component is the subject / carrier of a function ( in OO-terms).
On the other hand, if you take functional languages, I think it would be okay, to speak of functions as components in a functional composition.
In a broader sense, you could speak of a component in a whole, which represents one function: e.g. »a printing component« which stands for the function to be able to print.
2) What does a component have to do with the composite pattern?
See above: Composite is about putting together components.
3) Can you describe what a component is by using a funny metaphor? For example, explain how creating a component like baking an apple pie.
- One way to put it: A component is like an ingredient for a meal.
- Another way to put it: A component is a player in a soccer game. Each of them is different and has to play another role in the game, but together, they are a team and play soccer.
Best Answer
Component
The principle of systems made of components and the quest for reusable components have driven lots of work in the OO software engineering since the early 80s. There is therefore a rather clear consensus about what it is.
The SWEBOK provides the following definition in section 7.Software Design Strategies and Methods:
The UML specifications provide for similar semantic in section 11.6.3:
The core idea of both definition is that the component is something that is self-contained ("independent ... having well-defined ... dependencies"), that provides an interface with a contract (i.e. a promised behavior, the latter being implied by "independently composable"), and is replaceable by another component offering the same interface and contract.
However, despite this consensus, component may still mean slightly different things to different people. For example, some consider that a component must be embodied in a deployable artefact (e.g. executable, dynamic library, ...), while other see the component already in the construction process (e.g. separate compilation unit).
Package
There is unfortunately lot less consensus on what a package is. The reason is that different mainstream technologies use the same term for different purpose. For example:
So depending on the context, it is a group of deployagble artifacts that form a whole (which is in fact very close to the definition of a component), a group of components, or just a subdivision of something larger. The only general consensus here seems to be that a package regroups artifacts.