Creation Mode-simple factory Mode

Source: Internet
Author: User
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace simple factory mode {public class simplefactory {// if this function is changed to static, it is changed to static factory method public iproduct createproduct (string productname) {Switch (productname) {Case "producta": return New producta (); Case "prodcutb": return New productb (); default: Throw new exception ("product creation error! ") ;}} Public interface iproduct {void mustdosomething ();} public class producta: iproduct {public producta () {} public void mustdosomething () {// Service Logic of product A} public class productb: iproduct {public productb () {} public void mustdosomething () {// business logic of product B} public class appclient {public static void main (string [] ARGs) {simplefactory afactory = new simplefactory (); iproduct producta = afactory. createproduct ("producta"); producta. mustdosomething (); // call the method iproduct productb = afactory of product. createproduct ("prodcutb"); productb. mustdosomething (); // call the method of product B }}}

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.