Prototype Design Pattern in Java
Abstract Factory might store a set of Prototypes from which to clone and return product objects.
public class FactoryProto {
interface Xyz {
Xyz cloan();
}
static class Tom implements Xyz {
public Xyz cloan() {
return new Tom();
}
public String toString() {
return "ttt";
}
}
static class Dick implements Xyz {
public Xyz cloan() {
return new Dick();
}
public String toString() {
return "ddd";
}
}
static class Harry implements Xyz {
public Xyz cloan() {
return new Harry();
}
public String toString() {
return "hhh";
}
}
static class Factory {
private static java.util.Map prototypes = new java.util.HashMap();
static {
prototypes.put( "tom", new Tom() );
prototypes.put( "dick", new Dick() );
prototypes.put( "harry", new Harry() );
}
public static Xyz makeObject( String s ) {
return ((Xyz)prototypes.get(s)).cloan();
}
}
public static void main( String[] args ) {
for (int i=0; i < args.length; i++) {
System.out.print( Factory.makeObject( args[i] ) + " " );
}
}
}
D:\Java\patterns> java FactoryProto tom dick tom harry tom
ttt ddd ttt hhh ttt
List of Prototype examples
C# examples
C++ examples
Java examples
- Prototype in Java <=[You are here]
- Prototype in Java
PHP examples
|
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License |
