<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prabath Kavinda</title>
    <description>The latest articles on DEV Community by Prabath Kavinda (@prabathkavinda).</description>
    <link>https://dev.to/prabathkavinda</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3043412%2F4bb9e755-02f8-41d1-9490-384b3f419613.png</url>
      <title>DEV Community: Prabath Kavinda</title>
      <link>https://dev.to/prabathkavinda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prabathkavinda"/>
    <language>en</language>
    <item>
      <title>Exploring Nature: Top Adventure Spots Near Sabaragamuwa University, Sri Lanka</title>
      <dc:creator>Prabath Kavinda</dc:creator>
      <pubDate>Fri, 30 May 2025 00:49:22 +0000</pubDate>
      <link>https://dev.to/prabathkavinda/exploring-nature-top-adventure-spots-near-sabaragamuwa-university-sri-lanka-p91</link>
      <guid>https://dev.to/prabathkavinda/exploring-nature-top-adventure-spots-near-sabaragamuwa-university-sri-lanka-p91</guid>
      <description>&lt;p&gt;If you’re studying or visiting Sabaragamuwa University, you’re in luck—some of Sri Lanka’s most beautiful landscapes are right at your doorstep. Here are a few adventure spots you shouldn’t miss:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzfvg7li3kd65tn8hk24.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzfvg7li3kd65tn8hk24.jpeg" alt="Image description" width="800" height="1066"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;• Pahanthuduwa Waterfall &amp;amp; Citrine River: A haven for hikers and nature lovers.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgjukrk160lkgfnmkcjsh.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgjukrk160lkgfnmkcjsh.jpeg" alt="Image description" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;• Samanala Dam: Stunning reservoir views and a peaceful escape.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fauhkn9da2lyhl2usvsrm.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fauhkn9da2lyhl2usvsrm.jpeg" alt="Image description" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;• Non-pareil: Great for scenic walks and mountain views.
• Surathali Ella: A local favorite for river swims and picnics.
• Adara Kantha: The ultimate hiking challenge with seven peaks.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Want to know more about the university and its surroundings? Check out &lt;a href="https://www.sab.ac.lk/" rel="noopener noreferrer"&gt;Sabaragamuwa University of Sri Lanka&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;** Image credits go to the original owners **&lt;/p&gt;

</description>
      <category>tourist</category>
      <category>travel</category>
      <category>sports</category>
    </item>
    <item>
      <title>🍪 “Wait, Cookies Know My Name?!” — A Tech Tale You Didn’t Know You Were Living</title>
      <dc:creator>Prabath Kavinda</dc:creator>
      <pubDate>Tue, 22 Apr 2025 07:09:48 +0000</pubDate>
      <link>https://dev.to/prabathkavinda/wait-cookies-know-my-name-a-tech-tale-you-didnt-know-you-were-living-3cf8</link>
      <guid>https://dev.to/prabathkavinda/wait-cookies-know-my-name-a-tech-tale-you-didnt-know-you-were-living-3cf8</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgcpdc8ua2q4d97wkkh5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgcpdc8ua2q4d97wkkh5.png" alt="image 01" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture this: You walk into your favorite café, and the barista smiles, “Hey! Your usual caramel latte?” You nod, amazed they remember. Now imagine the internet doing the exact same thing — but instead of a friendly barista, it’s tiny files called &lt;strong&gt;cookies&lt;/strong&gt; working behind the scenes.&lt;/p&gt;

&lt;p&gt;Wait — &lt;em&gt;what?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That’s right. Digital cookies aren’t the kind you dunk in milk. They’re little trackers that websites use to remember you. Some are helpful (like remembering your login), while others… well, let’s just say they’re the overly clingy type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvsckqogs2ed1gdpe1nq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvsckqogs2ed1gdpe1nq2.png" alt="image 02" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;🧠 So, What Are Cookie Files?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Cookies are small text files that websites save on your device when you visit them. Think of them like Post-it notes, your browser leaves for itself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;“User X loves cat videos — show more!”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“This person left items in their cart — send a reminder!”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“Dark mode ON, language: English.”&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without cookies, you’d have to log in every single time you open Instagram or re-add items to your Amazon cart. Annoying, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;🎯 A Real-Life Cookie Scenarios&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario 1&lt;/strong&gt;: You search for “best gaming laptops” on Google. Later, every website you visit shows you ads for gaming laptops. Creepy? Yep. That’s tracking cookies sharing your data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2&lt;/strong&gt;: You log into Netflix on your friend’s laptop but forget to log out. Next time they open Netflix, your profile shows up. That’s a session cookie doing its job (until you clear it).&lt;/p&gt;

