Simple Factory mode-2015-1-7

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.text;namespace ConsoleApplication1{ Class Program {static void Main (string[] args) {Acompany item = Companyfactory.createcompany            (Enumcompany.car);            if (item = = NULL) Console.WriteLine ("null"); else {item.                Open (); Item.            Close ();            } Console.WriteLine ("****************************************");            Item = Companyfactory.createcompany (Enumcompany.car);            if (item = = NULL) Console.WriteLine ("null"); else {item.                Open (); Item.            Close ();        } console.readkey ();    }} enum Enumcompany {car=0, boat=1, shoat=2, food=3,} class Companyfactory {public static Acompany Createcompany (Enumcompany item) {Acompany Company = NULL;                Switch (item) {Case enumcompany.car:company= new Car (); Case enumcompany.boat:company= New Boat ();            Break        } return company;        }} interface Icompany {bool Open ();    BOOL Close ();        } abstract class Acompany:icompany {public int Count = 0;        public static int Flag = 0;        public abstract bool Open ();        public abstract bool Close ();    public void Test () {} public abstract void Method (); } class Car:acompany {public override void Method () {Console.WriteLine (this.                    ToString ()); public override bool Open () {Console.WriteLine (this).                        ToString ());            count++;            flag++;            Console.WriteLine ("Count:{0}", Count);            Console.WriteLine ("Flag:{0}", Flag);        return true;        }public override bool Close () {Console.WriteLine (this.            ToString ());            count++;            flag++;            Console.WriteLine ("Count:{0}", Count);            Console.WriteLine ("Flag:{0}", Flag);        return true; }} class Boat:acompany {public override bool Open () {Console.WriteLine (this.            ToString ());        return true; public override bool Close () {Console.WriteLine (this).            ToString ());        return true; public override void Method () {Console.WriteLine (this).        ToString ()); }    }    }

  

/             * * Output results            consoleapplication1.car            count:1            flag:1            consoleapplication1.car            count:2            Flag:2            ****************************************            consoleapplication1.car            count:1            Flag:3            Consoleapplication1.car            count:2            flag:4             * * *              

  

<div> This article simply lists the classes, interfaces, and abstract classes that are used in project development, which together form a simple factory model </div>

Simple Factory mode-2015-1-7

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.