UML
由代码自动生成UML类图,代码看起来繁琐,但照葫芦画瓢,事先学习一些规则,加以简单运用,还是比较好上手的
@startuml+abstract Device{{abstract} void work()}interface IBrand{void show();}+class ApplePad{void work();+void show();}class ApplePad implements IBrandclass ApplePad extends Device+class ApplePhone{void work();+void show();}class ApplePhone implements IBrandclass ApplePhone extends Device+class AppleWatch{void work();+void show();}class AppleWatch implements IBrandclass AppleWatch extends Device+class HuaweiPad{void work();+void show();}class HuaweiPad implements IBrandclass HuaweiPad extends Device+class HuaweiPhone{void work();+void show();}class HuaweiPhone implements IBrandclass HuaweiPhone extends Device+class HuaweiWatch{void work();+void show();}class HuaweiWatch implements IBrandclass HuaweiWatch extends Device+class MiPad{void work();+void show();}class MiPad implements IBrandclass MiPad extends Device+class MiPhone{void work();+void show();}class MiPhone implements IBrandclass MiPhone extends Device+class MiWatch{void work();+void show();}class MiWatch implements IBrandclass MiWatch extends Device+abstract AbstractIBrandFactory{{static} void produce(String device,String brand);{abstract} void produceApple();{abstract} void produceMi();{abstract} void produceHuawei();}+class PadFactory{void produceApple();void produceMi();void produceHuawei();}class PadFactory extends AbstractIBrandFactory+class PhoneFactory{void produceApple();void produceMi();void produceHuawei();}class PhoneFactory extends AbstractIBrandFactory+class WatchFactory{void produceApple();void produceMi();void produceHuawei();}class WatchFactory extends AbstractIBrandFactory+class FactoryClient{{static} void main(String[] args);}@enduml
第二次使用文本绘图绘制UML,打算增加含主要逻辑的客户类,四种类之间的关系,以及美化界面布局
选了好久主题,试图美化,好像也并没有变好看
上周五下午上了软件设计模式与体系结构课程,老师简要地介绍了这两个接口,感觉这应该是补充说明的知识,无关考点,课后我去问老师,果真是不用考的。
那我就怀着轻松愉悦的心情,借着这个机会在网站上更新一篇文吧。(非原创,裁缝怪整合的)
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)的引用。
代码
克隆对象
public class Info implements Cloneable {private int id;private String text;public Info(int id, String text) {this.id = id;this.text = text;}@Overridepublic boolean equals(Object obj) {//重写Object类的equals方法if (this == obj) {return true;}if (obj == null) {return false;}if (obj.getClass()!= getClass()) {return false;}Info temp = (Info) obj;if (id != temp.id) {return false;}if (text == null) {if (temp.text != null) {return false;}} else if (!text.equals(temp.text)) {return false;}return true;}@Overrideprotected Object clone() throws CloneNotSupportedException {//返回Object对象return super.clone();//调用父类的方法}public static void main(String[] args) throws CloneNotSupportedException {Info info1 = new Info(1, "I am ZHQ.");Info info2 = (Info) info1.clone();System.out.println(info1.getClass() == info2.getClass());//trueSystem.out.println(info1 == info2);//falseSystem.out.println(info1.equals(info2));//true}}
浅克隆
public class MyFile implements Cloneable {private String path;private Info info;//MyFile类对象的info字段引用了Info类对象public MyFile(String path, Info info) {this.path = path;this.info = info;}@Overridepublic boolean equals(Object obj) {//感觉可以无视这个重写方法,抓下面的重点if (this == obj) {return true;}if (obj == null) {return false;}if (obj.getClass()!= getClass()) {return false;}MyFile temp = (MyFile) obj;if (path == null) {if (temp.path != null) {return false;}} else if (!path.equals(temp.path)) {return false;}if (info == null) {if (temp.info != null) {return false;}} else if (!info.equals(temp.info)) {return false;}return true;}@Overrideprotected Object clone() throws CloneNotSupportedException {//感觉常规操作就是浅克隆return super.clone();}public static void main(String[] args) throws CloneNotSupportedException {Info info = new Info(2, "Hello world.");MyFile file1 = new MyFile("c:", info);MyFile file2 = (MyFile) file1.clone();//合理合法的强制类型转换System.out.println(file1.getClass() == file2.getClass());//trueSystem.out.println(file1 == file2);//false,比引用System.out.println(file1.equals(file2));//true,比内容System.out.println(file1.info.getClass() == file2.info.getClass());//trueSystem.out.println(file1.info == file2.info);//trueSystem.out.println(file1.info.equals(file2.info));//true}}
深克隆
public class MyFile2 implements Cloneable {private String path;private Info info;public MyFile2(String path, Info info) {this.path = path;this.info = info;}@Overridepublic boolean equals(Object obj) {//可以无视的,意会即可if (this == obj) {return true;}if (obj == null) {return false;}if (obj.getClass()!= getClass()) {return false;}MyFile2 temp = (MyFile2) obj;if (path == null) {if (temp.path != null) {return false;}} else if (!path.equals(temp.path)) {return false;}if (info == null) {if (temp.info != null) {return false;}} else if (!info.equals(temp.info)) {return false;}return true;}@Overrideprotected Object clone() throws CloneNotSupportedException {//深克隆的本质区别MyFile2 file = (MyFile2) super.clone();file.info = (Info) file.info.clone();//手动调用clone方法进行深克隆return file;}public static void main(String[] args) throws CloneNotSupportedException {Info info = new Info(2, "Hello world.");MyFile2 file1 = new MyFile2("c:", info);MyFile2 file2 = (MyFile2) file1.clone();System.out.println(file1.getClass() == file2.getClass());//trueSystem.out.println(file1 == file2);//falseSystem.out.println(file1.equals(file2));//trueSystem.out.println(file1.info.getClass() == file2.info.getClass());//trueSystem.out.println(file1.info == file2.info);//falseSystem.out.println(file1.info.equals(file2.info));//true}}
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();
生成器模式
这个应该也是不用考的,但老师既然讲了,自己对编码比较感兴趣,现在睡衣朦胧,也动手学习一下吧。
先看示例代码,毕竟老师上课有提到,自己有个模糊的印象。感觉下面的一串代码好有个性,所有类放置在同一个文件中。因为网上的概念没有统一,良莠不齐,所以先看代码,再去找适合自己的概念加以理解。
interface CarPlan {void setWheel(String wheel);//车轮void setFrame(String frame);//框架void setEngine(String engine);void setWidget(String widget);//微件}class Car implements CarPlan {//好像没有设置读取,应该是没必要吧private String myWheel;private String myFrame;private String myEngine;private String myWidget;void setWheel(String wheel) {myWheel = wheel;}void setFrame(String frame) {myFrame = frame;}void setEngine(String engine) {myEngine = engine;}void setWidget(String widget) {myWidget = widgetl}}
interface Builder {void buildWheel();void buildFrame();void buildEngine();void buildWidget();Car getCar();//生成汽车}//一个廉价车生成器class CheapBuilder implements Builder {Car cheapCar = new Car();//实例化Car getCar() {//重写return cheapCar;}void buildWheel() {cheapCar.setWheel("cheap wheel");}void buildFrame() {cheapCar.setFrame("cheap frame");}void buildEngine() {cheapCar.setEngine("cheap engine");}void buildWidget() {cheapCar.setWidget("no widget");//微件是直接没有}}//一个平价车生成器,平价还是比廉价好class EconomicBuilder implements Builder {Car economicCar = new Car();Car getCar() {return economicCar;}void buildWheel() {cheapCar.setWheel("cheap wheel");}void buildFrame() {cheapCar.setFrame("expensive frame");}void buildEngine() {cheapCar.setEngine("cheap engine");}void buildWidget() {cheapCar.setWidget("economic widget");}}
class Director {private Builder builder;director(Builder bld) {builder = bld;}void produceCar() {//这里对步骤进行控制builder.buildFrame();builder.buildWidget();builder.buildWheel();builder.buildEngine();}Car getCar() {builder.getCar();}}
public class Test {//并没有觉得这可以称之为一种设计模式public static void main(String[] args) {Builder bld = new EconomicBuilder();Director director = new Director(bld);director.produceCar();Car car = director.getCar();}
定义: 将一个复杂对象的构建与它的表示分离,使同样的构建过程可以创建不同的表示。
一个形象的例子:原谅我暂时get不到
李嘉诚的遗嘱执行:
财产(产品角色):李嘉诚拥有众多复杂的财产框架,这里以现金与物品入例。
遗嘱(建造者):相当于建造者,分配现金与物品。
具体遗嘱(具体建造者):1.给大儿子的财产分配,2,给小儿子的财产分配。
律师(指导者角色):按照具体的遗嘱指令分配财产。
