
//Subject
abstract class Graphic(protected var fileName: String) {
abstract fun draw()
fun getGFileName(): String {
return fileName
}
}
//RealSubject
class Image(fileName: String): Graphic(fileName) {
//Request()
override fun draw() {
println("draw $fileName")
}
}
//Proxy
class ImageProxy(fileName: String): Graphic(fileName) {
private var image: Image? = null
override fun draw() {
getImage().draw()
}
private fun getImage(): Image {
if (image == null) {
image = Image(fileName)
}
return image!!
}
}
//Client
val proxy = ImageProxy("1.png")
//operation without creating a RealSubject
val fileName = proxy.getGFileName()
//forwarded to the RealSubject
proxy.draw()
println("fileName is $fileName")
| Provide a surrogate or placeholder for another object to control access to it. The proxy implements the interface of the original object. |