diff --git a/src/main/kotlin/edu/nextstep/wordle/application/wordle/Word.kt b/src/main/kotlin/edu/nextstep/wordle/application/wordle/Word.kt index ef843c2..2c00451 100644 --- a/src/main/kotlin/edu/nextstep/wordle/application/wordle/Word.kt +++ b/src/main/kotlin/edu/nextstep/wordle/application/wordle/Word.kt @@ -1,6 +1,7 @@ package edu.nextstep.wordle.application.wordle import edu.nextstep.wordle.application.wordle.window.Alphabet +import edu.nextstep.wordle.application.wordle.window.AlphabetFactory import edu.nextstep.wordle.application.wordle.window.Match import edu.nextstep.wordle.application.wordle.window.Window import edu.nextstep.wordle.application.wordle.window.WindowResult @@ -40,8 +41,9 @@ data class Word( private const val WORD_SIZE: Int = 5 fun create(input: String): Word { + val alphabetFactory = AlphabetFactory.instance val windows = input.mapIndexed { index, alphabet -> - Window(alphabet = Alphabet(value = alphabet.toString()), position = index) + Window(alphabet = alphabetFactory.findBy(alphabet.toString()), position = index) }.toSet() return Word(windows) } diff --git a/src/main/kotlin/edu/nextstep/wordle/application/wordle/Wordle.kt b/src/main/kotlin/edu/nextstep/wordle/application/wordle/Wordle.kt index fc02555..6a51f67 100644 --- a/src/main/kotlin/edu/nextstep/wordle/application/wordle/Wordle.kt +++ b/src/main/kotlin/edu/nextstep/wordle/application/wordle/Wordle.kt @@ -23,7 +23,7 @@ data class Wordle( private fun WordFinder.notContain(word: Word): Boolean = !this.contain(word) private fun Word.rawWord(): String { - return windows.sortedBy { it.position }.joinToString(separator = "") { it.alphabet._value } + return windows.sortedBy { it.position }.joinToString(separator = "") { it.alphabet.alphabet } } fun isSuccess(): Boolean { diff --git a/src/main/kotlin/edu/nextstep/wordle/application/wordle/window/Alphabet.kt b/src/main/kotlin/edu/nextstep/wordle/application/wordle/window/Alphabet.kt index 2cbbfba..abefd04 100644 --- a/src/main/kotlin/edu/nextstep/wordle/application/wordle/window/Alphabet.kt +++ b/src/main/kotlin/edu/nextstep/wordle/application/wordle/window/Alphabet.kt @@ -3,30 +3,30 @@ package edu.nextstep.wordle.application.wordle.window class Alphabet( value: String, ) { - val _value: String = value.lowercase() + val alphabet: String = value.lowercase() init { - if (!alphabets.contains(_value)) { - throw IllegalArgumentException("$_value 알파벳 입력만 허용합니다.") + if (!alphabets.contains(alphabet)) { + throw IllegalArgumentException("$alphabet 알파벳 입력만 허용합니다.") } } - companion object { - private val alphabets = ('A'..'Z').map { it.lowercase() }.toSet() - } - override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false other as Alphabet - if (_value != other._value) return false + if (alphabet != other.alphabet) return false return true } override fun hashCode(): Int { - return _value.hashCode() + return alphabet.hashCode() + } + + companion object { + private val alphabets = ('A'..'Z').map { it.lowercase() }.toSet() } } diff --git a/src/main/kotlin/edu/nextstep/wordle/application/wordle/window/AlphabetFactory.kt b/src/main/kotlin/edu/nextstep/wordle/application/wordle/window/AlphabetFactory.kt index 0529925..758baac 100644 --- a/src/main/kotlin/edu/nextstep/wordle/application/wordle/window/AlphabetFactory.kt +++ b/src/main/kotlin/edu/nextstep/wordle/application/wordle/window/AlphabetFactory.kt @@ -9,7 +9,9 @@ class AlphabetFactory( } companion object { - fun create(): AlphabetFactory { + val instance = create() + + private fun create(): AlphabetFactory { val alphabets: Set = ('A'..'Z').map { Alphabet(it.lowercase()) } .toSet() diff --git a/src/test/kotlin/edu/nextstep/wordle/application/wordle/window/AlphabetFactoryTest.kt b/src/test/kotlin/edu/nextstep/wordle/application/wordle/window/AlphabetFactoryTest.kt index 8e4991f..677af97 100644 --- a/src/test/kotlin/edu/nextstep/wordle/application/wordle/window/AlphabetFactoryTest.kt +++ b/src/test/kotlin/edu/nextstep/wordle/application/wordle/window/AlphabetFactoryTest.kt @@ -8,7 +8,7 @@ internal class AlphabetFactoryTest { @Test fun `팩토리를 통해 알파벳을 찾을 수 있다`() { //given - val alphabetFactory = AlphabetFactory.create() + val alphabetFactory = AlphabetFactory.instance //when val alphabet = alphabetFactory.findBy("a")