-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandFactory.java
More file actions
50 lines (41 loc) · 1.49 KB
/
Copy pathCommandFactory.java
File metadata and controls
50 lines (41 loc) · 1.49 KB
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
package com.company;
import com.company.commands.Command;
import com.company.exceptions.NoSuchCommandException;
import com.company.exceptions.LoadPropertiesException;
import java.util.Properties;
public class CommandFactory {
private static volatile CommandFactory instance;
private final Properties properties = new Properties();
private CommandFactory() {
try {
properties.load(CommandFactory.class.getResourceAsStream("config.properties"));
}
catch (Exception e) {
throw new LoadPropertiesException("Problem with load properties file");
}
}
public static CommandFactory getInstance(){
if (instance == null) {
synchronized (CommandFactory.class) {
if (instance == null) {
instance = new CommandFactory();
}
}
}
return instance;
}
public Command createCommand(String commandName)
{
String commandClassName = properties.getProperty(commandName);
if (commandClassName == null)
throw new NoSuchCommandException(commandName);
Command command;
try {
Class<?> commandClass = Class.forName(commandClassName);
command = (Command) commandClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new NoSuchCommandException(commandName);
}
return command;
}
}