根据“界面隔离原则”我有
{"name": "8808", "ip": "192.168.241.110", "cameras": {"front": ["nf091"], "inside": ["nf067"], "right": ["004317"], "rear": ["000189"], "left": ["nf084"]}, "serial": "000002", "simId": 197078302}
{"name": "8893", "ip": "192.168.241.137", "cameras": {"front": ["nf052"], "inside": ["000211"], "right": ["000069"], "rear": ["000441"], "left": ["000400"]}, "serial": "000277", "simId": 197057802}
{"name": "1620", "ip": "192.168.242.145", "cameras": {"front": ["000174"], "inside": ["000197"], "right": ["000304"], "rear": ["000295"], "left": ["000553"]}, "serial": "000084", "simId": 310922501}
{"name": "0632", "ip": "192.168.242.166", "cameras": {"front": ["nf050"], "inside": ["nf022"], "right": ["nf047"], "rear": ["ne056"], "left": ["ne083"]}, "serial": "NF016", "simId": 310897301}
{"name": "1544", "ip": "192.168.242.234", "cameras": {"front": ["000061"], "inside": ["000068"], "right": ["004440"], "rear": ["000219"], "left": ["005516"]}, "serial": "000200", "simId": 310839901}
{"name": "12HA", "ip": "192.168.243.116", "cameras": {"front": ["000625"], "right": ["nf104"], "rear": ["ne047"], "left": ["000717"]}, "serial": "000181", "simId": 510339201}
现在我有一个简单的例子:
interface IMessage
{
void Send();
string ToAddress { get; set; }
string FromAddress { get; set; }
}
interface ITextMessage : IMessage
{
string Text { get; set; }
}
interface IEmailMessage : ITextMessage
{
string Subject { get; set; }
}
class EmailMessage : IEmailMessage
{
public string Text { get; set; }
public string Subject { get; set; }
public string FromAddress { get; set; }
public string ToAddress { get; set; }
public EmailMessage(string text, string subject /*more parameters*/)
{
Text = text;
Subject = subject;
//other parameters
}
public void Send()
{
Console.WriteLine("Send Email : {0}", Text);
}
}
class SmsMessage : ITextMessage
{
public string Text { get; set; }
public string FromAddress { get; set; }
public string ToAddress { get; set; }
public SmsMessage(string text /*more parameters*/)
{
Text = text;
//other parameters
}
public void Send()
{
Console.WriteLine("Send Sms : {0}", Text);
}
}
如何在没有将项目转换为EmailMessage的Messages [1]项目中获取“主题”属性(例如)?我应该使用哪种模式?
答案 0 :(得分:0)
这是一个多态问题。
将Subject
放在IMessage
上,如果没有,则返回null
(您也可以提供HasSubject
属性),否则需要施放。< / p>
没有对错。如果您不希望IMessage知道Subjects,那么您不希望在那里拥有这样的属性。如果有很多不同的子接口和子类型,那么转换可能会更好。