&lt;p&gt;That’s cookies at work. They silently whisper to sites, “Hey, this user was interested in these items, maybe remind them?”&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;👨‍💻 What Do We Do With Cookies?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Websites use cookies to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Keep you logged in (lifesaver if you hate passwords)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Personalize your experience (like showing dark mode by default)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Show you targeted ads (yes, those sneakers keep following you)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Track usage for analytics (they learn what students click most)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As users, we can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;View and clear cookies from browser settings&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Block third-party cookies for privacy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use incognito mode to avoid tracking (kinda)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn9uuh2ta69l0hvar1885.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn9uuh2ta69l0hvar1885.png" alt="image 03" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;✅ Advantages of Cookies&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Convenience: You don’t have to type your username every time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Personalization: Netflix knows what type of weird documentaries you binge.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Speed: Faster loading for websites you’ve visited before.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;❌ But Not All Cookies Are Sweet&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Privacy concerns: Some cookies track you across multiple sites.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Security risks: In rare cases, cookies can be hijacked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Storage clutter: Over time, too many cookies can slow your browser.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;💡 One Last Crumb of Thought&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Cookies aren’t good or bad — they’re tools. The key? Control them. Clear cookies often, use incognito mode when needed, and remember: the internet remembers.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;🔥 Liked this?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Smash the clap button, follow for more tech-simplified stories, and comment: “What’s the weirdest ad you’ve gotten thanks to cookies?” Let’s compare notes! 👇&lt;/p&gt;

</description>
      <category>browser</category>
      <category>web</category>
      <category>cookie</category>
      <category>programming</category>
    </item>
    <item>
      <title>Factory Pattern-&lt;Creational&gt;</title>
      <dc:creator>Prabath Kavinda</dc:creator>
      <pubDate>Sun, 20 Apr 2025 14:30:39 +0000</pubDate>
      <link>https://dev.to/prabathkavinda/factory-pattern--2hmm</link>
      <guid>https://dev.to/prabathkavinda/factory-pattern--2hmm</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F189lpoojwdpr7y8n11tx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F189lpoojwdpr7y8n11tx.png" alt="image one" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Factory Design Pattern in Java: Brew Up Scalable Coffee Shop Software&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine you own a coffee shop that’s booming. More customers mean more coffee types: Espresso, Latte, Cappuccino, you name it. But, your ordering system becomes a mess. Each new drink requires changes, making things slow and hard to manage. How do you handle creating different objects without creating a headache? The Factory Design Pattern helps solve the problem. It helps you make your code flexible and easier to handle.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is the Factory Design Pattern?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Factory Design Pattern is like a coffee machine. You select a drink, and it gives you the correct cup. It handles the creation of objects. This pattern lets you create objects without knowing how they’re made. It’s a blueprint for making your code more adaptable.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Key Components&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This pattern has key ingredients that make it work well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Product: This is the interface or abstract class. It defines what kind of object the factory creates. In our shop, it’s the Coffee interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Concrete Product: These are the real implementations of the Product. Think Espresso, Latte, and Cappuccino classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Factory: An interface or abstract class that defines a method to create objects. It’s like the coffee machine’s button panel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Concrete Factory: This class creates specific products. EspressoFactory makes espressos. Each factory handles its own type of coffee.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Benefits of Using the Factory Pattern&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Why should you care about this pattern?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It makes your code less dependent on specific classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your code becomes easier to update and grow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It hides the details of object creation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It follows the Open/Closed Principle. This means you can add new types without changing existing code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Implementing the Factory Pattern in Java: The Coffee Shop Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  *** Defining the Product Interface: Coffee**
&lt;/h2&gt;

