Factory mode of "design mode"

Source: Internet
Author: User

Directory

    • I have seen
    • Structural block diagram
    • Code instance
    • Summarize
Back to the top of what I see back to the top structure block diagram

Back to top code instance
Interface mcdonald{void cooking ();} Class Hamburger implements mcdonald{@Overridepublic void cooking () {System.out.println ("@-------mm Eat hamburger [email  Protected] ");}} Class Cola implements mcdonald{@Overridepublic void cooking () {System.out.println ("@-------mm drink Cola [email  Protected] ");}} Class Frenchfries implements mcdonald{@Overridepublic void cooking () {System.out.println ("@-------mm eat French fries [email  Protected] ");}} public class Factory {public Mcdonald Getfood (String type) {if (type==null) {return null;} if (Type.equals ("Hamburger")) {return new Hamburger ();} else if (type.equals ("Cola")) {return new Cola ();} else if (type.equals ("Frenchfries")) {return new frenchfries ();} return null;} public static void Main (string[] args) {Factory Factory = new Factory (); System.out.println ("@-------mm want to eat hamburger [email protected]"); Mcdonald m1 = Factory.getfood ("Hamburger"); m1.cooking (); System.out.println ("@-------mm want to drink cola [email protected]"); Mcdonald m2 =factory.getfood ("Cola"); m2.cooking (); System.out.println ("@-------mm want to eat French fries [email protected] "); Mcdonald m3 = Factory.getfood ("frenchfries"); m3.cooking ();}}

Results

@-------mm want to eat hamburger [email protected]@-------mm eat hamburger [email protected]@-------mm want to drink cola [email protected]@-------mm drink cola [email protected]@-------mm want to eat French fries [email protected]@-------mm eat french fries [email protected]

  

Back to top of the summary

Factory mode of "design 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.