UUXPNJP{_MSLQA83{~1W{OE.png

UML

由代码自动生成UML类图,代码看起来繁琐,但照葫芦画瓢,事先学习一些规则,加以简单运用,还是比较好上手的

  1. @startuml
  2. +abstract Device{
  3. {abstract} void work()
  4. }
  5. interface IBrand{
  6. void show();
  7. }
  8. +class ApplePad{
  9. void work();
  10. +void show();
  11. }
  12. class ApplePad implements IBrand
  13. class ApplePad extends Device
  14. +class ApplePhone{
  15. void work();
  16. +void show();
  17. }
  18. class ApplePhone implements IBrand
  19. class ApplePhone extends Device
  20. +class AppleWatch{
  21. void work();
  22. +void show();
  23. }
  24. class AppleWatch implements IBrand
  25. class AppleWatch extends Device
  26. +class HuaweiPad{
  27. void work();
  28. +void show();
  29. }
  30. class HuaweiPad implements IBrand
  31. class HuaweiPad extends Device
  32. +class HuaweiPhone{
  33. void work();
  34. +void show();
  35. }
  36. class HuaweiPhone implements IBrand
  37. class HuaweiPhone extends Device
  38. +class HuaweiWatch{
  39. void work();
  40. +void show();
  41. }
  42. class HuaweiWatch implements IBrand
  43. class HuaweiWatch extends Device
  44. +class MiPad{
  45. void work();
  46. +void show();
  47. }
  48. class MiPad implements IBrand
  49. class MiPad extends Device
  50. +class MiPhone{
  51. void work();
  52. +void show();
  53. }
  54. class MiPhone implements IBrand
  55. class MiPhone extends Device
  56. +class MiWatch{
  57. void work();
  58. +void show();
  59. }
  60. class MiWatch implements IBrand
  61. class MiWatch extends Device
  62. +abstract AbstractIBrandFactory{
  63. {static} void produce(String device,String brand);
  64. {abstract} void produceApple();
  65. {abstract} void produceMi();
  66. {abstract} void produceHuawei();
  67. }
  68. +class PadFactory{
  69. void produceApple();
  70. void produceMi();
  71. void produceHuawei();
  72. }
  73. class PadFactory extends AbstractIBrandFactory
  74. +class PhoneFactory{
  75. void produceApple();
  76. void produceMi();
  77. void produceHuawei();
  78. }
  79. class PhoneFactory extends AbstractIBrandFactory
  80. +class WatchFactory{
  81. void produceApple();
  82. void produceMi();
  83. void produceHuawei();
  84. }
  85. class WatchFactory extends AbstractIBrandFactory
  86. +class FactoryClient{
  87. {static} void main(String[] args);
  88. }
  89. @enduml

Pattern - 图3第二次使用文本绘图绘制UML,打算增加含主要逻辑的客户类,四种类之间的关系,以及美化界面布局 Pattern - 图4选了好久主题,试图美化,好像也并没有变好看

Pattern - 图5

上周五下午上了软件设计模式与体系结构课程,老师简要地介绍了这两个接口,感觉这应该是补充说明的知识,无关考点,课后我去问老师,果真是不用考的。
那我就怀着轻松愉悦的心情,借着这个机会在网站上更新一篇文吧。(非原创,裁缝怪整合的)

Cloneable

用途

Cloneable是标记型的接口,内部没有方法和属性,实现 Cloneable表示该对象能被克隆,能使用Object.clone()方法。如果没有实现 Cloneable的类对象调用clone()就会抛出CloneNotSupportedException。

克隆的分类

  • 浅克隆(shallow clone),浅克隆指拷贝对象时仅仅copy对象本身和对象中的基本变量,而不拷贝对象包含的引用指向的对象。
  • 深克隆(deep clone),不仅copy对象本身,而且copy对象包含的引用指向的所有对象。举例:对象X中包含对Y的引用,Y中包含对Z的引用。浅拷贝X得到X1,X1中依然包含对Y的引用,Y中依然包含对Z的引用。深拷贝则是对浅拷贝的递归,深拷贝X得到X1,X1中包含对Y1(Y的copy)的引用,Y1中包含对Z1(Z的copy)的引用。

    代码

    克隆对象

    1. public class Info implements Cloneable {
    2. private int id;
    3. private String text;
    4. public Info(int id, String text) {
    5. this.id = id;
    6. this.text = text;
    7. }
    8. @Override
    9. public boolean equals(Object obj) {//重写Object类的equals方法
    10. if (this == obj) {
    11. return true;
    12. }
    13. if (obj == null) {
    14. return false;
    15. }
    16. if (obj.getClass()!= getClass()) {
    17. return false;
    18. }
    19. Info temp = (Info) obj;
    20. if (id != temp.id) {
    21. return false;
    22. }
    23. if (text == null) {
    24. if (temp.text != null) {
    25. return false;
    26. }
    27. } else if (!text.equals(temp.text)) {
    28. return false;
    29. }
    30. return true;
    31. }
    32. @Override
    33. protected Object clone() throws CloneNotSupportedException {//返回Object对象
    34. return super.clone();//调用父类的方法
    35. }
    36. public static void main(String[] args) throws CloneNotSupportedException {
    37. Info info1 = new Info(1, "I am ZHQ.");
    38. Info info2 = (Info) info1.clone();
    39. System.out.println(info1.getClass() == info2.getClass());//true
    40. System.out.println(info1 == info2);//false
    41. System.out.println(info1.equals(info2));//true
    42. }
    43. }

    浅克隆

    1. public class MyFile implements Cloneable {
    2. private String path;
    3. private Info info;//MyFile类对象的info字段引用了Info类对象
    4. public MyFile(String path, Info info) {
    5. this.path = path;
    6. this.info = info;
    7. }
    8. @Override
    9. public boolean equals(Object obj) {//感觉可以无视这个重写方法,抓下面的重点
    10. if (this == obj) {
    11. return true;
    12. }
    13. if (obj == null) {
    14. return false;
    15. }
    16. if (obj.getClass()!= getClass()) {
    17. return false;
    18. }
    19. MyFile temp = (MyFile) obj;
    20. if (path == null) {
    21. if (temp.path != null) {
    22. return false;
    23. }
    24. } else if (!path.equals(temp.path)) {
    25. return false;
    26. }
    27. if (info == null) {
    28. if (temp.info != null) {
    29. return false;
    30. }
    31. } else if (!info.equals(temp.info)) {
    32. return false;
    33. }
    34. return true;
    35. }
    36. @Override
    37. protected Object clone() throws CloneNotSupportedException {//感觉常规操作就是浅克隆
    38. return super.clone();
    39. }
    40. public static void main(String[] args) throws CloneNotSupportedException {
    41. Info info = new Info(2, "Hello world.");
    42. MyFile file1 = new MyFile("c:", info);
    43. MyFile file2 = (MyFile) file1.clone();//合理合法的强制类型转换
    44. System.out.println(file1.getClass() == file2.getClass());//true
    45. System.out.println(file1 == file2);//false,比引用
    46. System.out.println(file1.equals(file2));//true,比内容
    47. System.out.println(file1.info.getClass() == file2.info.getClass());//true
    48. System.out.println(file1.info == file2.info);//true
    49. System.out.println(file1.info.equals(file2.info));//true
    50. }
    51. }

    深克隆

    1. public class MyFile2 implements Cloneable {
    2. private String path;
    3. private Info info;
    4. public MyFile2(String path, Info info) {
    5. this.path = path;
    6. this.info = info;
    7. }
    8. @Override
    9. public boolean equals(Object obj) {//可以无视的,意会即可
    10. if (this == obj) {
    11. return true;
    12. }
    13. if (obj == null) {
    14. return false;
    15. }
    16. if (obj.getClass()!= getClass()) {
    17. return false;
    18. }
    19. MyFile2 temp = (MyFile2) obj;
    20. if (path == null) {
    21. if (temp.path != null) {
    22. return false;
    23. }
    24. } else if (!path.equals(temp.path)) {
    25. return false;
    26. }
    27. if (info == null) {
    28. if (temp.info != null) {
    29. return false;
    30. }
    31. } else if (!info.equals(temp.info)) {
    32. return false;
    33. }
    34. return true;
    35. }
    36. @Override
    37. protected Object clone() throws CloneNotSupportedException {//深克隆的本质区别
    38. MyFile2 file = (MyFile2) super.clone();
    39. file.info = (Info) file.info.clone();//手动调用clone方法进行深克隆
    40. return file;
    41. }
    42. public static void main(String[] args) throws CloneNotSupportedException {
    43. Info info = new Info(2, "Hello world.");
    44. MyFile2 file1 = new MyFile2("c:", info);
    45. MyFile2 file2 = (MyFile2) file1.clone();
    46. System.out.println(file1.getClass() == file2.getClass());//true
    47. System.out.println(file1 == file2);//false
    48. System.out.println(file1.equals(file2));//true
    49. System.out.println(file1.info.getClass() == file2.info.getClass());//true
    50. System.out.println(file1.info == file2.info);//false
    51. System.out.println(file1.info.equals(file2.info));//true
    52. }
    53. }

Serializable

持久化

“持久化”意味着对象的“生存时间”并不取决于程序是否正在执行——它存在或“生存”于程序的每一次调用之间。通过序列化一个对象,将其写入磁盘,以后在程序再次调用时重新恢复那个对象,就能圆满实现一种“持久”效果。

说明

  • 对象的序列化处理非常简单,只需对象实现了Serializable 接口即可(该接口仅是一个标记,没有方法)
  • 序列化的对象包括基本数据类型,所有集合类以及其他许多东西,还有Class 对象
  • 对象序列化不仅保存了对象的“全景图”,而且能追踪对象内包含的所有句柄并保存那些对象;接着又能对每个对象内包含的句柄进行追踪(?)
  • 使用transient关键字修饰的的变量,在序列化对象的过程中,该属性不会被序列化

    序列化步骤(?)

  • 首先要创建某些OutputStream对象:OutputStream outputStream = new FileOutputStream(“output.txt”);

  • 将其封装到ObjectOutputStream对象内:ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
  • 此后只需调用writeObject()即可完成对象的序列化,并将其发送给OutputStream:objectOutputStream.writeObject(Object);
  • 最后不要忘记关闭资源:objectOutputStream.close(), outputStream .close();

    反序列化步骤//好像与前文是互异操作

  • 首先要创建某些InputStream对象:InputStream inputStream= new FileInputStream(“output.txt”);

  • 将其封装到ObjectInputStream对象内:ObjectInputStream objectInputStream= new ObjectInputStream(inputStream);
  • 此后只需调用readObject()即可完成对象的反序列化:objectInputStream.readObject();
  • 最后不要忘记关闭资源:objectInputStream.close(),inputStream.close();

生成器模式

这个应该也是不用考的,但老师既然讲了,自己对编码比较感兴趣,现在睡衣朦胧,也动手学习一下吧。
先看示例代码,毕竟老师上课有提到,自己有个模糊的印象。感觉下面的一串代码好有个性,所有类放置在同一个文件中。因为网上的概念没有统一,良莠不齐,所以先看代码,再去找适合自己的概念加以理解。

  1. interface CarPlan {
  2. void setWheel(String wheel);//车轮
  3. void setFrame(String frame);//框架
  4. void setEngine(String engine);
  5. void setWidget(String widget);//微件
  6. }
  7. class Car implements CarPlan {//好像没有设置读取,应该是没必要吧
  8. private String myWheel;
  9. private String myFrame;
  10. private String myEngine;
  11. private String myWidget;
  12. void setWheel(String wheel) {
  13. myWheel = wheel;
  14. }
  15. void setFrame(String frame) {
  16. myFrame = frame;
  17. }
  18. void setEngine(String engine) {
  19. myEngine = engine;
  20. }
  21. void setWidget(String widget) {
  22. myWidget = widgetl
  23. }
  24. }
  1. interface Builder {
  2. void buildWheel();
  3. void buildFrame();
  4. void buildEngine();
  5. void buildWidget();
  6. Car getCar();//生成汽车
  7. }
  8. //一个廉价车生成器
  9. class CheapBuilder implements Builder {
  10. Car cheapCar = new Car();//实例化
  11. Car getCar() {//重写
  12. return cheapCar;
  13. }
  14. void buildWheel() {
  15. cheapCar.setWheel("cheap wheel");
  16. }
  17. void buildFrame() {
  18. cheapCar.setFrame("cheap frame");
  19. }
  20. void buildEngine() {
  21. cheapCar.setEngine("cheap engine");
  22. }
  23. void buildWidget() {
  24. cheapCar.setWidget("no widget");//微件是直接没有
  25. }
  26. }
  27. //一个平价车生成器,平价还是比廉价好
  28. class EconomicBuilder implements Builder {
  29. Car economicCar = new Car();
  30. Car getCar() {
  31. return economicCar;
  32. }
  33. void buildWheel() {
  34. cheapCar.setWheel("cheap wheel");
  35. }
  36. void buildFrame() {
  37. cheapCar.setFrame("expensive frame");
  38. }
  39. void buildEngine() {
  40. cheapCar.setEngine("cheap engine");
  41. }
  42. void buildWidget() {
  43. cheapCar.setWidget("economic widget");
  44. }
  45. }
  1. class Director {
  2. private Builder builder;
  3. director(Builder bld) {
  4. builder = bld;
  5. }
  6. void produceCar() {
  7. //这里对步骤进行控制
  8. builder.buildFrame();
  9. builder.buildWidget();
  10. builder.buildWheel();
  11. builder.buildEngine();
  12. }
  13. Car getCar() {
  14. builder.getCar();
  15. }
  16. }
  1. public class Test {//并没有觉得这可以称之为一种设计模式
  2. public static void main(String[] args) {
  3. Builder bld = new EconomicBuilder();
  4. Director director = new Director(bld);
  5. director.produceCar();
  6. Car car = director.getCar();
  7. }

定义: 将一个复杂对象的构建与它的表示分离,使同样的构建过程可以创建不同的表示。
一个形象的例子:原谅我暂时get不到
李嘉诚的遗嘱执行:
财产(产品角色):李嘉诚拥有众多复杂的财产框架,这里以现金与物品入例。
遗嘱(建造者):相当于建造者,分配现金与物品。
具体遗嘱(具体建造者):1.给大儿子的财产分配,2,给小儿子的财产分配。
律师(指导者角色):按照具体的遗嘱指令分配财产。