From a0afc24aebae38ed7f378b8afb4a035b52ed634b Mon Sep 17 00:00:00 2001 From: chanin Date: Sat, 16 Apr 2022 00:02:50 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EB=B3=80=EC=88=98=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A6=AC=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 알파벳 변수 이름 변경 _value -> alphabet - 알파벳 팩토리를 사용하도록 변경 --- .../nextstep/wordle/application/wordle/Word.kt | 4 +++- .../wordle/application/wordle/Wordle.kt | 2 +- .../application/wordle/window/Alphabet.kt | 18 +++++++++--------- .../wordle/window/AlphabetFactory.kt | 4 +++- .../wordle/window/AlphabetFactoryTest.kt | 2 +- 5 files changed, 17 insertions(+), 13 deletions(-) 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")