usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceconsoleapplication9{/*design mode: Factory mode*/ //Parent class: NoteBook Public Abstract classNoteBook { Public Abstract voidSayHello (); } //sub-class: Public classAsus:notebook { Public Override voidSayHello () {Console.WriteLine ("I'm an ASUS notebook ."); } } Public classAcer:notebook { Public Override voidSayHello () {Console.WriteLine ("I'm a Acer laptop ."); } } Public classIbm:notebook { Public Override voidSayHello () {Console.WriteLine ("I am an IBM notebook"); } } Public classDell:notebook { Public Override voidSayHello () {Console.WriteLine ("I'm a Dell laptop ."); } } classProgram {Static voidMain (string[] args) {Console.WriteLine ("Please enter the notebook brand you want:"); stringBrand =Console.ReadLine (); NoteBook NB=Getnotebook (brand); nb. SayHello (); Console.ReadLine (); } /// <summary> ///the core of a simple factory (create objects assigned to the parent class based on user input)/// </summary> /// <param name= "brand" >Brand</param> /// <returns></returns> Public StaticNoteBook Getnotebook (stringbrand) {NoteBook NB=NULL; Switch(brand) { Case "ASUS": NB =NewASUS (); Break; Case "IBM": NB =NewIBM (); Break; Case "DELL": NB =NewDELL (); Break; Case "Acer": NB =NewAcer (); Break; } returnNB; } }}
Chapter 10. Design mode-Factory mode