In a Composite (or Decorator) structure, Prototype uses a prototypical instance to create new Composite Component. Composite is a structural pattern where as Prototype is creational pattern. They are related to each other in a sense that Prototype only make sense in the context of a structural pattern like Composite or Decorator. Composite (source: GoF) Prototype source: GoF overviewComposite pattern compose tree structures to represent part-whole hierarchies. It describes that a group of objects is to be treated in the same way as a single instance of an object through 'Component' interface. The code in the code example section show how Prototype makes sense Composite by mixing the Composite pattern and Prototype pattern. For example, in the Game of Life, the universe of a Game of Life contains an outermost Cell. This outer most Cell is a composite Neighborhood which composes of other DEFAULT_GRID_SIZExDEFAULT_GRID_SIZE Neighborhoods. This outer most Cell is created via Prototype, recursively: outermostCell = new Neighborhood code sampleComponent/Prototype public interface Cell Composite/ConcretePrototype public final class Neighborhood implements Cell Leaf/ConcretePrototype public final class Resident implements Cell when to use
comparisonFactory Method vs. Prototype references
1 Comment
|
Categories
All
Archives
May 2020
|