原型模式
Posted linbq1911
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原型模式相关的知识,希望对你有一定的参考价值。
1.定义:指原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象;
不需要知道任何创建的细节,不调用构造函数
2.类型:创建型
3.适用场景:类初始化消耗较多资源;new产生的一个对象需要非常繁琐的过程(数据准备、访问权限等);
构造函数比较复杂;循环体中产生大量对象时。
4.优点:原型模式性能比直接new一个对象性能高;简化创建过程。
5.缺点:必须配备克隆方法;对克隆复杂对象或对克隆出的对象进行复杂改造时,容易引入风险;
深拷贝、浅拷贝要运用得当。
6.扩展:深克隆;浅克隆。
7.实例目录package
8.实例UML类图
9.代码
1 package com.geely.design.pattern.creational.prototype; 2 3 public class Mail implements Cloneable{ 4 private String name; 5 private String emailAddress; 6 private String content; 7 public Mail(){ 8 System.out.println("Mail Class Constructor"); 9 } 10 11 public String getName() { 12 return name; 13 } 14 15 public void setName(String name) { 16 this.name = name; 17 } 18 19 public String getEmailAddress() { 20 return emailAddress; 21 } 22 23 public void setEmailAddress(String emailAddress) { 24 this.emailAddress = emailAddress; 25 } 26 27 public String getContent() { 28 return content; 29 } 30 31 public void setContent(String content) { 32 this.content = content; 33 } 34 35 @Override 36 public String toString() { 37 return "Mail{" + 38 "name=‘" + name + ‘‘‘ + 39 ", emailAddress=‘" + emailAddress + ‘‘‘ + 40 ", content=‘" + content + ‘‘‘ + 41 ‘}‘+super.toString(); 42 } 43 44 @Override 45 protected Object clone() throws CloneNotSupportedException { 46 System.out.println("clone mail object"); 47 return super.clone(); 48 } 49 }
1 package com.geely.design.pattern.creational.prototype; 2 3 import java.text.MessageFormat; 4 5 public class MailUtil { 6 public static void sendMail(Mail mail){ 7 String outputContent = "向{0}同学,邮件地址:{1},邮件内容:{2}发送邮件成功。"; 8 System.out.println(MessageFormat.format(outputContent,mail.getName(),mail.getEmailAddress(),mail.getContent())); 9 } 10 public static void saveOriginMailRecord(Mail mail){ 11 System.out.println("存储originMail记录,originMail:"+mail.getContent()); 12 } 13 }
1 package com.geely.design.pattern.creational.prototype; 2 3 public class Test { 4 public static void main(String[] args) throws CloneNotSupportedException { 5 Mail mail = new Mail(); 6 mail.setContent("初始化模板"); 7 System.out.println("初始化mail:"+mail); 8 for(int i = 0;i < 10; i++){ 9 Mail mailTemp =(Mail) mail.clone(); 10 mailTemp.setName("姓名" + i); 11 mailTemp.setEmailAddress("姓名"+i+"@imooc.com"); 12 mailTemp.setContent("恭喜你,此次慕课网活动中奖了"); 13 MailUtil.sendMail(mailTemp); 14 System.out.println("克隆的mailTemp:"+mailTemp); 15 } 16 MailUtil.saveOriginMailRecord(mail); 17 } 18 }
1 package com.geely.design.pattern.creational.prototype.abstractprototype; 2 3 public abstract class A implements Cloneable{ 4 @Override 5 protected Object clone() throws CloneNotSupportedException { 6 return super.clone(); 7 } 8 }
1 package com.geely.design.pattern.creational.prototype.abstractprototype; 2 3 public class B extends A { 4 public static void main(String[] args) throws CloneNotSupportedException { 5 B b = new B(); 6 b.clone(); 7 } 8 }
1 package com.geely.design.pattern.creational.prototype.clone; 2 3 import java.util.Date; 4 5 public class Pig implements Cloneable{ 6 private String name; 7 private Date birthday; 8 9 public Pig(String name, Date birthday) { 10 this.name = name; 11 this.birthday = birthday; 12 } 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public Date getBirthday() { 23 return birthday; 24 } 25 26 public void setBirthday(Date birthday) { 27 this.birthday = birthday; 28 } 29 30 @Override 31 protected Object clone() throws CloneNotSupportedException { 32 Pig pig = (Pig)super.clone(); 33 pig.birthday = (Date)pig.birthday.clone(); 34 return pig; 35 } 36 37 @Override 38 public String toString() { 39 return "Pig{" + 40 "name=‘" + name + ‘‘‘ + 41 ", birthday=" + birthday + 42 ‘}‘+ super.toString(); 43 } 44 }
1 package com.geely.design.pattern.creational.prototype.clone; 2 3 import java.util.Date; 4 5 public class Test { 6 public static void main(String[] args) throws CloneNotSupportedException { 7 Date birthday = new Date(0L); 8 Pig pig1 = new Pig("佩奇", birthday); 9 Pig pig2 = (Pig)pig1.clone(); 10 System.out.println(pig1); 11 System.out.println(pig2); 12 System.out.println(pig1 == pig2); 13 pig1.getBirthday().setTime(66666666666666L); 14 System.out.println(pig1); 15 System.out.println(pig2); 16 System.out.println(pig1 == pig2); 17 } 18 }
以上是关于原型模式的主要内容,如果未能解决你的问题,请参考以下文章