标签:
1 //接口的应用:工厂方法的设计模式 2 public class TestFactoryMethod { 3 public static void main(String[] args) { 4 IWorkFactory i = new StudentWorkFactory(); 5 i.getWork().doWork(); 6 7 IWorkFactory i1 = new TeacherWorkFactory(); 8 i1.getWork().doWork(); 9 } 10 } 11 12 interface IWorkFactory{ 13 Work getWork(); 14 } 15 class StudentWorkFactory implements IWorkFactory{ 16 17 @Override 18 public Work getWork() { 19 return new StudentWork(); 20 } 21 22 } 23 class TeacherWorkFactory implements IWorkFactory{ 24 25 @Override 26 public Work getWork() { 27 return new TeacherWork(); 28 } 29 30 } 31 32 interface Work{ 33 void doWork(); 34 } 35 36 class StudentWork implements Work{ 37 38 @Override 39 public void doWork() { 40 System.out.println("学生写作业"); 41 } 42 43 } 44 class TeacherWork implements Work{ 45 46 @Override 47 public void doWork() { 48 System.out.println("老师批改作业"); 49 } 50 51 }
标签:
原文地址:http://www.cnblogs.com/longc/p/5962903.html