当前位置:   article > 正文

软件设计模式---原型模式---邮箱复制(浅克隆和深克隆案例)_邮件克隆

邮件克隆

实例一:邮件复制(浅克隆) 由于邮件对象包含的内容较多(如发送者、接收者、标题、内容、日期、附件等),某系统中现需要提供一个邮件复制功能,对于已经创建好的邮件对象,可以通过复制的方式创建一个新的邮件对象,如果需要改变某部分内容,无须修改原始的邮件对象,只需要修改复制后得到的邮件对象即可。使用原型模式设计该系统。在本实例中使用浅克隆实现邮件复制,即复制邮件(Email)的同时不复制附件(Attachment)。

 

  1. Attachment类:
  2. public class Attachment
  3. {
  4. public void download()
  5. {
  6. System.out.println("下载附件");
  7. }
  8. }
  1. 客户端类:
  2. public class Client
  3. {
  4. public static void main(String a[])
  5. {
  6. Email email,copyEmail;
  7. email=new Email();
  8. copyEmail=(Email)email.clone();
  9. System.out.println("email==copyEmail?");
  10. System.out.println(email==copyEmail);
  11. System.out.println("email.getAttachment==copyEmail.getAttachment?");
  12. System.out.println(email.getAttachment()==copyEmail.getAttachment());
  13. }
  14. }
  1. Email类:
  2. public class Email implements Cloneable
  3. {
  4. private Attachment attachment=null;
  5. public Email()
  6. {
  7. this.attachment=new Attachment();
  8. }
  9. public Object clone()
  10. {
  11. Email clone=null;
  12. try
  13. {
  14. clone=(Email)super.clone();
  15. }
  16. catch(CloneNotSupportedException e)
  17. {
  18. System.out.println("Clone failure!");
  19. }
  20. return clone;
  21. }
  22. public Attachment getAttachment()
  23. {
  24. return this.attachment;
  25. }
  26. public void display()
  27. {
  28. System.out.println("查看邮件");
  29. }
  30. }

--------------------------------------------浅克隆结束-------------------------------------------------------------------------------------

 

实例二:邮件复制(深克隆)

 

  1. import java.io.*;
  2. public class Attachment implements Serializable
  3. {
  4. public void download()
  5. {
  6. System.out.println("下载附件");
  7. }
  8. }

 

  1. import java.io.*;
  2. public class Email implements Serializable
  3. {
  4. private Attachment attachment=null;
  5. public Email()
  6. {
  7. this.attachment=new Attachment();
  8. }
  9. public Object deepClone() throws IOException, ClassNotFoundException, OptionalDataException
  10. {
  11. //将对象写入流中
  12. ByteArrayOutputStream bao=new ByteArrayOutputStream();
  13. ObjectOutputStream oos=new ObjectOutputStream(bao);
  14. oos.writeObject(this);
  15. //将对象从流中取出
  16. ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
  17. ObjectInputStream ois=new ObjectInputStream(bis);
  18. return(ois.readObject());
  19. }
  20. public Attachment getAttachment()
  21. {
  22. return this.attachment;
  23. }
  24. public void display()
  25. {
  26. System.out.println("查看邮件");
  27. }
  28. }
  1. 客户端类:
  2. public class Client
  3. {
  4. public static void main(String a[])
  5. {
  6. Email email,copyEmail=null;
  7. email=new Email();
  8. try{
  9. copyEmail=(Email)email.deepClone();
  10. }
  11. catch(Exception e)
  12. {
  13. e.printStackTrace();
  14. }
  15. System.out.println("email==copyEmail?");
  16. System.out.println(email==copyEmail);
  17. System.out.println("email.getAttachment==copyEmail.getAttachment?");
  18. System.out.println(email.getAttachment()==copyEmail.getAttachment());
  19. }
  20. }

 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号