The meaning of the design pattern lies in: Facing the business content, the business data structure and the system architecture, the cohesion is low-coupling, graceful the plane logic three-dimensional.
1 PackageDesignpattern;2 3 /**4 * Factory Method mode5 * @authorAdministrator6 */7 Public classA4_factorymethodtest {8 9 /**Ten * Define an interface for creating objects, let subclasses decide which class to instantiate, FactoryMethod to defer instantiation of a class to its subclasses One * Applicable to: A when a class does not know the class of the object it must create. - 2. When a class wants its subclass to specify the object it creates. - 3. When the class delegates the responsibility of creating an object to one of several helper subclasses, and you want to put which helper subclass is the agent this information is localized. the */ - Public Static voidMain (string[] args) { -Iworkfactory studworkfactory=Newstudentworkfactory (); - studworkfactory.getwork (). DoWork (); +Iworkfactory teachworkfactory=Newteacherworkfactory (); - teachworkfactory.getwork (). DoWork (); + } A } at //Product defines the interface of the object created by the factory method - Interface Work - { - voiddoWork (); - } - //concreteproduct implementing the Product interface in classStudentworkImplements Work - { to Public voiddoWork () { +SYSTEM.OUT.PRINTLN ("Students do homework ..."); - } the } * classTeacherworkImplements Work $ {Panax Notoginseng Public voiddoWork () { -System.out.println ("Teacher grading homework ..."); the } + } A //Creator declares a factory method that returns an object of the product type. Creator can also define a default implementation of a factory method, the //it returns a Concreteproduct object that can call the factory method to create a Product object + Interfaceiworkfactory - { $ Work getwork (); $ } - //Concretecreator redefine the factory method to return a Concreteproduct object - classStudentworkfactoryImplementsiworkfactory the { - PublicWork Getwork () {Wuyi return Newstudentwork (); the } - } Wu classTeacherworkfactoryImplementsiworkfactory - { About PublicWork Getwork () { $ return Newteacherwork (); - } -}
Environment: Jdk1.6,maven,tomcat,eclipse
Source Address: Http://files.cnblogs.com/files/xiluhua/designPattern.rar
Welcome to the comments of the pro-members.
Factory method Mode