Factory creates an object from several possible classes.
For example, if you create a communication class interface and have multiple implementations, you can use factory to create an object that implements the interface, and factory can create the appropriate object based on our selection.
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Timers;Namespacedemo{PublicInterfaceicommunication {BOOL Send (Objectdata); }PublicClassserial:icommunication {PublicBOOL Send (ObjectData) {Console.WriteLine ("Send a data via serial port");ReturnTrue; } }PublicClasslan:icommunication {PublicBOOL Send (ObjectData) {Console.WriteLine ("Send a data through the network port");ReturnTrue; } }PublicClasscommunicationfactory {Public Icommunication Createcommunicationfactory (StringStyle) {Switch(style) {Case"Serial":ReturnNewSerial ();Case"Lan":ReturnNewLan (); }ReturnNullclass program {static void Main (string[] args) {Communicationfactory factory = new Communicationfactory (); Console.WriteLine ( " "); string input = Console.ReadLine (); object data = new object
C # design mode-Factory mode (Factory)