diff --git a/notes/设计模式.md b/notes/设计模式.md index 3e159d50..af6cf903 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -38,6 +38,8 @@ 拥有设计模式词汇,在沟通时就能用更少的词汇来讨论,并且不需要了解底层细节。 +[源码以及 UML 图](https://github.com/CyC2018/Design-Pattern-Java) + # 二、创建型 ## 1. 单例(Singleton) @@ -523,7 +525,54 @@ abcdefghijklmnopqrstuvwxyz ### 意图 -使用原型实例指定要创建对象的类型;通过复制这个原型来创建新对象。 +使用原型实例指定要创建对象的类型,通过复制这个原型来创建新对象。 + +### 类图 + +

+ +### 实现 + +```java +public abstract class Prototype { + abstract Prototype myClone(); +} +``` + +```java +public class ConcretePrototype extends Prototype { + + private String filed; + + public ConcretePrototype(String filed) { + this.filed = filed; + } + + @Override + Prototype myClone() { + return new ConcretePrototype(filed); + } + + @Override + public String toString() { + return filed; + } +} +``` + +```java +public class Client { + public static void main(String[] args) { + Prototype prototype = new ConcretePrototype("abc"); + Prototype clone = prototype.myClone(); + System.out.println(clone.toString()); + } +} +``` + +```html +abc +``` ### JDK diff --git a/pics/a40661e4-1a71-46d2-a158-ff36f7fc3331.png b/pics/a40661e4-1a71-46d2-a158-ff36f7fc3331.png new file mode 100644 index 00000000..e980a2e3 Binary files /dev/null and b/pics/a40661e4-1a71-46d2-a158-ff36f7fc3331.png differ