Skip to content

Commit bd69c2f

Browse files
authored
原型模式
原型模式
1 parent 4fa3d0f commit bd69c2f

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.java.design.prototype;
2+
3+
public class ConcreatePrototype extends Prototype {
4+
5+
public ConcreatePrototype(String name) {
6+
setName(name);
7+
}
8+
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.java.design.prototype;
2+
3+
public class Prototype implements Cloneable {
4+
5+
private String name;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
15+
@Override
16+
protected Object clone() throws CloneNotSupportedException {
17+
return super.clone();
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.java.design.prototype;
2+
3+
/**
4+
* 原型模式 -----> 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
5+
*
6+
* @author Administrator
7+
*
8+
*/
9+
public class PrototypePattern {
10+
11+
public static void main(String[] args) throws CloneNotSupportedException {
12+
13+
Prototype prototype = new ConcreatePrototype("Folger");
14+
Prototype prototype2 = (Prototype) prototype.clone();
15+
16+
System.out.println(prototype + " " + prototype.getName());
17+
System.out.println(prototype2 + " " + prototype2.getName());
18+
}
19+
20+
}

0 commit comments

Comments
 (0)