Proxy

The proxy pattern is useful when we want to restrict or improve a mechanism for accessing an expensive resource. Let's suppose that we want to access a media file. This is an expensive resource and it's better to cache it to avoid multiple loadings. The following diagram shows how this case can be implemented:

The preceding diagram contains the CachedMediaFile class that extends the MediaFile class which, in turn, implements the File interface. Using CachedMediaFile is preferable to MediaFile because it can save memory resources.

The following snippet shows how this pattern works:

interface File {
fun showContent()
}

class MediaFile: File {
override fun showContent() = println("showContent")
}

class CachedMediaFile: File {
private val file by lazy { MediaFile() }

override fun showContenĐ°t() = file.showContent()
}

We can use the CachedMediaFile class instead of MediaFile to save our resources.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.62