工厂方法
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);
}
}
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);
}
}
相关文章推荐
- SecurityContextHolder.getContext().getAuthentication()为null的问题
- 第一个uwp程序-- owlGZTV 上架商店
- The type org.apache.http.Header cannot be resolved. 的解决办法
- Swift中页面逆向传值
- PAM(3)
- mysql 5.7 配置安装
- Andriod GridView不改变背景色实现网格线效果
- python 异常处理学习笔记
- [bzoj3503] [Cqoi2014]和谐矩阵
- 页面设计 | 思考
- JPA-style positional param was not an integral ordinal
- PAM(二)
- maven项目编译的时候,拒绝访问
- nginx学习心得
- JNI_最简单的Java调用C/C++代码
- iOS中传感器的基本使用
- react-lifecycle-parent-child.jsx(自github)
- 深入理解HTTP协议(转)http协议学习系列
- 压缩和解压缩工具
- box-sizing的学习和认识