Java Reference
In-Depth Information
The RunPattern class demonstrates the use of this pattern by creating an Address object, then duplicating that
object by calling its copy method. The fact that the Address objects return two different hash code values
(numeric values that represent unique object identity) further confirms that the copy operation has produced a
different object from the first.
Example A.28 RunPattern.java
1. public class RunPattern{
2. public static void main(String [] arguments){
3. System.out.println("Example for Prototype pattern");
4. System.out.println();
5. System.out.println("This example will create an Address object,");
6. System.out.println(" which it will then duplicate by calling the");
7. System.out.println(" object's clone method.");
8. System.out.println();
9.
10. System.out.println("Creating first address.");
11. Address address1 = new Address("8445 Silverado Trail", "Rutherford", "CA", "91734");
12. System.out.println("First address created.");
13. System.out.println(" Hash code = " + address1.hashCode());
14. System.out.println(address1);
15. System.out.println();
16.
17. System.out.println("Creating second address using the clone() method.");
18. Address address2 = (Address)address1.copy();
19. System.out.println("Second address created.");
20. System.out.println(" Hash code = " + address2.hashCode());
21. System.out.println(address2);
22. System.out.println();
23.
24.
25. }
26. }
Search WWH ::




Custom Search