interface Shape {
fun draw()
}
class Rectangle : Shape {
override fun draw() {
println("Drawing a Rectangle")
}
}
class Circle : Shape {
override fun draw() {
println("Drawing a Circle")
}
}
interface ShapeFactory {
fun createShape(): Shape
}
class RectangleFactory : ShapeFactory {
override fun createShape(): Shape {
return Rectangle()
}
}
// 원을 생성하는 팩토리
class CircleFactory : ShapeFactory {
override fun createShape(): Shape {
return Circle()
}
}
fun main() {
val rectangleFactory: ShapeFactory = RectangleFactory()
val rectangle: Shape = rectangleFactory.createShape()
rectangle.draw() // Output: Drawing a Rectangle
val circleFactory: ShapeFactory = CircleFactory()
val circle: Shape = circleFactory.createShape()
circle.draw() // Output: Drawing a Circle
}
그리고 여기에 너무 잘 정리되어 있어서..ㅎㅎ 이걸 참고하면 될 것 같다!