배경
프로토타입 모드와 빌더 모드는 개발에 자주 사용되는 생성 모드에 속하므로 이 두 가지 디자인 모드를 작성하는 방법은 다음 코드 데모입니다.
프로토타입 패턴
프로토타입 패턴의 핵심은 복사
package com.example.prototype.prototype;
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class PrototypePattern implements Cloneable,Serializable {
private int age;
private String name;
private List<String> list;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
protected Object clone() throws CloneNotSupportedException {
//浅克隆
return super.clone();
}
protected Object clone2() throws CloneNotSupportedException {
PrototypePattern prototypePattern= (PrototypePattern) super.clone();
prototypePattern.setList((List<String>) ((ArrayList<String>)list).clone());
//也可以变成深克隆,不过对象内的应用类型要一个一个进行clone操作
return prototypePattern;
}
@Override
public String toString() {
return "PrototypePattern{" +
"age=" + age +
", name='" + name + '\'' +
", list=" + list +
'}';
}
//深克隆
protected Object deepClone() throws CloneNotSupportedException {
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
try {
ObjectOutputStream oos=new ObjectOutputStream(byteArrayOutputStream);
oos.writeObject(this);
ByteArrayInputStream bis=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bis);
return ois.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws CloneNotSupportedException {
PrototypePattern prototypePattern=new PrototypePattern();
prototypePattern.setAge(1);
prototypePattern.setName("小明");
List<String> list1=new ArrayList<>();
list1.add("数学");
list1.add("英语");
list1.add("语文");
prototypePattern.setList(list1);
System.out.println(prototypePattern.toString());
PrototypePattern clone1= (PrototypePattern) prototypePattern.clone();
PrototypePattern clone2= (PrototypePattern) prototypePattern.clone2();
PrototypePattern clone3= (PrototypePattern) prototypePattern.deepClone();
prototypePattern.getList().remove(2);
System.out.println(clone1.toString());
System.out.println(clone2.toString());
System.out.println(clone3.toString());
}
}
빌더 모드 표준 쓰기
복잡한 객체는 일반적으로 빌더 모드를 통해 생성되며 다양한 설정 매개 변수에 따라 다양한 객체를 구성할 수 있습니다.
package com.example.prototype.build;
public class BuildPattern {
private int max;
private int min;
private Long ts;
private String operate;
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public Long getTs() {
return ts;
}
public void setTs(Long ts) {
this.ts = ts;
}
public String getOperate() {
return operate;
}
public void setOperate(String operate) {
this.operate = operate;
}
}
package com.example.prototype.build;
public class BuildPatternBuilder {
BuildPattern buildPattern=new BuildPattern();
public BuildPattern setMax(int max){
buildPattern.setMax(max);
return buildPattern;
}
public BuildPattern setMin(int min){
buildPattern.setMin(min);
return buildPattern;
}
public BuildPattern setTs(Long ts){
buildPattern.setTs(ts);
return buildPattern;
}
}