"Design Mode" Factory method

Source: Internet
Author: User

"Design Mode" Factory method

1. Create objects from the corresponding factory

2. Compared to a simple factory, code design complexity increases, calls become complex, but maintenance costs are reduced, and each time a new class is added, the corresponding factory is added without the need to modify the original code. Simple Factory each time you add a new class, you need to modify the original method of creating the object.

3. More in line with the opening and shutting principle

4. Code

    PublicInterfaceifactory {calbase createcal (); }PublicClassaddfactory:ifactory {PublicCalbase createcal () {ReturnNewCaladd (); } }PublicClasssubfactory:ifactory {Publiccalbase createcal () { return new calsub ();}} public class mulfactory:ifactory { public calbase createcal () { return new Calmul (); } public class divfactory:ifactory { public calbase createcal () { return new Ca Ldiv (); } }

 

Design Mode Factory method

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.