Factory method Mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.