1 //application of the interface: the design pattern of the factory method2 public classTestfactorymethod {3 public Static voidmain (string[] Args) {4Iworkfactory i =Newstudentworkfactory ();5 i.getwork (). doWork ();6 7Iworkfactory I1 =Newteacherworkfactory ();8 i1.getwork (). doWork ();9 }Ten } one a Interfaceiworkfactory{ - work getwork (); - } the classStudentworkfactoryImplementsiworkfactory{ - - @Override - publicwork getwork () { + return Newstudentwork (); - } + a } at classTeacherworkfactoryImplementsiworkfactory{ - - @Override - publicwork getwork () { - return Newteacherwork (); - } in - } to + Interfacework{ - voiddoWork (); the } * $ classStudentworkImplementswork{Panax Notoginseng - @Override the public voiddoWork () { +System.out.println ("students Write homework"); a } the + } - classTeacherworkImplementswork{ $ $ @Override - public voiddoWork () { -SYSTEM.OUT.PRINTLN ("teacher Grading homework"); the } - Wuyi}
Java Factory Design Patterns