Junior — Middle
Can you share your experience with implementing or using design patterns?
sobes.tech AI
Answer from AI
In one of my Java projects, I used the Singleton pattern to manage access to the application's configuration parameters. This ensured that settings are loaded only once and are accessible throughout the program.
I also applied the Factory pattern to create objects of different types based on input data, which simplified extending functionality without modifying existing code.
Example of using Singleton:
public class Configuration {
private static Configuration instance;
private Properties props;
private Configuration() {
props = new Properties();
// load settings
}
public static synchronized Configuration getInstance() {
if (instance == null) {
instance = new Configuration();
}
return instance;
}
public String getProperty(String key) {
return props.getProperty(key);
}
}