?? 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(); } }
原文地址:http://blog.csdn.net/sd_tz_wzg/article/details/40507427