졸업프로젝트를 하면서, 클래스 내에 필드에
(봄, 여름, 가을, 겨울)
(시트러스향, 우디향, 플로럴향)
같은 열거가 필요한 상수를 많이 사용하였다. 점점 코드의 양이 많아지니까, 정의할 것이 계속 생겨나고, 가독성을 해치는 것 같다고 느껴져서 확실하게 알고 있지 못했던 Enum 클래스에 대한 공부를 했다.
밑 코드는 향(시트러스,우디 등)에 따른 메세지를 return해주는 메소드이다.
public String selectScent(Long id) {
Survey survey = findFeature(id);
String message = "";
if (survey.getScentAnswer().equals(CITRUS)) {
message = ScentMessage.CITRUS_MESSAGE;
}
if (survey.getScentAnswer().equals(SOAPY)) {
message = ScentMessage.SOAPY_MESSAGE;
}
if (survey.getScentAnswer().equals(WOODY)) {
message = ScentMessage.WOODY_MESSAGE;
}
if (survey.getScentAnswer().equals(FLORAL)) {
message = ScentMessage.FLORAL_MESSAGE;
}
if (survey.getScentAnswer().equals(FRUITY)) {
message = ScentMessage.FRUITY_MESSAGE;
}
if (survey.getScentAnswer().equals(VANILLA)) {
message = ScentMessage.VANILLA_MESSAGE;
}
return message;
해당 코드의 문제점은
public enum ScentType {
CITRUS("시트러스", "당신은 오렌지나 자몽처럼 상큼한 향을 좋아하시는군요! 신선한 향은 당신을 더욱 매력적으로 보이게 할 거예요 :)"),
WOODY("우디", "당신은 자연에서 나는 듯한 냄새와 잘 어울릴 것 같아요! 우디향 향수는 당신을 더욱 매력적인 사람으로 만들거예요 :)"),
SOAPY("소피", "당신은 은은하게 나는 비누향과 잘 어울릴 것 같아요! 깔끔하고 단정한 이미지를 만들어보아요 :)"),
FRUITY("프루티", "당신은 달콤한 과일같은 향기와 잘 어울릴 것 같아요! 이런 향수를 쓰면 지나간 사람들이 한번씩 뒤돌아볼 것 같아요 :)"),
FLORAL("플로럴", "당신은 우아하고 고급스러운 꽃 향과 잘 어울릴 것 같아요! 꽃 향기로 당신의 매력을 어필해보세요 :)"),
VANILLA("바닐라", "당신은 달짝지근한 향수와 잘 어울릴 것 같아요! 은은하게 퍼지는 향은 당신을 더욱 매력적인 사람으로 만들거예요 :)");
private String scent;
private String feature;
ScentType(String scent, String feature) {
this.scent = scent;
this.feature = feature;
}
public static String getFeature(Survey survey) {
String expectedScent = survey.getScentAnswer();
ScentType scentType = Arrays.stream(ScentType.values())
.filter(scent -> scent.getScent().equals(expectedScent))
.findAny().orElseThrow(SurveyNotFoundException::new);
return scentType.getFeature();
}
}
public String selectScent(Long id) {
Survey survey = findFeature(id);
return ScentType.getFeature(survey);
}
public class ScentMessage {
private static final String CITRUS = "시트러스";
private static final String WOODY = "우디";
private static final String SOAPY = "소피";
private static final String FLORAL = "플로럴";
private static final String FRUITY = "프루티";
private static final String VANILLA = "바닐라";
public static final String CITRUS_MESSAGE =
"당신은 오렌지나 자몽처럼 상큼한 향을 좋아하시는군요! 신선한 향은 당신을 더욱 매력적으로 보이게 할 거예요 :)";
public static final String FLORAL_MESSAGE =
"당신은 우아하고 고급스러운 꽃 향과 잘 어울릴 것 같아요! 꽃 향기로 당신의 매력을 어필해보세요 :)";
public static final String WOODY_MESSAGE =
"당신은 자연에서 나는 듯한 냄새와 잘 어울릴 것 같아요! 우디향 향수는 당신을 더욱 매력적인 사람으로 만들거예요 :)";
public static final String SOAPY_MESSAGE =
"당신은 은은하게 나는 비누향과 잘 어울릴 것 같아요! 깔끔하고 단정한 이미지를 만들어보아요 :)";
public static final String FRUITY_MESSAGE =
"당신은 달콤한 과일같은 향기와 잘 어울릴 것 같아요! 이런 향수를 쓰면 지나간 사람들이 한번씩 뒤돌아볼 것 같아요 :)";
public static final String VANILLA_MESSAGE =
"당신은 달짝지근한 향수와 잘 어울릴 것 같아요! 은은하게 퍼지는 향은 당신을 더욱 매력적인 사람으로 만들거예요 :)";
}
public class FeatureService {
public String selectScent(Long id) {
Survey survey = findFeature(id);
String message = "";
if (survey.getScentAnswer().equals(CITRUS)) {
message = ScentMessage.CITRUS_MESSAGE;
}
if (survey.getScentAnswer().equals(SOAPY)) {
message = ScentMessage.SOAPY_MESSAGE;
}
if (survey.getScentAnswer().equals(WOODY)) {
message = ScentMessage.WOODY_MESSAGE;
}
if (survey.getScentAnswer().equals(FLORAL)) {
message = ScentMessage.FLORAL_MESSAGE;
}
if (survey.getScentAnswer().equals(FRUITY)) {
message = ScentMessage.FRUITY_MESSAGE;
}
if (survey.getScentAnswer().equals(VANILLA)) {
message = ScentMessage.VANILLA_MESSAGE;
}
return message;
}
}
public enum ScentType {
CITRUS("시트러스", "당신은 오렌지나 자몽처럼 상큼한 향을 좋아하시는군요! 신선한 향은 당신을 더욱 매력적으로 보이게 할 거예요 :)"),
WOODY("우디", "당신은 자연에서 나는 듯한 냄새와 잘 어울릴 것 같아요! 우디향 향수는 당신을 더욱 매력적인 사람으로 만들거예요 :)"),
SOAPY("소피", "당신은 은은하게 나는 비누향과 잘 어울릴 것 같아요! 깔끔하고 단정한 이미지를 만들어보아요 :)"),
FRUITY("프루티", "당신은 달콤한 과일같은 향기와 잘 어울릴 것 같아요! 이런 향수를 쓰면 지나간 사람들이 한번씩 뒤돌아볼 것 같아요 :)"),
FLORAL("플로럴", "당신은 우아하고 고급스러운 꽃 향과 잘 어울릴 것 같아요! 꽃 향기로 당신의 매력을 어필해보세요 :)"),
VANILLA("바닐라", "당신은 달짝지근한 향수와 잘 어울릴 것 같아요! 은은하게 퍼지는 향은 당신을 더욱 매력적인 사람으로 만들거예요 :)");
private String scent;
private String feature;
ScentType(String scent, String feature) {
this.scent = scent;
this.feature = feature;
}
public static String getFeature(Survey survey) {
String expectedScent = survey.getScentAnswer();
ScentType scentType = Arrays.stream(ScentType.values())
.filter(scent -> scent.getScent().equals(expectedScent))
.findAny().orElseThrow(SurveyNotFoundException::new);
return scentType.getFeature();
}
}
public class FeatureService {
public String selectScent(Long id) {
Survey survey = findFeature(id);
return ScentType.getFeature(survey);
}
}
도움 많이많이 받은 참고자료
우아한 형제들 기술블로그