Prototype Pattern in Java 2 : Prototype Pattern : Design Pattern : Java examples (example source code) Organized by topic

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. JSP
19. JSTL
20. Language Basics
21. Network Protocol
22. PDF RTF
23. Regular Expressions
24. Security
25. Servlets
26. Spring
27. Swing Components
28. Swing JFC
29. SWT JFace Eclipse
30. Threads
31. Tiny Application
32. Velocity
33. XML
Java Tutorial
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
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.