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
| /** | |
| * Factory Pattern: A creation design pattern that delegates object creation to a factory. | |
| * | |
| * USE WHEN: Creating objects that require constructor parameters, making reflection impractical | |
| * or impossible (since reflection only works cleanly with no-arg constructors). | |
| * | |
| * In this example, the Factory interface encapsulates the creation logic, allowing: | |
| * - Parameterized constructors (v1, v2) | |
| * - Dependency injection at creation time | |
| * - Type-safe object creation with compile-time checks |
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
| /** | |
| * Reflection is a mechanism to inspect and manipulate classes, methods, and fields at runtime. | |
| * | |
| * In this example, reflection is used to create class instances dynamically without knowing | |
| * the concrete type at compile time. The Class<T> object provides metadata about the class, | |
| * allowing us to: | |
| * - Query available constructors via getDeclaredConstructor() | |
| * - Instantiate objects via newInstance() | |
| * - Access private members by modifying accessibility | |
| * |
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
| typealias CustomObserver<T> = (T?) -> Unit | |
| /** | |
| * A simple data holder class that allows observing changes to the data without being tied to an Android Lifecycle. | |
| * This is useful in scenarios where you need LiveData-like behavior in non-UI components or when you want to | |
| * manually manage the observer lifecycle. | |
| * | |
| * @param T The type of data held by this instance. | |
| */ | |
| class LiveDataWithoutLifecycle<T> { |
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
| /** | |
| This gist demonstrates how Kotlin coroutines internally use continuation-passing style (CPS) and | |
| a state machine with switch case, by manually simulating coroutine behavior. | |
| Coroutines appear linear, but under the hood they are compiled into: | |
| -> a Continuation object | |
| -> a label/state machine | |
| -> a big when {} block that decides where to resume multiple functions that | |
| get stitched together during suspension/resumption | |
| **/ |
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
| /** | |
| This example demonstrates how to perform background work in Android using Coroutines. | |
| **/ | |
| class AsyncWithCoroutines { | |
| private val scope = CoroutineScope(Dispatchers.Default) | |
| companion object { | |
| const val TAG = "AsyncWithCoroutines" | |
| } |
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
| /** | |
| This example demonstrates how to perform background work in Android using a simple Thread and | |
| return the result safely to the main thread using a Handler. This is helpful for understanding | |
| how Android threading works without coroutines or executors. | |
| **/ | |
| public class AsyncWithThreads { | |
| public final String TAG = "AsyncWithThreads"; | |
| void runAsyncTask() { // assuming this is called from onCreate() |
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
| //This code snippet assumes the JSON file content is an array of MODEL_CLASS objects. | |
| var content = emptyArray<MODEL_CLASS>() | |
| fun readJsonFile(context: Context) { | |
| //get an input stream from assets folder for the file | |
| val inputStream = context.assets.open("FILE_NAME.json") | |
| //get input stream size | |
| val size: Int = inputStream.available() | |
| //create a byte array of corresponing size | |
| val buffer = ByteArray(size) |
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
| @FragmentScope | |
| @Component( | |
| dependencies = [ApplicationComponent::class], | |
| modules = [ExampleModule::class] | |
| ) | |
| interface ExampleComponent { | |
| fun inject(fragment: ExampleFragment) | |
| } |
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
| fun interface Factory<T> { | |
| fun create(): T | |
| } | |
| fun <T> createInstance(factory: Factory<T>): T { | |
| return factory.create() | |
| } | |
| fun main() { | |
| val instance = createInstance(Factory { |
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
| class SomeClass { | |
| fun doTask() { | |
| for (i in 1..10) { | |
| println(i) | |
| } | |
| } | |
| } | |
| fun <T> createClassInstance(someClass: Class<T>): T{ | |
| return someClass.getDeclaredConstructor().newInstance() |
NewerOlder