Skip to content

Commit bfbc8a9

Browse files
committed
Bonus code example for Prototype design pattern
1 parent 6deb6b9 commit bfbc8a9

File tree

5 files changed

+172
-13
lines changed

5 files changed

+172
-13
lines changed

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.premaseem.prototypePattern;
2+
3+
public abstract class Cell implements Cloneable {
4+
5+
public Cell(){
6+
System.out.println("Constructor of Cell is called ... ");
7+
}
8+
9+
String cellType;
10+
public enum CellProtoTypes{
11+
AMOEBA,BACTERIA,SINGLE_CELL_ORG
12+
}
13+
14+
@Override
15+
protected Object clone() throws CloneNotSupportedException {
16+
// TODO Auto-generated method stub
17+
return super.clone();
18+
}
19+
20+
@Override
21+
public String toString() {
22+
// TODO Auto-generated method stub
23+
return cellType + "with id " + hashCode();
24+
}
25+
26+
public Object split() {
27+
Object cloneCell = null;
28+
try {
29+
cloneCell = this.clone();
30+
} catch (CloneNotSupportedException e) {
31+
e.printStackTrace();
32+
}
33+
return cloneCell;
34+
35+
}
36+
}
37+
38+
class Amoeba extends Cell{
39+
public Amoeba(){
40+
cellType=" Amoeba ";
41+
System.out.println("Constructor of Amoeba is called ... ");
42+
}
43+
}
44+
45+
class Bacteria extends Cell{
46+
public Bacteria(){
47+
cellType=" Bacteria ";
48+
System.out.println("Constructor of Bacteria is called ... ");
49+
}
50+
}
51+
52+
class SingleCell extends Cell{
53+
public SingleCell(){
54+
cellType=" SingleCell ";
55+
System.out.println("Constructor of SingleCell is called ... ");
56+
}
57+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.premaseem.prototypePattern;
2+
3+
import java.util.Scanner;
4+
5+
import com.premaseem.prototypePattern.Cell.CellProtoTypes;
6+
7+
public class ClientFile {
8+
9+
public static void main(String[] args) {
10+
11+
System.out.println("Welcome to Prototype design patters Lab ");
12+
Scanner scan = new Scanner(System.in);
13+
SpecimenCache specimenLab = SpecimenCache.getInstance();
14+
Cell subjectCell = null;
15+
16+
int repeatRunFlag = 1;
17+
while (repeatRunFlag == 1) {
18+
// TVInterface tv;
19+
System.out.println("Which organizm you would like to culture / grow / multiply ? ");
20+
System.out.println("press 1 for Single Cell Org");
21+
System.out.println("press 2 for Amoeba ");
22+
System.out.println("press 3 for Bacteria ");
23+
int type = scan.nextInt();
24+
if (type == 1) {
25+
subjectCell = specimenLab.getCellProtoType(CellProtoTypes.SINGLE_CELL_ORG);
26+
} else if (type == 2) {
27+
subjectCell = specimenLab.getCellProtoType(CellProtoTypes.AMOEBA);
28+
} else {
29+
subjectCell = specimenLab.getCellProtoType(CellProtoTypes.BACTERIA);
30+
}
31+
32+
System.out.println("Culture process has started ");
33+
System.out.println(" Cells are spliting/cloning and growing ");
34+
35+
for (int i = 0; i < 10; i++) {
36+
System.out.println(subjectCell.split());
37+
System.out.println(" ........... ........ >>>> .... ");
38+
}
39+
System.out.println("=============================");
40+
41+
System.out.println("Press 1 to Repeat .... ");
42+
repeatRunFlag = scan.nextInt();
43+
44+
}
45+
46+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
47+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$$ www.premaseem.com $$$$$$$$$$$$$$$$$$$$$$ \n ");
48+
49+
}
50+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Download the working example demo code in java from my GIT repository -https://github.com/premaseem/designPatterns/tree/4bb9beca7bab5b5e71d02b76e4f1ad48fce4aca6/ZipDownloadableProjects
2+
3+
Code Example
4+
The Prototype pattern specifies the kind of objects to create using a prototypical instance. Cell is the abstract class with different subclasses like singleCell Organism, amoeba, bacteria etc. For each sub class a breeder object is created before hand and registered in SpecimenCache.java class. Now whenever client program wants to breed or culture a particular organism say amobea they will fetch it from cache and invoke split/clone method which will provide replicated organism with same state property. Download the example code and have fun.
5+
Example of Prototype
6+
When creating an object is time consuming and a costly affair and you already have a most similar object instance in hand, then you go for prototype pattern. Instead of going through a time consuming process to create a complex object, just copy the existing similar object and modify it according to your needs. Clone the existing instance in hand and then make the required update to the cloned instance so that you will get the object you need. Other way is, tweak the cloning method itself to suit your new object creation need.
7+
8+
Intent
9+
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
10+
Co-opt one instance of a class for use as a breeder of all future instances.
11+
The new operator considered harmful.
12+
Problem
13+
Application "hard wires" the class of object to create in each "new" expression.
14+
Discussion
15+
Declare an abstract base class that specifies a pure virtual "clone" method, and, maintains a dictionary of all "cloneable" concrete derived classes. Any class that needs a "polymorphic constructor" capability: derives itself from the abstract base class, registers its prototypical instance, and implements the clone() operation.
16+
The client then, instead of writing code that invokes the "new" operator on a hard-wired class name, calls a "clone" operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.
17+
18+
Check list
19+
Add a clone() method to the existing "product" hierarchy.
20+
Design a "registry" that maintains a cache of prototypical objects. The registry could be encapsulated in a new Factory class, or in the base class of the "product" hierarchy.
21+
Design a factory method that: may (or may not) accept arguments, finds the correct prototype object, calls clone() on that object, and returns the result.
22+
The client replaces all references to the new operator with calls to the factory method.
23+
Rules of thumb
24+
Prototype co-opts one instance of a class for use as a breeder of all future instances.
25+
Prototypes are useful when object initialization is expensive, and you anticipate few variations on the initialization parameters. In this context, Prototype can avoid expensive "creation from scratch", and support cheap cloning of a pre-initialized prototype.
26+
Factory Method: creation through inheritance. Protoype: creation through delegation.
27+
Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Protoype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
28+
Prototype doesn't require subclassing, but it does require an "initialize" operation. Factory Method requires subclassing, but doesn't require Initialize.
29+
Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.
30+
31+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.premaseem.prototypePattern;
2+
3+
import java.util.EnumMap;
4+
import java.util.Map;
5+
6+
import com.premaseem.prototypePattern.Cell.CellProtoTypes;
7+
8+
public class SpecimenCache {
9+
10+
static SpecimenCache specimenCache=null;
11+
private Map<CellProtoTypes,Cell> prototypeSamples = new EnumMap<CellProtoTypes,Cell>(CellProtoTypes.class);
12+
13+
static public SpecimenCache getInstance(){
14+
if(specimenCache==null){
15+
specimenCache = new SpecimenCache();
16+
}
17+
return specimenCache;
18+
}
19+
20+
public SpecimenCache(){
21+
loadSpecimenCache();
22+
}
23+
24+
void loadSpecimenCache(){
25+
prototypeSamples.put(CellProtoTypes.SINGLE_CELL_ORG, new SingleCell());
26+
prototypeSamples.put(CellProtoTypes.BACTERIA, new Bacteria());
27+
prototypeSamples.put(CellProtoTypes.AMOEBA, new Amoeba());
28+
}
29+
30+
public Cell getCellProtoType(CellProtoTypes cell){
31+
return prototypeSamples.get(cell);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)