using
System;
interface
Device
{
void
Send(string
data) ;
}
class
EtherNet
: Device
{
public
void
Send(string
data)
{
Console.WriteLine("EtherNet
is Sending: {0}",data);
}
}
class
COM
: Device
{
public
void
Send(string
data)
{
Console.WriteLine("COM
is Transforming :{0}",
data);
}
}
class
USB
: Device
{
public
void
Send(string
data)
{
Console.WriteLine("USB
is Transforming :{0}",
data);
}
}
class
Wireless
: Device
{
public
void
Send(string
data)
{
Console.WriteLine("Wireless
is Transforming :{0}",
data);
}
}
class
FactoryDevice
{
Device
device
= null;
public
Device
CreateDevice(string
deviceType)
{
switch
(deviceType)
{
case
"net":
device = new
EtherNet();
break;
case
"com":
device = new
COM();
break;
case
"usb":
device = new
USB();
break;
case
"wireless":
device = new
Wireless();
break;
default:
Console.WriteLine("
对不起,没有此种设备!!");
break;
}
return
device;
}
}
class
Computer
{
[STAThread]
static
void
Main(string[]
args)
{
Console.WriteLine("
请输入设备类型:");
string
deviceType
= Console.ReadLine().ToLower();
Console.WriteLine("
请输入要发送的数据:");
string
data = Console.ReadLine().ToLower();
FactoryDevice
fac
= new
FactoryDevice();
Device
device
= fac.CreateDevice(deviceType);
device.Send(data);
Console.ReadLine();
}
} |