<aside> 💡
</aside>
추상 팩토리를 사용하지 않은 경우
class Teacher() {
val student = Student()
}
class Student {
val age = 5
fun getAge(): Int {
return age
}
}
application -> teacher -> student ( 구체 )
application -> teacher -> student ( 추상 )
studentImpl -> student ( 추상 )
studentFactoryImpl -> studentFactory -> student ( 추상 )
추상 팩토리를 사용 한 경우
class Teacher(
private val studentFactory: StudentFactory
) {
val student = studentFactory.makeStudent()
}
interface StudentFactory {
fun makeStudent(): Student
}
class StudentFactoryImpl : StudentFactory {
override fun makeStudent(): Student {
return StudentImpl()
}
}
class StudentImpl : Student {
val age = 5
override fun getAge(): Int {
return age
}
}
interface Student {
fun getAge(): Int
}
fun main() {
val teacher = Teacher(StudentFactoryImpl())
}
StudentFactoryImpl 클래스 이다.StudentFactoryImpl 한군데 뿐이다.
StudentFactoryImpl을 main에서 생성 한 이후 계속해서 재사용 할 수 있도록 전역 변수로 저장 할 수 도 있다.