/**
* 多例设计模式:类的能够实例化对象的个数是提前固定好的
* @author JIASHIXI
*
*/
class Sex{
private String value;
public static final int MALE = 0;
public static final int FAMALE = 1;
private static final Sex MALE_INSTANCE = new Sex("男");
private static final Sex FAMALE_INSTANCE = new Sex("女");
private Sex(String value) {
this.value = value;
}
public static Sex getInstance(int choose) {
switch(choose) {
case MALE:
return MALE_INSTANCE;
case FAMALE:
return FAMALE_INSTANCE;
default:
return null;
}
}
public String toString() {
return this.value;
}
}
public class Test {
public static void main(String[] args) {
Sex sex = Sex.getInstance(Sex.MALE);
System.out.println(sex);
}
}
举例说明Java中的多例设计模式
最新推荐文章于 2022-08-03 11:30:26 发布