Design Pattern---Factory mode---produce fruit

Source: Internet
Author: User

Design Mode---Factory mode

Requires a basic abstract class: quite a basic factory

A number of specific classes are required: quite a few products

Need tool class: quite a plant in the machine, used to choose different product production

Need main class: the order that the quite customer wants

Main class----> factory class----> Product class---> Product specific class

The boss chooses the right item in the tool pile according to the customer's order, then goes to the factory to produce the suitable product, returns to the customer

Advantage: The ability to add the required classes and methods without modifying the original code situation.

Insufficient: Will increase the number of class files

Interface: The content provided, equivalent to the protocol, that is, when the outside world uses this interface, the content of the interface is not allowed to modify the content of the interface, but the content of the interface can be materialized,

From the code, the code in the interface class is public static final, static and common

Products-Java interface classes: protocols

1 /**2  *3  * @authorSows4  */5  Public InterfaceProduct {6     7     voidproduct Type ();8     9     voidtaste ();Ten      One     voidpacking form (); A      -  - @Override the String toString (); -}
Product

The specific type of product _ Apple. The specific type of Java product _ Strawberry. Specific types of Java products _ bananas. Java Specific class: Product realization to meet product agreement

1 /**2  *3  * @authorSows4  */5  Public classSpecific type of product _ AppleImplementsProduct {6 7     PrivateString Productmanager;8     PrivateString Chewinggum;9     PrivateString Wrapper;Ten      One @Override A      Public voidProduct Type () { -productmanager= "apple cider Vinegar"; -     } the  - @Override -      Public voidtaste () { -Chewinggum= "The light taste of sour acid"; +     } -      + @Override A      Public voidPackage form () { atWrapper= "in the form of plastic bottles"; -     } -      -      PublicString toString () { -         return"Apple" + "\ T" +productmanager+ "\ T" +chewinggum+ "\ T" +wrapper -+ "\ n------------------------------------------------"; in     } -}
specific type of product _ Apple
1 /**2  *3  * @authorSows4  */5  Public classSpecific types of products _ StrawberryImplementsProduct {6 7     PrivateString Productmanager;8     PrivateString Chewinggum;9     PrivateString Wrapper;Ten      One @Override A      Public voidProduct Type () { -Productmanager= "Strawberry Heap"; -     } the  - @Override -      Public voidtaste () { -chewinggum= "Strawberry Flavor"; +     } -  + @Override A      Public voidPackage form () { atwrapper= "Transparent rectangular box"; -     } -  -          PublicString toString () { -         return"Strawberry" + "\ T" +productmanager+ "\ T" +chewinggum+ "\ T" +wrapper -+ "\ n------------------------------------------------"; in     } -}
Specific types of products _ Strawberry
1 /**2  *3  * @authorSows4  */5  Public classSpecific type of product _ BananaImplementsProduct {6 7     PrivateString Productmanager;8     PrivateString Chewinggum;9     PrivateString Wrapper;Ten          One @Override A      Public voidProduct Type () { -productmanager= "Banana slices"; -     } the  - @Override -      Public voidtaste () { -Chewinggum= "The smell of the strange."; +     } -  + @Override A      Public voidPackage form () { atWrapper= "film-mounted"; -     } -      -      PublicString toString () { -         return"Banana" + "\ T" +productmanager+ "\ T" +chewinggum+ "\ T" +wrapper -+ "\ n------------------------------------------------"; in     } -      to}
specific type of product _ Banana

Factory. Java

1 /**2  *3  * @authorSows4  */5  Public classFactory {6 7      PublicProduct tool (String sows) {8 9         if(Sows.equalsignorecase (NULL)) {Ten             return NULL; One}Else if(Sows.equalsignorecase ("Apple"))) { ASpecific type of product _ Apple Apple =Newthe specific type of product _ Apple (); - apple. Product Type (); - Apple. Packaging form (); the apple. Taste (); -             returnApple; -}Else if(Sows.equalsignorecase ("Strawberry")) { -The specific type of product _ Strawberry CEO =Newthe specific type of product _ Strawberry (); + CEO. Product type (); - CEO. Packing form (); + CEO. Taste (); A             returnCEO; at}Else if(Sows.equalsignorecase ("Banana")) { -The specific type of product _ Banana CEO =Newthe specific type of product _ Banana (); - CEO. Product type (); - CEO. Packing form (); - CEO. Taste (); -             returnCEO; in         } -  to         return NULL; +     } -}
Factory

Shop. Java

1 /**2  *3  * @authorSows4  */5  Public classShop {6      Public Static voidMain (string[] args) {7Factory sows=NewFactory ();8Product ceo=sows. Tools ("Apple"));9 System.out.println (ceo.tostring ());Ten          OneCeo=sows. Tools ("Strawberry"); A System.out.println (ceo.tostring ()); -          -Ceo=sows. Tool ("Banana"); the System.out.println (ceo.tostring ()); -     } -}
Shop

Show Results:

Run
Apple Cider acetic acid light taste in the form of plastic bottles
------------------------------------------------
Strawberry Strawberry Heap Strawberry flavor transparent rectangular box
------------------------------------------------
Banana banana slices Weird taste film pack
------------------------------------------------
Build successfully (Total time: 0 seconds)

Design Pattern---Factory mode---produce fruit

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.