この道はあなたにとって快適ですか?
public class AreaCalculator {
private int length = 10;
private int width = 10;
private static final AreaCalculator areaCalculator = new AreaCalculator();
private AreaCalculator() {
// TODO Auto-generated constructor stub
}
public static AreaCalculator getInstance() {
return areaCalculator;
}
public AreaCalculator fillLength(int length) {
this.length = length;
return getInstance();
}
public AreaCalculator fillWidth(int width) {
this.width = width;
return getInstance();
}
public AreaCalculator fillAll(int length, int width) {
this.length = length;
this.width = width;
return getInstance();
}
public int calculateArea() {
if (!isInit()) {
throw new RuntimeException("Should Init first!");
}
return length * width;
}
public boolean isInit() {
// add something
return true;
}
テストコード
public class TestMain {
public static void main(String[] args) {
int result = AreaCalculator.getInstance().fillWidth(20).calculateArea();
System.out.println("10*20=" + result);
result = AreaCalculator.getInstance().fillWidth(10).fillLength(10).calculateArea();
System.out.println("10*10=" + result);
result = AreaCalculator.getInstance().fillAll(10, 30).calculateArea();
System.out.println("10*30=" + result);
}
**
構成インターフェースの使用。
**
ConfigInf(lib側)
public interface ConfigInf {
public static String F_LENGTH = "length";
public static String F_WIDTH = "width";
public String getProperty(String key);
}
ConfigFactory(lib側)
public class ConfigFactory {
private static ConfigInf config = null;
public static void init(ConfigInf config) {
ConfigFactory.config = config;
}
public static ConfigInf getConfig() {
if (config == null) {
throw new NullPointerException("Config should be init first");
}
return config;
}
}
ClientConfig (クライアント側)
public class ClientConfig implements ConfigInf {
String value = "10";
@Override
public String getProperty(String key) {
// read configFile
if (ConfigInf.F_LENGTH == key) {
return value;
} else {
return "100";
}
}
public void update() {
value = "100";
}
}
AreaCalculatorWithConfig(lib側)
public class AreaCalculatorWithConfig {
private int length = 10;
private int width = 10;
//
private static final AreaCalculatorWithConfig areaCalculator = new AreaCalculatorWithConfig();
private AreaCalculatorWithConfig() {
// TODO Auto-generated constructor stub
}
public static AreaCalculatorWithConfig getInstance() {
return areaCalculator;
}
public int calculateArea() {
ConfigInf config = ConfigFactory.getConfig();
length = getInt(config.getProperty(ConfigInf.F_LENGTH));
width = getInt(config.getProperty(ConfigInf.F_WIDTH));
return length * width;
}
public static int getInt(String value) {
return Integer.parseInt(value);
}
}
TestMainWithConfig (クライアント側)
public class TestMainWithConfig {
public static void main(String[] args) {
// init;
ClientConfig config = new ClientConfig();
ConfigFactory.init(config);
//
System.out.println("Before update:"
+ AreaCalculatorWithConfig.getInstance().calculateArea());
config.update();
System.out.println("After update:"
+ AreaCalculatorWithConfig.getInstance().calculateArea());
}
}