The Factory Method is usually categorised by a switch statement where each case returns a different class, using the same root interface so that the calling code never needs to make decisions about the implementation.
Think of a credit card validator factory which returns a different validator for each card type.
public ICardValidator GetCardValidator (string cardType)
{
switch (cardType.ToLower())
{
case "visa":
return new VisaCardValidator();
case "mastercard":
case "ecmc":
return new MastercardValidator();
default:
throw new CreditCardTypeException("Do not recognise this type");
}
}
The Abstract Factory is where you have multiple concrete factory classes (not Factory Methods) derived from one interface which may return many different types from different methods.
Think of a chess game manager with a different class for each set of variant rules.
public class StandardChessRulesFactory : IChessRulesFactory
{
public IBoardMapper GetBoardMapper()
{
return new StandardChessBoardMapper();
}
public IKingMover GetKingMover()
{
return new StandardChessKingMover();
}
public IMoveClock GetMoveClock()
{
return new StandardMoveClock();
}
}
public class HexagonalChessRulesFactory : IChessRulesFactory
{
public IBoardMapper GetBoardMapper()
{
return new HexagonalChessBoardMapper();
}
public IKingMover GetKingMover()
{
return new HexagonalChessKingMover();
}
public IMoveClock GetMoveClock()
{
return new StandardMoveClock();
}
}
public class SpeedChessRulesFactory : IChessRulesFactory
{
public IBoardMapper GetBoardMapper()
{
return new StandardChessBoardMapper();
}
public IKingMover GetKingMover()
{
return new StandardChessKingMover();
}
public IMoveClock GetMoveClock()
{
return new SpeedChessMoveClock();
}
}
An Abstract Factory, much like a Strategy, is often selected using a Factory Method, but it isn't necessary to combine them so it is its own pattern.
The keyword «access» is used for private package imports. The UML 2.4.1 spec says "A package import is shown using a dashed arrow with an open arrowhead from the importing namespace to the imported package. A keyword is shown near the dashed arrow to identify which kind of package import is intended. The predefined keywords are «import» for a public package import, and «access» for a private package import."[1]
The keyword «use» is used to depict the use of an interface. The UML 2.4.1 spec says "For displaying the full signature of an interface of a component, the interfaces can also be displayed as typical classifier rectangles that can be expanded to show details of operations and events." [2] and "A usage dependency is shown as a dependency with a «use» keyword attached to it." [3]
[1] UML Superstructure Specification, v2.4.1; page 112; http://www.omg.org/spec/UML/2.4.1/Superstructure/PDF
[2] UML Superstructure Specification, v2.4.1; page 153; http://www.omg.org/spec/UML/2.4.1/Superstructure/PDF
[3] UML Superstructure Specification, v2.4.1; page 139; http://www.omg.org/spec/UML/2.4.1/Superstructure/PDF
Best Answer
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:
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«.
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.