Patterns / Structural patterns
image

//Subject
class Graphic {
    fileprivate var fileName = ""

    init(_ fileName: String) {
        self.fileName = fileName
    }

    func draw() {
        print("base draw")
    }

    func getFileName() -> String {
        return fileName
    }
}

//RealSubject
class ImageGraphic {

    //Request()
    override func draw() {
        super.draw()
        print("draw " + fileName)
    }
}

//Proxy
class ImageProxyGraphic {
    private var image: Image?

    override func draw() {
        getImage().draw()
    }

    func getImage() -> Image {
        if (image == nil) {
            image = Image(fileName)
        }
        return image!
    }
}

//Client
let proxy = ImageProxy("1.png")
//operation without creating a RealSubject
let fileName = proxy.getFileName()
//forwarded to the RealSubject
proxy.draw()

print("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.