đ Kotlin Companion Object – āĻŦ্āϰেāĻāύ-āĻĢ্āϰেāύ্āĻĄāϞি āĻŦ্āϝাāĻ্āϝা! đ¯
đ Kotlin Companion Object – āĻŦ্āϰেāĻāύ-āĻĢ্āϰেāύ্āĻĄāϞি āĻŦ্āϝাāĻ্āϝা! đ¯
đ¤ Companion Object āĻী?
đ Kotlin-āĻ static keyword āύেāĻ, āĻিāύ্āϤু āĻ
āύেāĻ āϏāĻŽā§ Java-āĻāϰ static behavior āĻĒ্āϰā§োāĻāύ āĻšā§!
đ Companion Object āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰে āĻāĻāĻি āĻ্āϞাāϏেāϰ āĻেāϤāϰে āĻāĻŽāύ āĻিāĻু āĻĄিāĻĢাāĻāύ āĻāϰা āϝাā§, āϝা āĻ্āϞাāϏেāϰ āĻ
āĻŦāĻেāĻ্āĻ āĻাā§াāĻ āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰা āϏāĻŽ্āĻāĻŦ!
đ āĻāĻি āĻŽূāϞāϤ Singleton-āĻāϰ āĻŽāϤো āĻাāĻ āĻāϰে!
đĨ Companion Object āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰে Static-āĻāϰ āĻŦিāĻāϞ্āĻĒ!
✅ Java-āĻāϰ static āĻĢিāĻাāϰেāϰ āĻŽāϤো āĻাāĻ āĻāϰে!
✅ āĻোāύো āĻ
āĻŦāĻেāĻ্āĻ āϤৈāϰি āύা āĻāϰেāĻ Companion Object-āĻāϰ āĻŽেāĻĨāĻĄ āĻāĻŦং āĻ্āϝাāϞু āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰা āϝাā§!
đ āĻāĻĻাāĻšāϰāĻŖ:
class MathUtils {
companion object {
fun square(x: Int): Int {
return x * x
}
}
}
fun main() {
println(MathUtils.square(5)) // 25
}
đ ️ Output:
25
✅ MathUtils.square(5) āĻāϞ āĻāϰāϤে āĻোāύো āĻ
āĻŦāĻেāĻ্āĻ āϤৈāϰি āĻāϰāϤে āĻšā§āύি!
✅ companion object āĻāϰ āĻেāϤāϰেāϰ āĻŽেāĻĨāĻĄ āĻ্āϞাāϏেāϰ āύাāĻŽ āĻĻিā§েāĻ āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰা āϏāĻŽ্āĻāĻŦ!
đ Companion Object-āĻāϰ āĻŽাāϧ্āϝāĻŽে Properties & Functions āϏংāϰāĻ্āώāĻŖ āĻāϰা āϝাā§!
class AppConfig {
companion object {
const val VERSION = "1.0.0"
fun getAppInfo(): String {
return "App Version: $VERSION"
}
}
}
fun main() {
println(AppConfig.VERSION) // 1.0.0
println(AppConfig.getAppInfo()) // App Version: 1.0.0
}
đ ️ Output:
1.0.0
App Version: 1.0.0
✅ āĻāĻাāύে VERSION āĻāĻŦং getAppInfo() āĻĢাংāĻļāύ āĻ্āϞাāϏেāϰ āύাāĻŽ āĻĻিā§েāĻ āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰা āϝাāĻ্āĻে!
đ¯ Companion Object āĻে āύাāĻŽ āĻĻেāĻā§া āϝাā§!
đ āĻিāĻু āϏāĻŽā§ companion object āĻে āύিāϰ্āĻĻিāώ্āĻ āύাāĻŽ āĻĻেāĻā§া āĻĻāϰāĻাāϰ āĻšāϤে āĻĒাāϰে!
class UserManager {
companion object Instance {
fun getUserCount(): Int {
return 10
}
}
}
fun main() {
println(UserManager.getUserCount()) // 10
}
✅ āĻāĻাāύে companion object Instance āύাāĻŽ āĻĻেāĻā§া āĻšā§েāĻে, āĻিāύ্āϤু .getUserCount() āĻāĻেāϰ āĻŽāϤোāĻ āĻাāĻ āĻāϰāĻে!
đ Companion Object + Factory Pattern! (Singleton Alternative)
đ Companion Object āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰে Factory Method āϤৈāϰি āĻāϰা āϝাā§!
đ āĻāĻি āĻ্āϞাāϏেāϰ āĻ
āĻŦāĻেāĻ্āĻ āϤৈāϰি āĻāϰাāĻে āĻāϰāĻ āύিā§āύ্āϤ্āϰিāϤ āĻāϰে!
class User private constructor(val name: String) {
companion object {
fun create(name: String): User {
return User(name)
}
}
}
fun main() {
val user1 = User.create("Alice")
val user2 = User.create("Bob")
println(user1.name) // Alice
println(user2.name) // Bob
}
✅ āĻāĻাāύে User āĻ্āϞাāϏেāϰ Constructor private, āĻিāύ্āϤু companion object āĻĻিā§ে āύāϤুāύ āĻ
āĻŦāĻেāĻ্āĻ āϤৈāϰি āĻāϰা āĻšā§েāĻে!
✅ āĻāĻি Factory Pattern āĻ
āύুāϏāϰāĻŖ āĻāϰে!
đ āϏংāĻ্āώেāĻĒে:
✔️ Companion Object āĻšāϞো āĻāĻāĻি Singleton āĻ
āĻŦāĻেāĻ্āĻ, āϝা āĻ্āϞাāϏেāϰ āϏাāĻĨে āϝুāĻ্āϤ āĻĨাāĻে!
✔️ Java-āĻāϰ static āĻĢিāĻাāϰেāϰ āĻŦিāĻāϞ্āĻĒ āĻšিāϏেāĻŦে āĻাāĻ āĻāϰে!
✔️ āĻোāύো āĻ
āĻŦāĻেāĻ্āĻ āϤৈāϰি āĻাā§াāĻ Companion Object-āĻāϰ āĻĒ্āϰāĻĒাāϰ্āĻি āĻ āĻŽেāĻĨāĻĄ āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰা āϝাā§!
✔️ Factory Pattern, Static Constants, āĻāĻŦং Utility Functions āϤৈāϰি āĻāϰāϤে āĻĻাāϰুāĻŖ āĻাāϰ্āϝāĻāϰ!
đĨ āĻāĻāύ āĻĨেāĻে Companion Object āĻŦ্āϝāĻŦāĻšাāϰ āĻāϰে āĻāϰāĻ āĻ্āϞিāύ āĻ āĻĒাāϰāĻĢāϰāĻŽ্āϝাāύ্āϏ āĻাāϞো āĻāϰা Kotlin āĻোāĻĄ āϞিāĻāϤে āĻĒাāϰāĻŦেāύ! đ
Comments
Post a Comment