<?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: Thanh Tam</title>
    <description>The latest articles on DEV Community by Thanh Tam (@krissirk0906).</description>
    <link>https://dev.to/krissirk0906</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%2F994513%2F32056d6a-afe4-4741-9fc3-02c20c036f71.jpeg</url>
      <title>DEV Community: Thanh Tam</title>
      <link>https://dev.to/krissirk0906</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/krissirk0906"/>
    <language>en</language>
    <item>
      <title>Singleton in Android</title>
      <dc:creator>Thanh Tam</dc:creator>
      <pubDate>Fri, 06 Jan 2023 01:55:29 +0000</pubDate>
      <link>https://dev.to/krissirk0906/singleton-design-pattern-in-android-25dd</link>
      <guid>https://dev.to/krissirk0906/singleton-design-pattern-in-android-25dd</guid>
      <description>&lt;p&gt;The Singleton design pattern is a software design pattern that ensures that a class has only one instance, and provides a global point of access to that instance. This can be useful in Android development when you need to share data or state across multiple activities or fragments.&lt;/p&gt;

&lt;p&gt;There are several ways to implement the Singleton pattern in Android, each with its own set of pros and cons. Here, we'll take a look at three common approaches:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lazy initialization: This is the most straightforward way to implement the Singleton pattern. The instance is created only when it is needed, and it is stored as a static field. This approach is simple, but it is not thread-safe and can cause performance issues if the instance is expensive to create.&lt;/li&gt;
&lt;li&gt;Eager initialization: In this approach, the instance is created as a static field when the class is first loaded. This ensures that the instance is always available, but it can waste resources if the instance is not actually needed.&lt;/li&gt;
&lt;li&gt;Double-checked locking: This approach uses a combination of lazy initialization and synchronization to create the instance in a thread-safe way. It is more complex than the other two approaches, but it provides the best performance in cases where the instance is expensive to create.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which approach is best for your project will depend on your specific needs. Lazy initialization is a good choice if the instance is inexpensive to create, while double-checked locking is a better choice if the instance is expensive to create. Eager initialization is a good choice if the instance must be available immediately, but it can waste resources if the instance is not actually needed.&lt;/p&gt;

&lt;p&gt;In summary, the Singleton design pattern is a useful tool for managing global state and shared data in Android development. By carefully considering the trade-offs between different implementation approaches, you can choose the one that is best suited to your project's needs.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>automation</category>
    </item>
    <item>
      <title>SOLID in Android</title>
      <dc:creator>Thanh Tam</dc:creator>
      <pubDate>Thu, 05 Jan 2023 01:49:58 +0000</pubDate>
      <link>https://dev.to/krissirk0906/solid-in-android-35f</link>
      <guid>https://dev.to/krissirk0906/solid-in-android-35f</guid>
      <description>&lt;p&gt;SOLID is a set of five principles that can help developers write more maintainable and scalable software. The acronym stands for:&lt;/p&gt;

&lt;p&gt;Single Responsibility Principle (SRP)&lt;br&gt;
Open-Closed Principle (OCP)&lt;br&gt;
Liskov Substitution Principle (LSP)&lt;br&gt;
Interface Segregation Principle (ISP)&lt;br&gt;
Dependency Inversion Principle (DIP)&lt;br&gt;
In this blog post, we'll go through each of these principles and discuss how they can be applied to Android development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Single Responsibility Principle (SRP)
&lt;/h2&gt;

&lt;p&gt;The Single Responsibility Principle (SRP) states that a class should have only one reason to change. In other words, a class should have a single, well-defined responsibility.&lt;/p&gt;

&lt;p&gt;One way to apply this principle in Android development is to create classes that are specific to a particular feature or task. For example, you might have a class that is responsible for handling network requests, a class that is responsible for rendering a list of items on the screen, and a class that is responsible for storing data in a database.&lt;/p&gt;

&lt;p&gt;By creating classes that have a single responsibility, you can make your code more maintainable and easier to understand. When you need to change something, you know exactly where to look and you don't have to wade through a lot of unrelated code to find what you're looking for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Open-Closed Principle (OCP)
&lt;/h2&gt;

&lt;p&gt;The Open-Closed Principle (OCP) states that software entities (such as classes, modules, and functions) should be open for extension but closed for modification.&lt;/p&gt;

&lt;p&gt;This means that you should design your classes in such a way that they can be extended to add new functionality without changing the existing code.&lt;/p&gt;

&lt;p&gt;One way to apply this principle in Android development is to use inheritance and polymorphism. For example, you might create a base class that defines a set of common methods and properties, and then create derived classes that override or extend those methods as needed. This allows you to add new functionality without modifying the base class.&lt;/p&gt;

