VSCode๋ ์ฌ์ฉ์์๊ฒ ๋ค์ํ ์ข ๋ฅ(.py, .java, .kt ๋ฑ)์ ๋ฌธ์๋ฅผ ์ ๊ณตํ๋ ์์ฉํ๋ก๊ทธ๋จ์ด๋ค. ํ ์คํธ ์๋ํฐ์ ๊ธฐ๋ฅ์ ํ๋ ์์ฉํ๋ก๊ทธ๋จ์ VSCode ์ธ์๋ NotePad++, SublimeText ๋ฑ ์ฌ๋ฌ๊ฐ๊ฐ ์๋ค.
- ํ ์คํธ ์๋ํฐ ๊ธฐ๋ฅ์ ํ๋ ์์ฉํ๋ก๊ทธ๋จ ํ๋ ์์ํฌ๋ฅผ TextEditor ์ถ์ ํด๋์ค๋ก ์ ์ํ์.
- ์์ฉํ๋ก๊ทธ๋จ์ด ์ง์ํ๋ ๋ค์ํ ๋ฌธ์ ํฌ๋งท์ ์ธํฐํ์ด์ค Document ํด๋์ค๋ก ์ ์ํ์.
abstract class TextEditor {
protected abstract fun createDocument(type: DocumentType): Document // ํฉํ ๋ฆฌ ๋ฉ์๋
fun create(type: DocumentType): Document {
return createDocument(type)
}
}
interface Document {
fun open()
fun close()
fun save()
fun revert()
}์ถ์ ํด๋์ค์ธ TextEditor๋ VSCode, NotePad++, SublimeText, ์ฌ์ง์ด MS Word ๋ฑ์ ๋ํ๊ฒฉ์ด๋ค. ์ด๋ค ์ข
๋ฅ์ ์์ฉ ํ๋ก๊ทธ๋จ์ ์ ํํ๋๋์ ๋ฐ๋ผ ๊ทธ์ ์ข
์๋๋ ๋ฌธ์์ ์๋ธํด๋์ค๊ฐ ๋ฌ๋ผ์ง๋ค. ์๋ฅผ ๋ค์ด MS Word๋ฅผ ์ ํํด์ ๊ตฌํํ๊ฒ ๋๋ฉด, ์ด์ ์ข
์๋๋ ๋ฌธ์๊ฐ .docx ํฌ๋งท์ด ๋ ์๋ ์์ง๋ง, .cpp ํฌ๋งท์ด ๋์ง๋ ๋ชปํ๋ค. ๋ฐ๋ผ์ TextEditor ํด๋์ค๋ ์ธ์ Document์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ด์ผ ํ๋์ง๋ง ์๊ณ ์์ ๋ฟ(create() ๋ฉ์๋), ์ด๋ค ์ข
๋ฅ์ ๋ฌธ์๋ฅผ ์์ฑํด์ผ ํ๋์ง ์ฌ์ ์ ์์ง ๋ชปํ๋ค.
ํ๋ ์์ํฌ๋ ์ถ์ ํด๋์ค ๋ฐ์ ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์, ์ถ์ ํด๋์ค์ ์ธ์คํด์คํ ์์ ์ ํ ์ ์๊ฒ ๋๋ค. ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ด ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
์ ์ฝ๋์์ createDocument()๋ก ์ ์๋ ๋ฉ์๋๋ ํฉํ ๋ฆฌ ๋ฉ์๋๋ผ ์นญํ๋ค. createDocument()์ ๋๊ธด ๋งค๊ฐ๋ณ์ type: DocumentType์ ๊ทผ๊ฑฐํ์ฌ ์ด๋ค ์ข
๋ฅ์ Document๋ฅผ ๋ง๋ค ๊ฒ์ธ๊ฐ๋ฅผ ์๊ณ ์๊ธฐ ๋๋ฌธ์ด๋ค. ํฉํ ๋ฆฌ ๋ฉ์๋ createDocument()๋ Document์ ์๋ธํด๋์ค ์ค ์ด๋ ๊ฒ์ ์์ฑํด์ผ ํ๋์ง์ ๋ํ ์ ๋ณด๋ฅผ ์๋ํ์ฌ ํ๋ ์์ํฌ ๋จ์์ ์ ์ ์๋๋ก ํ๋ค.
TextEditor ํด๋์ค์ ์๋ธํด๋์ค๊ฐ VSCode๋ผ๋ฉด VSCode๋ createDocument()๋ฅผ ์ฌ์ ์ํ์ฌ, VSCode๊ฐ ์ง์ํ๋ ํฌ๋งท์ Document ์๋ธํด๋์ค๋ฅผ ๋ฐํํ ๊ฒ์ด๋ค.
abstract class TextEditor {
protected abstract fun createDocument(type: DocumentType): Document
fun create(type: DocumentType): Document {
return createDocument(type)
}
}
interface Document {
fun open()
fun close()
fun save()
fun revert()
}ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ ๋ ๊ฐ์ง๊ฐ ์๋ค.
- ์์ ์์์ฒ๋ผ Creator(TextEditor)๋ฅผ ์ถ์ ํด๋์ค๋ก ์ ์ํ๊ณ , ์ ์ํ ํฉํ ๋ฆฌ ๋ฉ์๋์ ๋ํ ๊ตฌํ์ ์ ๊ณตํ์ง ์๋ ๊ฒฝ์ฐ. ์ ์ํ ํฉํ ๋ฆฌ ๋ฉ์๋ ์ญ์ ์ถ์ ๋ฉ์๋๋ก ์ง์ ํ๋ค.
- Creator(TextEditor)๋ฅผ ๊ตฌ์ฒด ํด๋์ค๋ก ์ ์ํ๊ณ , ํฉํ ๋ฆฌ ๋ฉ์๋์ ๋ํดํธ ๊ตฌํ์ ์ ๊ณตํ๋ ๊ฒฝ์ฐ
- ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ๋งค๊ฐ๋ณ์ํ ํ ์๋ ์๋ค.
// type: DocumentType ์ด๋ผ๋ Enum ๋ณ์๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋๊ธฐ๋ฉด ํฉํ ๋ฆฌ ๋ฉ์๋๊ฐ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ ์ด๋ค ์ข
๋ฅ์ ์ ํ์ ์์ฑํ ์ง ์๋ณํ ์ ์๋ค. ์ฌ๊ธฐ์๋ ์ ๊ณต ๋ฌธ์๋ฅผ Java, Kotlin, Cpp๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ ์ํ์๋๋ฐ, TextEditor์ ์๋ธํด๋์ค์์ ์ด๋ฅผ ์ค๋ฒ๋ผ์ด๋ํ์ฌ ๋ฌธ์ ์์ฑ์ ๋ํ ๋ก์ง์ ๋ ์ถ๊ฐํ ์๋ ์์ ๊ฒ์ด๋ค.
class TextEditor {
protected fun createDocument(type: DocumentType): Document {
when(type){
DocumentType.JAVA -> JavaDocument()
DocumentType.KOTLIN -> KotlinDocument()
DocumentType.CPP -> CppDocument()
}
}
}ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ์ฐ๋ฉด Document ํด๋์ค๋ฅผ ์ถ๊ฐํ๋ ค ํ ๋๋ง๋ค ์๋ธํด๋์ฑ์ ํด์ผํ๋ค. MSWordTextEditor๋ .docx ํฌ๋งท์ ๋ฌธ์๋ฅผ ๋ฐํ ๋ฐ๋๋ค๊ณ ๊ฐ์ ํ์. DocxDocument() ํด๋์ค๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด ์ด๋ฅผ ๋ฐํ๋ฐ์ผ๋ ค๋ TextEditor์ ์๋ธ ํด๋์ค์ธ MSWordTextEditor๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค. ์ด ๊ณผ์ ์ ๋ฐ๋ณต์ ์ด๊ธฐ ๋๋ฌธ์, ํ ํ๋ฆฟ์ ์ฌ์ฉํ์ฌ ํผํด์ผ ํ๋ค.
abstract class TextEditor {
protected abstract fun createDocument(): Document
fun create(): Document {
return createDocument()
}
}
// TextEditor์ ์์ํ๋ค.
class MSWordTextEditor: TextEditor {
override fun createDocument(): Document {
return DocxDocument()
}
}ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ ์ด์ฉํ ๊ฒฝ์ฐ
- ์์ฉ ํ๋ก๊ทธ๋จ์ ํ๋ ์์ํฌ ๋ ๋ฒจ์์ ๊ตฌ์ฒด ํด๋์ค์ ๋ํ ์ ๋ณด๋ฅผ ์๋ํ ์ ์๋ค.
- ๋์์ ํ๋ ์์ํฌ๊ฐ ์ถ์ ํด๋์ค๋ฅผ ์ธ์คํด์คํํ์ง ๋ชปํ๋ค๋ ๋จ์ ๋ ํด๊ฒฐํ๋ค.