Simple factory: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
56
57
58
59import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactory {
private static Properties env = new Properties();
static{
try {
//Step 1: get IO Stream
InputStream inputStream = BeanFactory.class.getResourceAsStream("/applicationContext.properties");
//Step 2: load() method reads a property list (key and element pairs) from the input byte stream.
env.load(inputStream);
//Stream source doesn't belong to JVM, so it can't be handled by GC and needs to be closed mannualy
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
Way to initial an object:
1. Constructor: UserService userService = new UserServiceImpl();
2. Refelction: This method can loose coupling
Class clazz = Class.forName("com.baizhiedu.basic.UserServiceImpl");
UserService userService = (UserService)clazz.newInstance();
*/
public static UserService getUserService() {
UserService userService = null;
try {
Class clazz = Class.forName(env.getProperty("userService"));
userService = (UserService) clazz.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return userService;
}
public static UserDAO getUserDAO(){
UserDAO userDAO = null;
try {
Class clazz = Class.forName(env.getProperty("userDAO"));
userDAO = (UserDAO) clazz.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return userDAO;
}
}
applicationContext.properties:1
2
3
4
5
6
7# Properties extends Hashtable<Object,Object>
# Map: key=String value=String
# Properties [userService = com.baizhiedu.xxx.UserServiceImpl]
# Properties.getProperty("userService")
userService = com.baizhiedu.basic.UserServiceImpl
userDAO = com.baizhiedu.basic.UserDAOImpl
General factory
There are lots of redundancy in simple factory like those exceptions in try-catch
. Here introduced general factory: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
28import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactory {
private static Properties env = new Properties();
static{
try {
InputStream inputStream = BeanFactory.class.getResourceAsStream("/applicationContext.properties");
env.load(inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object getBean(String key){
Object ret = null;
try {
Class clazz = Class.forName(env.getProperty(key));
ret = clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}
General factory shares the same applicationContext.properties
with simple factory.
How to use general factory:
- Define class
- Inform the factory using
applicationContext.properties
- Get object using BeanFactroy:
1
Object ret = BeanFactory.getBean("key");