您的位置:首页 > 其它

工厂方法

2016-03-22 13:56 253 查看
要得到一个对象,不用new ,而是通过工厂方法得到目标对象的实例

public class ISample {

}

public class SampleA extends ISample{

}

public class SampleB extends ISample{

}

public class Factory {

    public static ISample create(int i){

        switch (i) {

        case 1:

            return new SampleA();

        case 2:

            return new SampleB();

        default:

            break;

        }

        return null;

    }

}

public class Test {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        ISample sample = Factory.create(1);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: