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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Design Pattern » Prototype PatternScreenshots 
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
w_w__w_.___j_av__a_2s___.__c_o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.