G.Prototype

  1. Summary

      1. Prototype pattern uses cloning as object instantiation pattern. We can use prototype pattern in following scenario.

        1. When need to create a large number of objects that just differ in terms of their attribute values. For example rooms in a building construction project.

        2. In this case each object is derived from a common interface and have cloning logic (ex. Copy and paste operation )

        3. When all the object need to share some common schema loaded at runtime for example XML Document Objects.

  2. Overview Tutorial

  1. Class Diagram

      1. Client holds an instants of object that expose clone() method . Whenever client needs an object it uses clone() method to create a new object. Client can further update its properties if required.

  1. Specific problems and Considerations

    1. Using Prototype Manager for cloning

        1. We can use prototype manager that is implemented usually as a hash table keeping the object to clone. In this cases prototype become a factory method which uses cloning instead of Instantiation.

    2. Deep Clones vs. Shallow Clones

        1. When we clone complex objects which contains other objects, we should take care how they are cloned.

        2. We can clone contained objects also (deep cloning) or we can the same reference for them, and to share them between cloned container objects.

    1. Explicit Object Initialization

        1. There are certain situations when objects need to be initialized after they are created.

    1. Prototype Vs Factory Vs Builder

        1. In nut shell prototype uses replication, factory uses instantiation and builder uses composition mechanism to create final object.

        2. Use Prototype if final object differ only attribute values not in schemas of object.

        3. Prototype with Prototype Manger become Factory pattern that uses cloning as object initialization.

        4. Prototype is very useful when a family of object need to share a common schema, reference or constraints , best example is XMLDocument class.