samedi 24 septembre 2016

Which design pattern for an alerter

I'm really struggling to come up with a design pattern for an Alerter I'm building. Here's a contrived example of what I'm trying to do:

A person wants to get an alert by weather type (rain, snow, sun, etc.). A person also has a choice of alert method (email, sms, slack channel, hipchat room, etc.)

I need to: have a class which takes in a weather type. Then it retrieves all the people that care about that weather type. Then it loops through all the people and sends them their alert (based on the person's alert type preference).

Here's my basic outline, but it seems like it should be done "better":

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;
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire