Created
September 6, 2025 21:53
-
-
Save iprashantpanwar/76c32c34a82e7706db92e26e3559e2e5 to your computer and use it in GitHub Desktop.
How to depend on interfaces rather than concrete implementations and optionally modularize features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // BAD: Direct dependency on Glide | |
| class ImageLoader { | |
| private val glide = Glide.with(context) | |
| fun load(url: String, imageView: ImageView) { | |
| glide.load(url).into(imageView) | |
| } | |
| } | |
| // GOOD: Depend on an abstraction | |
| interface ImageLoader { | |
| fun load(url: String, imageView: ImageView) | |
| } | |
| class GlideImageLoader(private val context: Context) : ImageLoader { | |
| override fun load(url: String, imageView: ImageView) { | |
| Glide.with(context).load(url).into(imageView) | |
| } | |
| } | |
| // SDK users can now provide their own implementation | |
| class MySdk(private val imageLoader: ImageLoader) { | |
| fun display(url: String, imageView: ImageView) { | |
| imageLoader.load(url, imageView) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment