生手学java(12) - java设计模式之工厂模式
新手学java(12) - java设计模式之工厂模式java设计模式-工厂模式小例子产品public interface Work {void d
新手学java(12) - java设计模式之工厂模式
java设计模式-工厂模式小例子
产品public interface Work { void doWork();}ConcreteProduct public class StudentWork implements Work { public void doWork() { System.out.println("学生做作业!"); }}public class TeacherWork implements Work { public void doWork() { System.out.println("老师审批作业!"); }}生产者public interface IWorkFactory { Work getWork();}ConcreteCreator public class StudentWorkFactory implements IWorkFactory { public Work getWork() { return new StudentWork(); }}public class TeacherWorkFactory implements IWorkFactory { public Work getWork() { return new TeacherWork(); }}Test public class Test { public static void main(String[] args) { IWorkFactory studentWorkFactory = new StudentWorkFactory(); studentWorkFactory.getWork().doWork(); IWorkFactory teacherWorkFactory = new TeacherWorkFactory(); teacherWorkFactory.getWork().doWork(); }}