Skip to content

Instantly share code, notes, and snippets.

@aroranubhav
aroranubhav / ClassCreationWithFactory.Kt
Created February 16, 2026 08:45
Class Creation with Factory pattern.
/**
* 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
@aroranubhav
aroranubhav / ClassCreationWithReflection.Kt
Created February 16, 2026 08:34
Reflection for object creation
/**
* 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
*
@aroranubhav
aroranubhav / LiveDataWithoutLifecycle.kt
Created February 4, 2026 12:37
LiveData Implementation without Lifecycle
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> {
@aroranubhav
aroranubhav / Continuation.kt
Last active November 18, 2025 08:17
Continuation-Passing Style (CPS) — How Kotlin Coroutines Work Under the Hood
/**
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
**/
@aroranubhav
aroranubhav / AsyncWithCoroutines.kt
Created November 18, 2025 06:05
Running Asynchronous Tasks in Android Using Coroutines
/**
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"
}
@aroranubhav
aroranubhav / AsyncWithThreads.java
Created November 18, 2025 05:57
Running Asynchronous Tasks in Android Using Raw Threads
/**
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()
@aroranubhav
aroranubhav / ReadJsonFromAssets.kt
Last active May 9, 2025 10:36
Read Json File from Assets Folder and map it to a model class
//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)
@aroranubhav
aroranubhav / ExampleComponent.kt
Created March 5, 2025 08:20
Dagger DI in Fragments
@FragmentScope
@Component(
dependencies = [ApplicationComponent::class],
modules = [ExampleModule::class]
)
interface ExampleComponent {
fun inject(fragment: ExampleFragment)
}
@aroranubhav
aroranubhav / ClassInstanceViaFactory.kt
Created March 3, 2025 20:35
Classs Instance Via Factory
fun interface Factory<T> {
fun create(): T
}
fun <T> createInstance(factory: Factory<T>): T {
return factory.create()
}
fun main() {
val instance = createInstance(Factory {
@aroranubhav
aroranubhav / ClassInstanceViaReflection.kt
Created March 3, 2025 20:23
Creating class instance via Reflection Example
class SomeClass {
fun doTask() {
for (i in 1..10) {
println(i)
}
}
}
fun <T> createClassInstance(someClass: Class<T>): T{
return someClass.getDeclaredConstructor().newInstance()