设计模式(java版)——创建型——简单工厂
项目源码已提交github:https://github.com/ckl001/designPattem_javapackage com.Ljava.design.pattem.creational.simpleFactory;/*** @Auther 20173* @Date 2019-4-8 14:53* @Des 视频(抽象)**/public abstract cl...
·
项目源码已提交github:https://github.com/ckl001/designPattem_java
package com.Ljava.design.pattem.creational.simpleFactory;
/**
* @Auther 20173
* @Date 2019-4-8 14:53
* @Des 视频(抽象)
**/
public abstract class Video {
public abstract void produce();
}
package com.Ljava.design.pattem.creational.simpleFactory;
/**
* @Auther 20173
* @Date 2019-4-8 14:53
* @Des java视频
**/
public class JavaVideo extends Video {
@Override
public void produce() {
System.out.println("生产java视频");
}
}
package com.Ljava.design.pattem.creational.simpleFactory;
/**
* @Auther 20173
* @Date 2019-4-8 14:57
* @Des py视频
**/
public class PythonVideo extends Video {
@Override
public void produce() {
System.out.println("生产python视频");
}
}
package com.Ljava.design.pattem.creational.simpleFactory;
/**
* @Auther 20173
* @Date 2019-4-8 14:58
* @Des 视频工厂类
**/
public class VideoFactory {
public Video getVideo(Class c){
Video video = null;
try{
//通过反射拿到instance
video = (Video) Class.forName(c.getName()).newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return video;
}
}
package com.Ljava.design.pattem.creational.simpleFactory;
/**
* @Auther 20173
* @Date 2019-4-8 15:06
* @Des TODO
**/
public class Test {
public static void main(String[] args) {
VideoFactory videoFactory = new VideoFactory();
Video video = videoFactory.getVideo(JavaVideo.class);
if(video == null){
return;
}
video.produce();
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)