Skip to content Skip to navigation

Connexions

You are here: Home » Content » Java design pattern

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

      A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

      What is in a lens?

      Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

      Who can create a lens?

      Any individual Connexions member, a community, or a respected organization.

    • External bookmarks
  • E-mail the author

Recently Viewed

This feature requires Javascript to be enabled.

Java design pattern

Module by: Joanne Ann

Summary: Allow user to know 23 methods of design pattern

Prototype(1,1) Template(1,2) Bridges(1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)

JAVA DESIGN PATTERNS

Creational Patterns - Prototype Pattern

Example : Cloning a sheep

The prototype means making a clone. This implies cloning of an object to avoid creation. If the cost of creating a new object is large and creation is resource intensive, we clone the object. We use the interface Cloneable and call its method clone() to clone the object.

Again let’s try and understand this with the help of a non-software example. I am stressing on general examples throughout this write-up because I find them easy to understand and easy to accept as we all read and see them in day-to-day activity. The example here we will take will be of a plant cell. This example is a bit different from the actual cloning in a way that cloning involves making a copy of the original one. Here, we break the cell in two and make two copies and the original one does not exist. But, this example will serve the purpose. Let’s say the Mitotic Cell Division in plant cells.Let’s take a class PlantCell having a method split(). The plant cell will implement the interface Cloneable.

Following is the sample code for class PlantCell.

PlantCell.java

package creational.builder;/*** Shows the Mitotic cell division in the plant cell.*/public class PlantCell implements Cloneable {
  public Object split() {
    try {
      return super.clone();}catch(Exception e) {System.out.println("Exception occured: "+e.getMessage());return null;}
    }
}// End of class

Now let’s see, how this split method works for PlantCell class. We will make another class CellDivision and access this method.

CellDivision.java

package creational.prototype;/*** Shows how to use the clone.*/public class CellDivision {
  public static void main(String[] args) {PlantCell cell = new PlantCell(); // create a clonePlantCell newPlantCell = (PlantCell)cell.split();}
}// End of class

One thing is you cannot use the clone as it is. You need to instantiate the clone before using it. This can be a performance drawback. This also gives sufficient access to the data and methods of the class. This means the data access methods have to be added to the prototype once it has been cloned. 

Comments, questions, feedback, criticisms?

Send feedback