Software Development, Product to Market
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use

Composite and Prototype Design Pattern Example

4/21/2016

0 Comments

 
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.
Picture
Composite (source: GoF)
Picture
Prototype source: GoF

overview

​Composite 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
(DEFAULT_GRID_SIZE,
 // passed-in prototype
 new Neighborhood(DEFAULT_GRID_SIZE,
                  new Resident())       // passed-in prototype
);

code sample

Component/Prototype
public interface Cell
{
  Cell create();
  ...
}
Composite/ConcretePrototype
public final class Neighborhood implements Cell
{
  /** The actual grid of Cells contained within this neighborhood. */
  private final Cell[][] grid;
 
  /** The neighborhood is square, so gridSize is both the horizonta
   *  and vertical size.
   */
  private final int        gridSize;

  /** Create a new Neigborhood containing gridSize-by-gridSize
   *  clones of the prototype. The Protype is deliberately
   *  not put into the grid, so you can reuse it if you like.
   */
  public Neighborhood(int gridSize, Cell prototype)
  {
      this.gridSize = gridSize;
      this.grid = new Cell[gridSize][gridSize];
      for( int row = 0; row < gridSize; ++row )
        for( int column = 0; column < gridSize; ++column )
          grid[row][column] = prototype.create();
  }

  /** The "clone" method used to create copies of the current
   *  neighborhood. This method is called from the containing
   *  neighborhood's constructor.
   */
    public Cell create()
    {    return new Neighborhood(gridSize, grid[0][0]);
    }
  ...
}
​
Leaf/ConcretePrototype
public final class Resident implements Cell
{
  private static final Color BORDER_COLOR = Colors.DARK_YELLOW;
  private static final Color LIVE_COLOR     = Color.RED;
  private static final Color DEAD_COLOR   = Colors.LIGHT_YELLOW;
  private boolean amAlive     = false;
  private boolean willBeAlive    = false;
  ...
  public Cell create(){    // The "clone" method
    return new Resident(); 
  }
  ...
}

when to use

  1. Prototype only make sense in the context of a structural pattern like Composite or Decorator.
  2. You want to form a composite object with a prototype (a library of reusable objects) and the prototype itself may need to be formed from another prototype recursively.

comparison

Factory Method vs. Prototype

references

The Game of Life
0 Comments



Leave a Reply.

    Categories

    All
    Algorithm
    Concurrency
    CQ
    Data Structure
    Design Pattern
    Developer Tool
    Dynamic Programming
    Entrepreneur
    Functional Programming
    IDE
    Java
    JMX
    Marketing
    Marklogic
    Memory
    OSGI
    Performance
    Product
    Product Management
    Security
    Services
    Sling
    Social Media Programming
    Software Development

    Feed Widget

    Archives

    May 2020
    March 2020
    April 2018
    March 2018
    February 2018
    December 2017
    March 2017
    November 2016
    June 2016
    May 2016
    April 2016
    October 2015
    September 2015
    August 2015
    September 2014
    July 2014
    June 2014
    May 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013

    RSS Feed

in loving memory of my mother  and my 4th aunt
Photos used under Creative Commons from net_efekt, schani, visnup, Dan Zen, gadl, bobbigmac, Susana López-Urrutia, jwalsh, Philippe Put, michael pollak, oskay, Creative Tools, Violentz, Kyknoord, mobilyazilar