|
|
@@ -0,0 +1,81 @@
|
|
|
+package cn.qinys.learn.creational.prototype;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 具体原型,实现深拷贝
|
|
|
+ */
|
|
|
+public class ConcretePrototype implements Prototype {
|
|
|
+ private String id;
|
|
|
+ private String data;
|
|
|
+ private Map<String, String> config = new HashMap<>();
|
|
|
+
|
|
|
+ public ConcretePrototype() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public ConcretePrototype(String id, String data) {
|
|
|
+ this.id = id;
|
|
|
+ this.data = data;
|
|
|
+ }
|
|
|
+
|
|
|
+ // copy constructor for deep clone
|
|
|
+ protected ConcretePrototype(ConcretePrototype other) {
|
|
|
+ this.id = other.id;
|
|
|
+ this.data = other.data;
|
|
|
+ this.config = new HashMap<>(other.config);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Prototype clonePrototype() {
|
|
|
+ return new ConcretePrototype(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getId() {
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setId(String id) {
|
|
|
+ this.id = id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getData() {
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setData(String data) {
|
|
|
+ this.data = data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, String> getConfig() {
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setConfig(Map<String, String> config) {
|
|
|
+ this.config = config;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean equals(Object o) {
|
|
|
+ if (this == o) return true;
|
|
|
+ if (o == null || getClass() != o.getClass()) return false;
|
|
|
+ ConcretePrototype that = (ConcretePrototype) o;
|
|
|
+ return Objects.equals(id, that.id) && Objects.equals(data, that.data) && Objects.equals(config, that.config);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int hashCode() {
|
|
|
+ return Objects.hash(id, data, config);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return "ConcretePrototype{" +
|
|
|
+ "id='" + id + '\'' +
|
|
|
+ ", data='" + data + '\'' +
|
|
|
+ ", config=" + config +
|
|
|
+ '}';
|
|
|
+ }
|
|
|
+}
|
|
|
+
|