多例模式

单例模式:是指一个类只能返回它唯一的实例
多例模式:是指一类根据不同的条件,返回不同的,唯一实例。

1.构造器私有
2.对象实例私有
3.向外提供公开静态的实例访问接口
4.定义标记接口

public class Person(){
    private static char sex;
    private Person(char sex){
        this.sex =sex;
    }
    private static finl Person man = new Person("男");
    private static finl Person woman = new Person("女");
interface Choose(){
    public int MAN=1;
    public int WOMAN=2;
    
}  
    public static Person newinstance(int sex){
        switch(sex){
            case Choose.MAN:retrun nam;
            case Choose.WOMAN:retrun woman;
            default:return null;
        }
    }
}

posted @ 2016-12-07 11:03  goumingming  阅读(277)  评论(0编辑  收藏  举报