<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rajesh Gupta on Medium]]></title>
        <description><![CDATA[Stories by Rajesh Gupta on Medium]]></description>
        <link>https://medium.com/@rajeshgupta_57912?source=rss-d827237cb50------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gI2Vi9wyj9PRTLfjwQ4tOA.jpeg</url>
            <title>Stories by Rajesh Gupta on Medium</title>
            <link>https://medium.com/@rajeshgupta_57912?source=rss-d827237cb50------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:26:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajeshgupta_57912/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What is a singleton , why it’s used, how it can be broken, and how to implement it correctly.]]></title>
            <link>https://medium.com/@rajeshgupta_57912/what-is-a-singleton-why-its-used-how-it-can-be-broken-and-how-to-implement-it-correctly-8ff8a41919f9?source=rss-d827237cb50------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ff8a41919f9</guid>
            <category><![CDATA[singleton-design-pattern]]></category>
            <dc:creator><![CDATA[Rajesh Gupta]]></dc:creator>
            <pubDate>Sat, 10 Aug 2024 12:07:30 GMT</pubDate>
            <atom:updated>2024-08-10T12:07:30.439Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>What is a singleton , why it’s used, how it can be broken, and how to implement it correctly</strong>.</h4><p>A singleton is a design pattern in software development that restricts the instantiation of a class to one single instance. This instace is globally accessible, meaning that the same object is used. wherever the singleton class is referenced within the application.</p><p>Why Use a Singleton?</p><ol><li>Controlled Access to a Single Instance</li><li>Global State Management</li><li>Resource Management</li><li>Thread Safety</li><li>Lazy Initialization</li></ol><p>Simple Example of a singleton in kotlin</p><pre>object MySingleton {<br>    var counter: Int = 0<br>    fun incrementCounter() {<br>        counter++<br>    }<br>}</pre><pre>fun main(){<br>  MySingleton.incrementCounter()<br>  println(MySingleton.counter) // Output will be 1<br>}</pre><p>How it can be broken?.</p><ol><li>Reflection:- Reflection can be used to bypass the private constructor of a singleton class, allowing multiple instances to be created</li></ol><pre>object MySingleton<br><br>fun main() {<br>    val constructor = MySingleton::class.java.getDeclaredConstructor()<br>    constructor.isAccessible = true<br>    val instance1 = constructor.newInstance()<br>    val instance2 = constructor.newInstance()<br><br>    println(instance1 === instance2) // false, two different instances<br>}</pre><p>2. Serialization:- When a singleton object is serialized and then deserialize, it can create a new instance, breaking the singleton pattern.</p><pre>@Serializable<br>object MySingleton<br><br>fun main() {<br>    val serializedObject = ObjectOutputStream(ByteArrayOutputStream()).use {<br>        it.writeObject(MySingleton)<br>        it.toByteArray()<br>    }<br><br>    val deserializedObject = ObjectInputStream(ByteArrayInputStream(serializedObject)).use {<br>        it.readObject() as MySingleton<br>    }<br><br>    println(MySingleton === deserializedObject) // false, two different instances<br>}</pre><p>3. Cloning:- If the singleton class implements ‘cloneable’, it might be possible to create a clone of the instance, resulting in multiple instance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ff8a41919f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>