Design Patterns 2 traveling with me --- simple factory mode simplefactory

Source: Internet
Author: User

 
Simple factory mode simplefactory

The simple factory mode is also called the static method mode (because the factory class defines a static method). A factory class determines which product class instance to create based on the input parameters.

Change the demandEncapsulationTo oneClassTo reduce the coupling between objects.

// Encapsulate the frequently changed requirements into the public class simplefactorycooker of the simple factory class simplefactorycooker {// defines the static method cook public static food cook (string type) {// define the object to be returned and assign the value to null food = NULL; // switch (type) {Case "tomato": Food = new tomatoes (); break; Case "potato": Food = new potatoes (); break ;}// return the target object return food ;}}

Application scenarios:

    • When the factory class is responsible for creating fewer objects, you can consider using the simple factory mode;

    • If you only know the parameters passed into the factory class, you can consider using the simple factory mode when you do not care about how to create the object logic.

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.