Skip to content

Commit

Permalink
refactor : 변수이름 변경 및 팩토리 사용
Browse files Browse the repository at this point in the history
 - 알파벳 변수 이름 변경 _value -> alphabet
 - 알파벳 팩토리를 사용하도록 변경
  • Loading branch information
pci2676 committed Apr 15, 2022
1 parent ea3292f commit a0afc24
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class AlphabetFactory(
}

companion object {
fun create(): AlphabetFactory {
val instance = create()

private fun create(): AlphabetFactory {
val alphabets: Set<Alphabet> = ('A'..'Z').map { Alphabet(it.lowercase()) }
.toSet()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class AlphabetFactoryTest {
@Test
fun `팩토리를 통해 알파벳을 찾을 수 있다`() {
//given
val alphabetFactory = AlphabetFactory.create()
val alphabetFactory = AlphabetFactory.instance

//when
val alphabet = alphabetFactory.findBy("a")
Expand Down

0 comments on commit a0afc24

Please sign in to comment.