class EtherNet { public void Send(string data) { Console.WriteLine("EtherNet is Sending: {0}", data); } } class COM { public void Transform(string data) { Console.WriteLine("COM is Transforming :{0}", data); } } class USB { public void Transform(string data) { Console.WriteLine("USB is Transforming :{0}", data); } } //门面,把各种设备封装,对外提供统一的接口 class IO { private EtherNet net; private COM com; private USB usb; public void Send(string data, string port) { switch (port) { case "net": net = new EtherNet(); net.Send(data); break; case "com": com = new COM(); com.Transform(data); break; case "usb": usb = new USB(); usb.Transform(data); break; default: Console.WriteLine(" 对不起,没有此种设备!"); break; } } } class Computer { static private IO io; [STAThread] static void Main(string[] args) { Console.WriteLine(" 请指定设备端口:"); string port = Console.ReadLine().ToLower(); Console.WriteLine(" 请输入要发送的数据:"); string data = Console.ReadLine().ToLower(); io = new IO(); io.Send(data, port); } } |