package test;
class 多例设计模式测试 {
//构造方法私有化
private 多例设计模式测试(String title) {
this.title=title;
}
//实例化对象
private static final 多例设计模式测试 MALE=new 多例设计模式测试("男");
private static final 多例设计模式测试 FEMALE=new 多例设计模式测试("女");
//属性私有化
private String title;
//取得外部对象的方法
public static 多例设计模式测试 getInstance(String msg) {
switch(msg) {
case "male":
return MALE;
case "female":
return FEMALE;
default:
return null;
}
}
//getter方法
public String getTitile() {
return title;
}
}
public class 多例设计模式{
public static void main(String[] args) {
多例设计模式测试 多例=多例设计模式测试.getInstance("male");
System.out.println(多例.getTitile());
}
}