如何創建一個女朋友
@2017-02-14
在第N個情人節,終於可以學以致用,用代碼創建一個Girlfriend過節
語言選用Java,設計模式用工廠模式
import org.apache.log4j.Logger;
/**
* Created by Jianjie Wang on 2017/2/14.
*/
public class GirlfriendTest {
private static Logger LOG = Logger.getLogger(GirlfriendTest.class);
private static Double WU_MAO_QIAN = 0.5;
@org.junit.Test
public void newGirlfriendTest() {
Girlfriend myGirlfriend = null;
try {
System.out.println("Start new a girlfriend...");
Double money = WU_MAO_QIAN;
myGirlfriend = GirlfriendFactory.createGirlfriend(money);
} catch (MoneyNotEnoughException e) {
// FIXME by wangjj: 2017/2/14
System.err.println("Failed: Too yong, too simple:)");
System.exit(-1);
}
myGirlfriend.enjoyValentineDay();
}
static class GirlfriendFactory {
public static Girlfriend createGirlfriend(Double money) throws MoneyNotEnoughException {
if (money < Double.MAX_VALUE) throw new MoneyNotEnoughException();
return new Girlfriend();
}
}
static class Girlfriend {
public void enjoyValentineDay() {
System.out.println("Think too much:)");
}
}
static class MoneyNotEnoughException extends Exception {
}
}
運行結果