首页 > 代码库 > 设计模式之工厂模式
设计模式之工厂模式
?? interface Work{ public void doWork(); } class StudentWork implements Work{ @Override public void doWork() { System.out.println("student 写作业"); } } class TeacherWork implements Work{ @Override public void doWork() { System.out.println("teacher 批改作业"); } } interface WorkFactory{ public Work getWork(); } class StudentWorkFactory implements WorkFactory{ @Override public Work getWork() { return new StudentWork(); } } class TeacherWorkFactory implements WorkFactory{ @Override public Work getWork() { return new TeacherWork(); } } public class TestFactory { /** * @Title: main * @Description: * @param: * @return void * @user: wangzg * @Date:2014-10-27 * @throws */ public static void main(String[] args) { // TODO Auto-generated method stub WorkFactory workFactory1 = new StudentWorkFactory(); workFactory1.getWork().doWork(); System.out.println(); WorkFactory workFactory2 = new TeacherWorkFactory(); workFactory2.getWork().doWork(); } }
设计模式之工厂模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。