我真的很努力想出一个我正在构建的警报器的设计模式。下面是我试图做的一个人为的例子:警报器的设计模式
一个人想要通过天气类型(雨,雪,太阳等)获得警报。一个人也可以选择警报方式(电子邮件,短信,闲暇频道,时尚聊天室等)
我需要:有一个课程需要天气类型。然后它检索所有关心该天气类型的人。然后它循环遍历所有人并向他们发送警报(基于人的警报类型首选项)。
这里是我的基本轮廓,但看起来它应该做的 “更好”:
public class Alerter
{
private readonly WeatherType _weatherType;
public Alerter(WeatherType weatherType)
{
_weatherType = weatherType;
}
public void SendAlerts()
{
var people = PersonRepository.GetPeople(_weatherType);
foreach (Person person in people)
{
switch (person.AlertType)
{
case Email:
var e = new EmailAlerter();
e.SendToPerson(person, _weatherType);
return;
case SMS:
var s = new SmsAlerter();
s.SendToPerson(person, _weatherType);
return;
}
}
}
}
如果你需要讨论一个算法或设计模式,你可能应该把问题发布给程序员.stackexchange.com – Steve
@Steve在引用其他网站时,指出[交叉发布是皱眉了](http://meta.stackexchange.com/tags/cross-posting/info) – gnat
@gnat绝对正确,我的错在这里,但我是如此从程序员看到的一个meta post被拿走,我已经完全忘记 – Steve