首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式(四) | 简历复印与原型模型不得不说的一些事

设计模式(四) | 简历复印与原型模型不得不说的一些事

作者头像
谭庆波
发布2018-08-10 11:02:24
2050
发布2018-08-10 11:02:24
举报
文章被收录于专栏:轮子工厂轮子工厂

原型模型

  • 原型模式其实就是从一个对象基础上再创建另外一个可定制的对象,而且不需要知道任何创建的细节。
  • 原型模型可以大大提高效率。一般在初始化的信息不发生变化的情况下,克隆是最好的办法,即隐藏了创建对象的细节,又对性能有大大的提升。
  • 看代码就知道怎么回事了。以书写简历为例:
    1. public class Resume implements Cloneable{
    2. String name;
    3. String sex;
    4. String age;
    5. String timearea;
    6. String company;
    7. public Resume(String name) {
    8. super();
    9. this.name = name;
    10. }
    11. public Resume() {
    12. super();
    13. }
    14. //设置个人信息
    15. public void setPersonInfo(String sex,String age){
    16. this.sex = sex;
    17. this.age = age;
    18. }
    19. //设置个人信息
    20. public void setWorkExperience(String timearea,String company){
    21. this.timearea = timearea;
    22. this.company = company;
    23. }
    24. @Override
    25. public String toString() {
    26. return name + " " + sex + " " + age + "\n工作经历:" + timearea + " "
    27. + company;
    28. }
    29. public Object Clone() throws Exception{
    30. return this.clone();
    31. }
    32. }
    33. public class Test {
    34. public static void main(String[] args) throws Exception {
    35. Resume a = new Resume("大鸟");
    36. a.setPersonInfo("男", "20岁");
    37. a.setWorkExperience("2013-2017", "东北林业大学");
    38. Resume b = (Resume)a.Clone();
    39. b.setWorkExperience("2017-2022", "哈尔滨工业大学");
    40. Resume c = (Resume)a.Clone();
    41. c.setWorkExperience("2022-2030", "google");
    42. System.out.println(a);
    43. System.out.println(b);
    44. System.out.println(c);
    45. }
    46. }
  • 输出结果:
    1. 大鸟 20岁
    2. 工作经历:2013-2017 东北林业大学
    3. 大鸟 20岁
    4. 工作经历:2017-2020 哈尔滨工业大学
    5. 大鸟 20岁
    6. 工作经历:2020-2022 google
  • 注:上述代码是原型模型的浅复制,只能复制值类型的数据,对于引用类型的对象不能复制。
  • 如果将工作经历也单独做一个类,然后在resume类中应用工作经历,就会输出3条一模一样的结果。
  • 浅复制被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都任指向原来的对象。
  • 将工作经历也单独做一个类的代码:
    1. public class WorkExperience {
    2. String timearea;
    3. String company;
    4. public String getTimearea() {
    5. return timearea;
    6. }
    7. public void setTimearea(String timearea) {
    8. this.timearea = timearea;
    9. }
    10. public String getCompany() {
    11. return company;
    12. }
    13. public void setCompany(String company) {
    14. this.company = company;
    15. }
    16. @Override
    17. public String toString() {
    18. return "\n工作经历" + timearea + " " + company;
    19. }
    20. }
    21. public class Resume implements Cloneable{
    22. String name;
    23. String sex;
    24. String age;
    25. WorkExperience work;
    26. public Resume(String name) {
    27. super();
    28. this.name = name;
    29. work = new WorkExperience();
    30. }
    31. public Resume() {
    32. super();
    33. }
    34. //设置个人信息
    35. public void setPersonInfo(String sex,String age){
    36. this.sex = sex;
    37. this.age = age;
    38. }
    39. public void setWorkExperience(String timearea,String company){
    40. work.timearea = timearea;
    41. work.company = company;
    42. }
    43. @Override
    44. public String toString() {
    45. return name + " " + sex + " " + age + work ;
    46. }
    47. public Object Clone() throws Exception{
    48. return this.clone();
    49. }
    50. }
  • 输出结果
    1. 大鸟 20岁
    2. 工作经历:2020-2022 google
    3. 大鸟 20岁
    4. 工作经历:2020-2022 google
    5. 大鸟 20岁
    6. 工作经历:2020-2022 google
  • 深复制:把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
  • 二层深复制的代码:
    1. public class WorkExperience implements Cloneable{
    2. String timearea;
    3. String company;
    4. public String getTimearea() {
    5. return timearea;
    6. }
    7. public void setTimearea(String timearea) {
    8. this.timearea = timearea;
    9. }
    10. public String getCompany() {
    11. return company;
    12. }
    13. public void setCompany(String company) {
    14. this.company = company;
    15. }
    16. @Override
    17. public String toString() {
    18. return "\n工作经历" + timearea + " " + company;
    19. }
    20. public Object Clone() throws Exception{
    21. return this.clone();
    22. }
    23. }
    24. public class Resume implements Cloneable{
    25. String name;
    26. String sex;
    27. String age;
    28. WorkExperience work;
    29. public Resume(String name) {
    30. super();
    31. this.name = name;
    32. work = new WorkExperience();
    33. }
    34. public Resume() {
    35. super();
    36. }
    37. private Resume(WorkExperience work) throws Exception {
    38. super();
    39. this.work = (WorkExperience)work.Clone();
    40. }
    41. //设置个人信息
    42. public void setPersonInfo(String sex,String age){
    43. this.sex = sex;
    44. this.age = age;
    45. }
    46. public void setWorkExperience(String timearea,String company){
    47. work.timearea = timearea;
    48. work.company = company;
    49. }
    50. @Override
    51. public String toString() {
    52. return name + " " + sex + " " + age + work ;
    53. }
    54. public Object Clone() throws Exception{
    55. Resume obj = new Resume(this.work);
    56. obj.name = this.name;
    57. obj.age = this.age;
    58. obj.sex = this.sex;
    59. return obj;
    60. }
    61. }
  • 输出结果:
    1. 大鸟 29岁
    2. 工作经历2013-2017 东北林业大学
    3. 大鸟 29岁
    4. 工作经历2017-2022 哈尔滨工业大学
    5. 大鸟 29岁
    6. 工作经历2020-2030 google
  • 代码改动的地方:
    • 让WorkExperience类也实现了Cloneable的接口,并增加了clone()方法。
    • 在resume类中新增了一个私有的构造方法。
    • 修改了resume的clone()的方法。

往期回顾

设计模式(一) | 啥是工厂模式和策略模式?

设计模式(二) | 装饰模式---穿什么有这么重要?

设计模式(三) | 为别人做嫁衣---代理模式

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-09-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 轮子工厂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原型模型
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档