Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. class Complex {
  2. int[] nums = {1,2,3,4,5};
  3. public Complex clone() {
  4. return new Complex();//this line create a new object, so is it violate the objective of prototype ?//
  5. }
  6. }
  7.  
  8. class Test2 {
  9. Complex c1 = new Complex();
  10. Complex makeCopy() {
  11. return (Complex)c1.clone();// Is it actually create a new object ? based on the clone method in Complex class? //
  12. }
  13. public static void main(String[] args) {
  14. Test2 tp = new Test2();
  15. Complex c2 = tp.makeCopy();
  16. }
  17. }
  18.  
  19. public Complex clone(){
  20. Complex clone = (Complex)super.clone();
  21. clone.nums = this.nums;
  22. return clone;
  23. }
  24.  
  25. import java.awt.Dimension;
  26. import java.io.Serializable;
  27.  
  28. /**
  29. * This class also acts as a factory for creating prototypical objects.
  30. */
  31. public class PageBanner implements Serializable, Cloneable {
  32. private String slogan;
  33. private String image;
  34. private String font;
  35. private Dimension dimension;
  36.  
  37. // have prototype banner from which to derive all other banners
  38. private static final PageBanner PROTOTYPE = new PageBanner("",
  39. "blank.png", "Verdana", new Dimension(600, 45));
  40.  
  41. PageBanner(String slogan, String image, String font,
  42. Dimension dim) {
  43. this.slogan = slogan;
  44. this.image = image;
  45. //... other assignments
  46. }
  47.  
  48. // getters and setters..
  49.  
  50. public String toString() {
  51. return new StringBuilder("PageBanner[")
  52. .append("Slogan=").append(slogan)
  53. .append("Image=").append(image)
  54. .append("Font=").append(font)
  55. .append("Dimensions=").append(dimension)
  56. .toString();
  57.  
  58. }
  59.  
  60. protected Object clone() {
  61. Object cln = null;
  62. try {
  63. cln = super.clone();
  64. }catch(CloneNotSupportedException e) {
  65. // ignore, will never happen
  66. }
  67. return cln;
  68. }
  69.  
  70. /**
  71. * This is the creational method that uses the prototype banner
  72. * to create banners and changes it slightly (setting slogan and image)
  73. */
  74. public static PageBanner createSloganBanner(String slogan, String image) {
  75. PageBanner banner = (PageBanner) PROTOTYPE.clone();
  76. banner.slogan = slogan;
  77. banner.image = image;
  78. return banner;
  79. }
  80.  
  81. /**
  82. * Another creational method that uses the prototype banner
  83. * to create banners and changes it slightly (setting image)
  84. */
  85. public static PageBanner createImageBanner(String image) {
  86. PageBanner banner = (PageBanner) PROTOTYPE.clone();
  87. banner.image = image;
  88. return banner;
  89. }
  90.  
  91. // similarly you can have a number of creational methods with
  92. // different parameters for different types of banners that
  93. // vary slightly in their properties.
  94.  
  95. // main... (for illustration)
  96. public static void main(String[] args) {
  97. // both these banners are created from same prototypical instance
  98. PageBanner slogan = PageBanner.createSloganBanner(
  99. "Stackoverflow Rocks", "stack.png");
  100. PageBanner img = PageBanner.createImageBanner("stackBanner.png");
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement