Prototype Pattern in Java 2 : Prototype Pattern « Design Pattern « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Prototype Pattern 




Prototype Pattern in Java 2

//[C] 2002 Sun Microsystems, Inc.---


public class RunPrototypePattern {
  public static void main(String[] arguments) {
    System.out.println("Example for Prototype pattern");
    System.out.println();
    System.out.println("This example will create an Address object,");
    System.out.println(" which it will then duplicate by calling the");
    System.out.println(" object's clone method.");
    System.out.println();

    System.out.println("Creating first address.");
    Address address1 = new Address("8445 Silverado Trail""Rutherford",
        "CA""91734");
    System.out.println("First address created.");
    System.out.println("    Hash code = " + address1.hashCode());
    System.out.println(address1);
    System.out.println();

    System.out.println("Creating second address using the clone() method.");
    Address address2 = (Addressaddress1.copy();
    System.out.println("Second address created.");
    System.out.println("    Hash code = " + address2.hashCode());
    System.out.println(address2);
    System.out.println();

  }
}

interface Copyable {
  public Object copy();
}

class Address implements Copyable {
  private String type;

  private String street;

  private String city;

  private String state;

  private String zipCode;

  public static final String EOL_STRING = System
      .getProperty("line.separator");

  public static final String COMMA = ",";

  public static final String HOME = "home";

  public static final String WORK = "work";

  public Address(String initType, String initStreet, String initCity,
      String initState, String initZip) {
    type = initType;
    street = initStreet;
    city = initCity;
    state = initState;
    zipCode = initZip;
  }

  public Address(String initStreet, String initCity, String initState,
      String initZip) {
    this(WORK, initStreet, initCity, initState, initZip);
  }

  public Address(String initType) {
    type = initType;
  }

  public Address() {
  }

  public String getType() {
    return type;
  }

  public String getStreet() {
    return street;
  }

  public String getCity() {
    return city;
  }

  public String getState() {
    return state;
  }

  public String getZipCode() {
    return zipCode;
  }

  public void setType(String newType) {
    type = newType;
  }

  public void setStreet(String newStreet) {
    street = newStreet;
  }

  public void setCity(String newCity) {
    city = newCity;
  }

  public void setState(String newState) {
    state = newState;
  }

  public void setZipCode(String newZip) {
    zipCode = newZip;
  }

  public Object copy() {
    return new Address(street, city, state, zipCode);
  }

  public String toString() {
    return "\t" + street + COMMA + " " + EOL_STRING + "\t" + city + COMMA
        " " + state + " " + zipCode;
  }
}

           
       














Related examples in the same category
1.Prototype pattern in Java
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.