&lt;p&gt;First, we define a Coffee interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Coffee {
    void prepare();
    void brew();
    void pour();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This interface declares the actions a coffee must perform.&lt;/p&gt;

&lt;h2&gt;
  
  
  *** Creating Concrete Products: Espresso, Latte, Cappuccino**
&lt;/h2&gt;

&lt;p&gt;Next, create classes that implement the Coffee interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Espresso implements Coffee {
    @Override
    public void prepare() {
        System.out.println("Grinding espresso beans...");
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Override
    public void brew() {
        System.out.println("Brewing espresso...");
    }
    @Override
    public void pour() {
        System.out.println("Pouring espresso into a cup.");
    }
}
class Latte implements Coffee {
    @Override
    public void prepare() {
        System.out.println("Steaming milk...");
    }
    @Override
    public void brew() {
        System.out.println("Adding espresso...");
    }
    @Override
    public void pour() {
        System.out.println("Pouring latte into a cup.");
    }
}
class Cappuccino implements Coffee {
    @Override
    public void prepare() {
        System.out.println("Frothing milk...");
    }
    @Override
    public void brew() {
        System.out.println("Adding espresso...");
    }
    @Override
    public void pour() {
        System.out.println("Pouring cappuccino into a cup.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each class has its own way of preparing, brewing, and pouring.&lt;/p&gt;

&lt;h2&gt;
  
  
  *** Designing the Factory Interface: CoffeeFactory**
&lt;/h2&gt;

&lt;p&gt;Now, create a CoffeeFactory interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface CoffeeFactory {
    Coffee createCoffee(String type);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This interface has a method to create coffee based on type.&lt;/p&gt;

&lt;h2&gt;
  
  
  *** Implementing Concrete Factories:** EspressoFactory, LatteFactory, CappuccinoFactory
&lt;/h2&gt;

&lt;p&gt;Finally, implement the factory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SimpleCoffeeFactory implements CoffeeFactory {
    public Coffee createCoffee(String type) {
        Coffee coffee = null;
        if ("espresso".equalsIgnoreCase(type)) {
            coffee = new Espresso();
        } else if ("latte".equalsIgnoreCase(type)) {
            coffee = new Latte();
        } else if ("cappuccino".equalsIgnoreCase(type)) {
            coffee = new Cappuccino();
        }
        return coffee;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can create different kinds of coffee with ease. The factory handles the creation details.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Types of Factory Patterns&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Factory Pattern comes in different flavors.&lt;/p&gt;

&lt;p&gt;*** Simple Factory**&lt;/p&gt;

&lt;p&gt;This is the easiest type. It’s a single class that creates objects. But, it can become complex if you have many types. If you only need a factory once, then use it.&lt;/p&gt;

&lt;p&gt;*** Factory Method**&lt;/p&gt;

&lt;p&gt;This pattern lets subclasses decide which class to create. It’s great for frameworks where the parent class cannot know the exact object type.&lt;/p&gt;

&lt;p&gt;*** Abstract Factory**&lt;/p&gt;

&lt;p&gt;This creates families of related objects. Imagine a UI factory that makes buttons and windows for different operating systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Real-World Applications and Examples&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Factory Pattern isn’t just theory. It is used everywhere.&lt;/p&gt;

&lt;p&gt;*** Database Connection Factories**&lt;/p&gt;

&lt;p&gt;It can create connections to different databases. Your code doesn’t need to know the specific database details.&lt;/p&gt;

&lt;p&gt;*** UI Component Factories**&lt;/p&gt;

&lt;p&gt;It makes UI elements for different platforms. For example, create buttons or text fields.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Factory Design Pattern brings flexibility and order to object creation. It makes your code easier to maintain and grow. Embrace this pattern and make your software shine. Design patterns are like secret tools that help you solve tough problems. Start using them.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>designpatterns</category>
      <category>java</category>
      <category>factorypattern</category>
    </item>
    <item>
      <title>Singleton Pattern-&lt;Creational&gt;</title>
      <dc:creator>Prabath Kavinda</dc:creator>
      <pubDate>Fri, 18 Apr 2025 17:01:58 +0000</pubDate>
      <link>https://dev.to/prabathkavinda/singleton-pattern--17ii</link>
      <guid>https://dev.to/prabathkavinda/singleton-pattern--17ii</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;01) What?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1.1) Real World Scenario&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a scenario where having multiple copies of something could cause problems. Think about a database connection, or maybe a printer. If everyone had their own connection or tried to print at the same time, things could get messy real fast. The Singleton pattern is a clever way to make sure you only have one instance of a particular class. It’s a common pattern in software design, and it’s super useful for managing resources efficiently. In this guide, we’ll explore what the Singleton pattern is, why it matters, and how you can use it. You’ll learn how to implement it and when it’s the right tool for the job.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpymp9741mkk2szklyoy6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpymp9741mkk2szklyoy6.png" alt="Image01"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.2) What is the Singleton Pattern?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Singleton pattern is all about control. It ensures that a class has only one instance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Plus, it provides a single, global point of access to that instance. Think of it like the principal of your school. There’s only one principal, and everyone knows who to go to if they need to talk to them. That’s essentially what the Singleton pattern does for classes in your code.&lt;/p&gt;

&lt;p&gt;Singleton Pattern comes under the Creational Design Patterns. There are two more types in Design Patterns, need to know? read my previous story&lt;/p&gt;


&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/prabathkavinda/design-patterns-in-software-development-4eaa" class="crayons-story__hidden-navigation-link"&gt;Design Patterns in Software Development&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/prabathkavinda" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3043412%2F4bb9e755-02f8-41d1-9490-384b3f419613.png" alt="prabathkavinda profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/prabathkavinda" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Prabath Kavinda
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Prabath Kavinda
                
              
              &lt;div id="story-author-preview-content-2401218" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/prabathkavinda" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3043412%2F4bb9e755-02f8-41d1-9490-384b3f419613.png" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Prabath Kavinda&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/prabathkavinda/design-patterns-in-software-development-4eaa" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Apr 12 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/prabathkavinda/design-patterns-in-software-development-4eaa" id="article-link-2401218"&gt;
          Design Patterns in Software Development
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/designpatterns"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;designpatterns&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/softwaredevelopment"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;softwaredevelopment&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/oop"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;oop&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
            &lt;a href="https://dev.to/prabathkavinda/design-patterns-in-software-development-4eaa#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              1&lt;span class="hidden s:inline"&gt; comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            3 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;02) Why?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Why would you want only one instance of a class? Well, there are many reasons. Sometimes, it’s about managing a shared resource, like a database connection. Only one connection is needed, and it’s more efficient to share it. Other times, it’s about ensuring consistent behavior across your application. If you have multiple instances, they might get out of sync, leading to unexpected issues. The Singleton pattern helps avoid these problems by guaranteeing that everyone is using the same instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;03) Benefits?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;3.1) Controlled Resource Access,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Singletons are great for managing shared resources. Imagine you have a logging system. You want to make sure all parts of your application write logs to the same place. A Singleton logger can ensure this, preventing different parts of the code from creating separate log files or messing with each other’s logs. This makes debugging and monitoring much easier. For example, a print spooler is a great use of the singleton pattern because it ensures only one job prints at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.2) Lazy Initialization and Performance,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Singletons can be lazily initialized. This means the instance is only created when it’s first needed. This can improve startup performance. If you have a class that takes a long time to initialize, you don’t want to create it when your application starts. You only want to create it when it’s actually used. This is where lazy initialization comes in handy. But there’s a trade-off: the first time you access the instance, there might be a slight delay while it’s being created.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.3) Namespace Management,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Singletons help keep your code organized. They provide a single, well-defined point of access, reducing the risk of naming conflicts. Think of it like having one official address for a company. Instead of having multiple addresses floating around, everyone knows to use the same one. This simplifies your code and makes it easier to understand and maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;04) Step by Step?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s look at how to implement the Singleton pattern. Here I use Java to implement this. You can use your familiar language, structure and the concept is same only the syntax will change as according to language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.1) Basic Implementation,&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Singleton {
    private static Singleton instance;

    private Singleton() {
        // Use Private constructor to prevent from instantiation
    }

      // When user needs to use this instance, get method will help

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code shows the three key elements: a private constructor, a static instance variable, and a static getInstance() method. The getInstance() method checks if an instance already exists. If not, it creates one and returns it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.2) Thread-Safe Singleton Implementation,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a multi-threaded environment, you need to be careful. Multiple threads might try to create an instance at the same time, which would break the Singleton pattern. To prevent this, you can use locking mechanisms. Here’s an example using double-checked locking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Singleton {
    private static volatile Singleton instance;

    private Singleton() {
        // Use Private constructor to prevent from instantiation
    }

    // When user needs to use this instance, get method will help

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;05) Where?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;5.1) Database Connection Management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.2) Configuration Settings&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.3) Logging&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.4) Cache Manager&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>singleton</category>
      <category>java</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Design Patterns in Software Development</title>
      <dc:creator>Prabath Kavinda</dc:creator>
      <pubDate>Sat, 12 Apr 2025 03:32:20 +0000</pubDate>
      <link>https://dev.to/prabathkavinda/design-patterns-in-software-development-4eaa</link>
      <guid>https://dev.to/prabathkavinda/design-patterns-in-software-development-4eaa</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bta150yeeiagcazhgle.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bta150yeeiagcazhgle.jpg" alt="First Image" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Design Patterns in Software Development: A Practical Guide&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Ever worked on a software project that turned into a tangled mess? Maybe adding a simple feature became a nightmare. Or perhaps the code was so confusing that nobody dared to touch it. These problems often stem from poor design choices. But there's a solution!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Design patterns are like blueprints for solving common software design problems. They're reusable solutions that can make your code easier to understand, maintain, and scale. By using design patterns, you can write better code, faster.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What are Design Patterns?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Design patterns are proven solutions to recurring problems in software design. Think of them as templates you can adapt to solve specific issues. They are not finished designs that can be transformed directly into code. A design pattern is a description or template for how to solve a problem that can be used in many different situations.&lt;/p&gt;

&lt;p&gt;Unlike algorithms or data structures, design patterns aren't specific pieces of code. They are more like general guidelines for structuring your code. These patterns show how to arrange classes and objects to solve a design problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprg4cxuq95i7l7rsxpmh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprg4cxuq95i7l7rsxpmh.jpg" alt="Second Image" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Categories of Design Patterns&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Design patterns are typically grouped into three main categories. These categories reflect the type of problem they solve:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Creational: These patterns deal with object creation mechanisms. They help you create objects in a flexible and controlled way.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Structural: These patterns focus on how to compose objects into larger structures. They help you define relationships between objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Behavioral: These patterns address object interaction and responsibility assignment. They define how objects communicate and collaborate.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Benefits of Using Design Patterns&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Using design patterns offers several key advantages. They improve code reusability. Once you understand a pattern, you can apply it in different projects. Code that follows established patterns is easier to understand and modify.&lt;/p&gt;

&lt;p&gt;Design patterns can also improve scalability. A well-designed application can handle increasing workloads and new features without major rework. For example, using the Observer pattern can allow you to add new features that react to events, with minimal impact to the system. This saves time in the long run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhkobxd1j3veydwegxdbb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhkobxd1j3veydwegxdbb.jpg" alt="Third Image" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Choosing the Right Design Pattern&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Choosing the right design pattern is important. You need to understand the problem you're trying to solve. Consider the tradeoffs involved. Don't just apply a pattern because it sounds cool.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Understanding the Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Carefully analyze the problem domain. Identify the core design challenges. What are the key requirements? What are the constraints? A clear understanding of the problem will guide you towards the right pattern. You have to know what you're solving to pick the right pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Evaluating Tradeoffs&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Each design pattern has its own tradeoffs. Some patterns add complexity to the code. Some could impact performance. Others may make the code harder to maintain. Consider these factors before choosing a pattern. There isn't a free lunch.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;When NOT to Use Design Patterns&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Not every problem requires a design pattern. Sometimes, simpler solutions are better. Overusing patterns can lead to over-engineering. This makes the code more complicated than it needs to be. Remember, the goal is to solve the problem. Keep the code simple and easy to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Design patterns offer numerous benefits in software development. They improve code quality. Also, they reduce development time.&lt;/p&gt;

&lt;p&gt;Understanding different types of design patterns is key. Pick the right one for the problem. Do it to make good code.&lt;/p&gt;

&lt;p&gt;Continue to learn and experiment with design patterns. Your software development skills will improve.&lt;/p&gt;

&lt;p&gt;Have you used design patterns in your projects? What are your experiences? Share your thoughts and ask questions in the comments below!&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>softwaredevelopment</category>
      <category>programming</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
