Create mode -- factory method mode, create type -- Factory

Source: Internet
Author: User

Create mode -- factory method mode, create type -- Factory
 Overview

Defines an interface used to create objects, so that the subclass determines which class to instantiate. FactoryMethod delays the instantiation of a class to its subclass.
  Applicability
1. 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 multiple help sub-classes and you want to localize the information of which help sub-classes are proxies.
  Participants
1. Product defines the interface of the object created by the factory method. 2. ConcreteProduct implements the Product interface. 3. Creator declares the factory method, which returns an object of the Product type. Creator can also define the default implementation of a factory method, which returns a default ConcreteProduct object. You can call the factory method to create a Product object. 4. ConcreteCreator redefines the factory method to return a ConcreteProduct instance.

Class diagram:

Code Description:
/*** Defines the interface of the object created by the factory method. * @ Author Linhai Gu **/public interface IWork {void doWork ();}


<Strong> <span style = "font-size: 18px;">/*** implements the IWork interface. * @ Author Linhai Gu **/public class CoderWork implements IWork {@ Overridepublic void doWork () {// TODO Auto-generated method stubSystem. out. println ("programmers are desperately writing code and submitting projects! ") ;}}</Span> </strong>



<Strong> <span style = "font-size: 18px;">/*** implements the IWork interface. * @ Author Linhai Gu **/public class ManagerWork implements IWork {@ Overridepublic void doWork () {// TODO Auto-generated method stubSystem. out. println ("the project manager reviews the project and goes online! ") ;}}</Span> </strong>




<Strong> <span style = "font-size: 18px;">/*** declares the factory method. This method returns an IWork object. * You can call the factory method to create an IWork object. * @ Author Linhai Gu **/public interface IWorkFactory {IWork getWork () ;}</span> </strong>



<Strong> <span style = "font-size: 18px;">/*** redefine the factory method to return a specific instance. * @ Author Linhai Gu **/public class CoderWorkFactory implements IWorkFactory {@ Overridepublic IWork getWork () {// TODO Auto-generated method stubreturn new CoderWork ();}} </span> </strong>



<Strong> <span style = "font-size: 18px;">/*** redefine the factory method to return a specific instance. * @ Author Linhai Gu **/public class ManagerWorkFactory implements IWorkFactory {@ Overridepublic IWork getWork () {// TODO Auto-generated method stubreturn new ManagerWork ();}} </span> </strong>



<Strong> <span style = "font-size: 18px; ">/*** test * @ author Linhai Gu **/public class MainTest {public static void main (String [] args) {// TODO Auto-generated method stubCoderWorkFactory coderWorkFactory = new CoderWorkFactory (); ManagerWorkFactory managerWorkFactory = new ManagerWorkFactory (); coderWorkFactory. getWork (). doWork (); managerWorkFactory. getWork (). doWork () ;}</span> </strong>

Running result:








Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/41413431 sentiment control _

Related Article

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.