Groovy web console

subscribe to the feed Subscribe
to this
site

Factory method template

Published 2 months ago by map with tags factory
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
interface ReportSender {
    void send(Map templateData, def receiver)
}  

class EmailSender implements ReportSender, Serializable {
    void send(Map template, def receiver) { 
        println "INFO: sent by EmailSender.send()"
    }
}

class TeamsSender implements ReportSender, Serializable {
    String channelId
    TeamsSender(String channelId) { 
        this.channelId = channelId
    }
    void send(Map template, def receiver) { 
        println "INFO: sent by TeamsSender.send()"
    }
}

class ReportSenderFactory {
    static ReportSender createReportSender(String senderType) {
       if (senderType == 'teams') {
          return new TeamsSender("channelId: '... channel GUID ...'")
       } else if (senderType == 'email') {
          return new EmailSender()
       } else {
          throw new Exception("Unknown sender type " + senderType)
       }
    }
}



// client code 1

    ReportSender sender = ReportSenderFactory.createReportSender('teams')
    sender.send([source: 'xxx', id: 4234243], '...')

// client code 2

    // ReportSender sender = ReportSenderFactory.createReportSender('email')
    // sender.send([source: 'xxx', id: 4234243], '...')