Prototype Patterns set the kinds of objects to create using a prototypical instance.
Prototype Patterns create new objects by copying this prototype.
import java.util.Random; abstract class Phone implements Cloneable { public String modelname; public int price; public String getModelname() { return modelname; }//from www .ja v a2s . co m public void setModelname(String modelname) { this.modelname = modelname; } public static int setPrice() { int price = 0; Random r = new Random(); int p = r.nextInt(100000); price = p; return price; } public Phone clone() throws CloneNotSupportedException { return (Phone) super.clone(); } } class Android extends Phone { public Android(String m) { modelname = m; } @Override public Phone clone() throws CloneNotSupportedException { return (Android) super.clone(); } } class Iphone extends Phone { public Iphone(String m) { modelname = m; } @Override public Phone clone() throws CloneNotSupportedException { return (Iphone) super.clone(); } } public class Main { public static void main(String[] args) throws CloneNotSupportedException { Phone iPhoneBase = new Iphone("Green Iphone"); iPhoneBase.price = 100000; Phone androidBasic = new Android("Android Yellow"); androidBasic.price = 500000; Phone bc1 = iPhoneBase.clone(); bc1.price = iPhoneBase.price + Phone.setPrice(); System.out.println("Phone is: " + bc1.modelname + " and price is " + bc1.price); bc1 = androidBasic.clone(); bc1.price = androidBasic.price + Phone.setPrice(); System.out.println("Phone is: " + bc1.modelname + " and price is " + bc1.price); } }