Skip to content

ITEM 34. int 상수 대신 열거 타입을 사용하라 #61

Description

@sypark9646

아래 요구사항을 Enum 을 이용하여 구현해 봅시다.

  • 두 string s1, s2를 비교하는 StringComparator 을 만든다.
  • NATURAL, REVERSE, LENGTH 각각은 int compare(String s1, String s2) 을 구현한다.
    • NATURAL: s1, s2를 순서로 비교
    • REVERSE: s2, s1를 순서로 비교
    • LENGTH: s1, s2의 길이를 비교

p.s. Enum의 특성을 이용해서 Singleton을 사용해야 하는 팩토리 패턴 문제를 해결할 수 있는 방법도 있네요! 한 번 읽어보시면 좋을 것 같습니다 :)
https://effectiveprogramming.tistory.com/entry/Enum-Factory-Method-패턴

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions