原型模式是创造型模式的一种,因此他提供了对象创建的机制

该种模式主要用于:当你需要创建某个对象时,而刚好已经存在与该对象类似的对象,但是同时
不同用已经存在的对象,因为你也想对新的对象做某些定制,此时你就可以使用原型模式咯

Java中是实现对象的clone 方法来实现原型模式的啦

下面这个例子中,我们已经存在一个对象,该对象中的数据需要load from db,此时程序中需要多次对这些数据进行修改,且相互不影响, 此时我们使用原型模式,先创建一个对象,再将对象clone 成多份,避免了每创建一个对象就重新 load data from db

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.walterlife.dp.ProtoTypeDP;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class Employees implements Cloneable {
static Logger logger = Logger.getLogger(Employees.class.getName());

private List<String> empList;

public Employees(List<String> empList) {
if(empList == null) {
this.empList = new ArrayList<String>();
logger.warn("empList is null!!!");
} else {
this.empList = empList;
}
}

public Employees() {
this.empList = new ArrayList<String>();
}

public List<String> getEmpList() {
return this.empList;
}

public void loadData() {
if(empList == null) {
empList = new ArrayList<String>();
logger.warn("empList is null");
}
// load data from db
empList.add("walter");
empList.add("hello");
empList.add("world");
}

public Object clone() throws CloneNotSupportedException {
List<String> tmp = new ArrayList<String>();

if(empList == null) {
logger.warn("empList is null");
return tmp;
}

for(String emp: empList) {
tmp.add(emp);
}

return new Employees(tmp);
}
}

实现 clone 方法,并且是对象的深拷贝

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.walterlife.dp;

import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.walterlife.dp.ProtoTypeDP.Employees;

public class App {
static Logger logger = Logger.getLogger(App.class.getName());

public static void main( String[] args ) {

logger.setLevel((Level)(Level.DEBUG));

Employees emp = new Employees();
emp.loadData();
logger.info("emp emps: " + emp.getEmpList());

Employees empNew;
try {
empNew = (Employees) emp.clone();
List<String> list = empNew.getEmpList();
if(list != null) {
list.add("hehe");
} else {
logger.error("empNew-list is null!!!");
}
logger.info("empNew emps: " + empNew.getEmpList());

Employees empNew1 = (Employees)emp.clone();

if(empNew1 != null) {
List<String> list1 = empNew1.getEmpList();
if(list1 != null) {
if(list1.indexOf("hello") != -1) {
list1.remove("hello");
} else {
logger.warn("key hello not exist!!");
}
logger.info("empNew1 emps: " + empNew1.getEmpList());
} else {
logger.error("empNew1-list is null!!!");
}
} else {
logger.error("empNew1 is null!!!");
}
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}

运行结果

1
2
3
2015-08-23 10:52:58 INFO  com.walterlife.dp.App main:19 -> emp emps: [walter, hello, world]
2015-08-23 10:52:58 INFO com.walterlife.dp.App main:31 -> empNew emps: [walter, hello, world, hehe]
2015-08-23 10:52:58 INFO com.walterlife.dp.App main:46 -> empNew1 emps: [walter, world]

留言

Aug 22 2015