- Hands-On Design Patterns with Java
- Dr. Edward Lavieri
- 357字
- 2021-06-24 14:58:03
Main email handler
MainEmailHandler() is the primary handler and implements the UniversityEmailHandler interface. The first part of this class contains the processEmailHandler() method, which performs the search of the email text for the keywords assigned to each email handler. The following code shows this:
public abstract class MainEmailHandler implements UniversityEmailHandler {
private UniversityEmailHandler theNextHandlerInTheChain;
public void setNextEmailHandler(UniversityEmailHandler emailHandler) {
theNextHandlerInTheChain = emailHandler;
}
public void processEmailHandler(String emailText) {
// starting value
boolean keyWordFound = false;
// check for a matching keyword in emailText
if (keyWords().length == 0) {
keyWordFound = true;
} else {
for (String oneKeyWord : keyWords()) {
if (emailText.indexOf(oneKeyWord) >= 0) {
keyWordFound = true; // change value if match is found
break; // leave loop if match is found
}
}
}
Next, we check to determine whether the email can be processed by the current email handler based on the keyword match:
if (keyWordFound) {
processEmailHandler(emailText);
} else {
// pass along the chain if the email is not processed
// by the current email handler
theNextHandlerInTheChain.processEmailHandler(emailText);
}
}
The third part of the MainEmailHandler() class contains the handleEmail() method, which takes the email text as a parameter. It then creates instances of each handler:
public static void handleEmail(String emailText) {
UniversityEmailHandler academic = new AcademicEmailHandler();
UniversityEmailHandler alumni = new AlumniEmailHandler();
UniversityEmailHandler advising = new AdvisingEmailHandler();
UniversityEmailHandler finance = new FinanceEmailHandler();
UniversityEmailHandler hr = new HREmailHandler();
UniversityEmailHandler admin = new AdminEmailHandler();
The final part of the MainEmailHandler() class sets up the direction for the chain by making calls to the setNextEmailHandler() method for each of the email handlers except for the last one, AdminEmailHandler(), since that is our catchall email handler:
// setup chain direction
academic.setNextEmailHandler(alumni);
alumni.setNextEmailHandler(advising);
advising.setNextEmailHandler(finance);
finance.setNextEmailHandler(hr);
hr.setNextEmailHandler(admin);
// we do not need to set the next email handler after admin
// because it is the end of the chain of responsibility
// this line will start the chain
academic.processEmailHandler(emailText);
}
protected abstract String[] keyWords();
protected abstract void processEmailFinal(String emailText);
}
The MainEmailHandler() code provided is the primary handler and contains the processEmailHandler() method, which performs the search of the email text for the keywords assigned to each email handler.
- Access 2007數(shù)據(jù)庫(kù)應(yīng)用上機(jī)指導(dǎo)與練習(xí)
- Spark大數(shù)據(jù)編程實(shí)用教程
- SQL優(yōu)化最佳實(shí)踐:構(gòu)建高效率Oracle數(shù)據(jù)庫(kù)的方法與技巧
- 大數(shù)據(jù)架構(gòu)商業(yè)之路:從業(yè)務(wù)需求到技術(shù)方案
- 深入淺出 Hyperscan:高性能正則表達(dá)式算法原理與設(shè)計(jì)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)教程上機(jī)指導(dǎo)與習(xí)題集(微課版)
- 達(dá)夢(mèng)數(shù)據(jù)庫(kù)運(yùn)維實(shí)戰(zhàn)
- 淘寶、天貓電商數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第2版)
- TextMate How-to
- 探索新型智庫(kù)發(fā)展之路:藍(lán)迪國(guó)際智庫(kù)報(bào)告·2015(上冊(cè))
- Power BI智能數(shù)據(jù)分析與可視化從入門(mén)到精通
- Python數(shù)據(jù)分析從小白到專家
- 數(shù)據(jù)指標(biāo)體系:構(gòu)建方法與應(yīng)用實(shí)踐
- Deep Learning with R for Beginners
- Kubernetes快速進(jìn)階與實(shí)戰(zhàn)