接口
public interface SendServiceInterface {
boolean send (String content);
default void sendLog (String content, Boolean isSuccess) {
System.out.println(String.format("content :: %s,isSuccess :: %s",content,isSuccess));
}
}
实现类
@Component
public class EmailU implements InitializingBean, SendServiceInterface{
@Override
public boolean send(String content) {
System.out.println("send Email ...");
sendLog(content,false);
return false;
}
@Override
public void afterPropertiesSet() throws Exception {
SendTypeContext.regist("email",this);
}
}
@Component
public class SmsU implements InitializingBean, SendServiceInterface{
@Override
public boolean send(String content) {
System.out.println("send Sms ...");
sendLog(content,true);
return true;
}
@Override
public void afterPropertiesSet() throws Exception {
SendTypeContext.regist("sms",this);
}
}
工厂
public class SendTypeContext {
public static Map<String,SendServiceInterface> handlerMap = new ConcurrentHashMap<>();
public static void regist (String sendType, SendServiceInterface sendServiceInterface) {
if (StringUtils.isBlank(sendType) || Objects.isNull(sendServiceInterface)){
return;
}
handlerMap.put(sendType,sendServiceInterface);
}
public static SendServiceInterface getHandler (String sendType){
return handlerMap.get(sendType);
}
}