Design patterns of factory methods for Java design patterns

Source: Internet
Author: User
Tags lenovo

1. Definition

Defines an interface for creating an object, letting subclasses decide which class to instantiate, FactoryMethod the instantiation of a class to its subclasses.

2. Awareness Map

3. Code examples

Lenovo factory creates Lenovo's computer, Dell creates Dell's computer

1) Computer interface

/** * Computer interface * [email protected] *   */  Public Interface Computer {    /**     * Description --and a word about @param  Description  Descriptive language      */     Public void describe (String description);}

2). Dell Computer

/** * Dell PC * [email protected] *   */  Public class Implements Computer {    @Override    publicvoid  describe (String description) {        System.out.println (description);    }    }

3). Lenovo Computer

/** * Lenovo Computer * [email protected] *   */  Public class Implements Computer {    @Override    publicvoid  describe (String description) {        System.out.println (description);    }    }

4). Computer Factory

/*** Computer Factory * [email protected] * December 25, 2017*/ Public classComputerfactory { Public StaticComputer Buildcomputer (String pcname) {if("Dell". Equalsignorecase (Pcname))return NewDellcomputer (); if("Lenovo". Equalsignorecase (Pcname))return NewLenovocomputer (); return NULL; }}

5). Computer Operation class

 /**   * Computer Operation class, Call factory to create PC * [email protected] * December 25, 2017  */ public  class   Computeroperator {
     /**   * Create PC *   @param   pcname computer name *   @param  Span style= "COLOR: #008000" > description computer description  */ public  void   Createcomputer (    String pcname,string description) {computerfactory.buildcomputer (pcname). Describe (description); }}

6). Client class

/** * client * [email protected] *   */  Public class Client {    publicstaticvoid  main (string[] args) {         New computeroperator ();        Computeroperation.createcomputer ("Lenovo", "I am Lenovo");        Computeroperation.createcomputer ("Dell", "I Am Dell");}    }

7). Results

I'm Lenovo, I'm Dale.

4. Summary

When to choose?

1). One interface multiple implementations, but do not know the specific implementation of which, the subclass to specify

2). The system design itself assigns the implementation class to the subclass

Design patterns of factory methods for Java design patterns

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.