&lt;p&gt;Another way to apply the OCP is to use interfaces. By defining an interface and then implementing it in your classes, you can add new functionality without changing the existing code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Liskov Substitution Principle (LSP)
&lt;/h2&gt;

&lt;p&gt;The Liskov Substitution Principle (LSP) states that objects of a subclass should be able to be used in the same way as objects of the base class.&lt;/p&gt;

&lt;p&gt;In other words, if a class is derived from another class, it should be able to be used in the same way as the base class without causing any problems.&lt;/p&gt;

&lt;p&gt;To apply this principle in Android development, you should ensure that your derived classes correctly implement the methods and properties of the base class. This means that the derived class should be a "strict superset" of the base class, meaning that it should have all of the same methods and properties, and possibly more.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interface Segregation Principle (ISP)
&lt;/h2&gt;

&lt;p&gt;The Interface Segregation Principle (ISP) states that clients should not be forced to depend on interfaces they do not use.&lt;/p&gt;

&lt;p&gt;In other words, you should not create interfaces with a lot of methods that are not relevant to all of the classes that implement them. This can lead to "fat" interfaces that are difficult to implement and maintain.&lt;/p&gt;

&lt;p&gt;To apply this principle in Android development, you should create smaller, more focused interfaces that only contain the methods that are relevant to a specific group of classes. This will make it easier for those classes to implement the interface and will also make your code&lt;/p&gt;

</description>
      <category>android</category>
    </item>
    <item>
      <title>What is Dependency Injection</title>
      <dc:creator>Thanh Tam</dc:creator>
      <pubDate>Wed, 04 Jan 2023 10:50:20 +0000</pubDate>
      <link>https://dev.to/krissirk0906/what-is-dependency-injection-1pl5</link>
      <guid>https://dev.to/krissirk0906/what-is-dependency-injection-1pl5</guid>
      <description>&lt;p&gt;Dependency injection is a software design pattern that allows a system to be more flexible and modular by decoupling the implementation of objects from the objects that use them. It does this by allowing the objects that use other objects (called clients) to specify which objects they will use (called dependencies) at runtime, rather than having the clients create the dependencies themselves.&lt;/p&gt;

&lt;p&gt;The key benefit of dependency injection is that it enables clients to specify their dependencies at runtime rather than hardcoding them. This makes it easier to change the implementation of a dependency without affecting the client, as well as making it easier to test the client in isolation.&lt;/p&gt;

&lt;p&gt;There are several ways to implement dependency injection, including constructor injection, setter injection, and interface injection. In constructor injection, the dependencies are provided to the client via the constructor when the client is created. In setter injection, the dependencies are set on the client using setter methods. In interface injection, the dependencies are passed to the client through an interface implemented by the client.&lt;/p&gt;

&lt;p&gt;Here's an example of constructor injection in Kotlin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface FileSystem {
    fun readFile(path: String): String
}

class RealFileSystem: FileSystem {
    override fun readFile(path: String): String {
        // implementation to read a file from the real file system goes here
    }
}

class MyClass(private val fileSystem: FileSystem) {
    fun doSomething() {
        val fileContents = fileSystem.readFile("/path/to/file.txt")
        // use the file contents to do something
    }
}

val fileSystem = RealFileSystem()
val myClass = MyClass(fileSystem)
myClass.doSomething()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, MyClass has a dependency on FileSystem, which it needs to read a file. The FileSystem dependency is injected into MyClass via the constructor when MyClass is created. This allows MyClass to use any implementation of FileSystem without knowing how it is implemented.&lt;/p&gt;

&lt;p&gt;It's also possible to use setter injection in Kotlin like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface FileSystem {
    fun readFile(path: String): String
}

class RealFileSystem: FileSystem {
    override fun readFile(path: String): String {
        // implementation to read a file from the real file system goes here
    }
}

class MyClass {
    private lateinit var fileSystem: FileSystem

    fun setFileSystem(fileSystem: FileSystem) {
        this.fileSystem = fileSystem
    }

    fun doSomething() {
        val fileContents = fileSystem.readFile("/path/to/file.txt")
        // use the file contents to do something
    }
}

val fileSystem = RealFileSystem()
val myClass = MyClass()
myClass.setFileSystem(fileSystem)
myClass.doSomething()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the fileSystem dependency is set on MyClass using the setFileSystem setter method.&lt;/p&gt;

&lt;p&gt;Overall, dependency injection is a useful pattern for achieving loose coupling between objects, which can make a system more flexible and easier to maintain.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>android</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
