<?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: Iyanu Falaye</title>
    <description>The latest articles on DEV Community by Iyanu Falaye (@falay).</description>
    <link>https://dev.to/falay</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%2F1158181%2Fe327990c-f740-4487-903c-260bee48e9b0.png</url>
      <title>DEV Community: Iyanu Falaye</title>
      <link>https://dev.to/falay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/falay"/>
    <language>en</language>
    <item>
      <title>Design Patterns: Singleton</title>
      <dc:creator>Iyanu Falaye</dc:creator>
      <pubDate>Mon, 26 Feb 2024 13:32:02 +0000</pubDate>
      <link>https://dev.to/falay/design-patterns-singleton-4713</link>
      <guid>https://dev.to/falay/design-patterns-singleton-4713</guid>
      <description>&lt;p&gt;Design patterns are a collection of standard solutions to solve software development design problems. Software engineers need to know how to use design patterns as essential tools. They can utilize design patterns to discover elegant solutions for commonly occurring challenges in software design, just as architects in building architecture learn to address design difficulties using established patterns. The efficiency, scalability, and readability of code are all improved by comprehending and utilizing these patterns.&lt;br&gt;
The three categories that these patterns fall into are creational, behavioural, and structural. To keep things brief, we’ll define these categories and explore examples that fall into these categories over a series.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creational Pattern:&lt;/strong&gt; These patterns address procedures for creating objects. They assist in crafting objects in a way that makes sense for the scenario. The simplest method of creating objects may lead to problems with design or more complexity in a design. This issue is resolved by creational design patterns, which regulate the creation process. Some of the popular creational patterns include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Builder&lt;/li&gt;
&lt;li&gt;Factory&lt;/li&gt;
&lt;li&gt;Abstract Factory&lt;/li&gt;
&lt;li&gt;Singleton&lt;/li&gt;
&lt;li&gt;Prototype&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Structural Pattern:&lt;/strong&gt; The composition of classes and objects is central to structural patterns. They support the formation of complex structures with classes and objects. Structural patterns use inheritance to create interfaces and implementations. Several well-known patterns of structure include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adapter&lt;/li&gt;
&lt;li&gt;Decorator&lt;/li&gt;
&lt;li&gt;Facade&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Behavioural Pattern:&lt;/strong&gt; a category of software design patterns that helps control how various system elements communicate and interact. Example includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Observer&lt;/li&gt;
&lt;li&gt;Strategy&lt;/li&gt;
&lt;li&gt;State&lt;/li&gt;
&lt;li&gt;Iterator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’ll start the series with one of the creational patterns: Singleton.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Singleton Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Singleton pattern ensures that the instance of a class is instantiated once and provides global access to the instance. It is part of the creational design pattern category.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Motivation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When building a system, sometimes we want the system to have access to a shared resource, e.g. a database and a logging system. To make this work as intended, a single instance of an object or resource must exist at a time. Instantiating a constructor would not work because it returns a new instance of an object whenever it is called. Singleton pattern solves for us by ensuring that the regular constructor is called only once under the hood, and that is when it is being created for the first time.&lt;br&gt;
Singleton pattern also provides global access to an object by making an object available everywhere in a program, which helps with;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Consistency: there should be at most one instance of a database in a system; if there is, it would further improve the level of consistency in the application's behaviour.&lt;/li&gt;
&lt;li&gt;It helps to conserve resources&lt;/li&gt;
&lt;li&gt;It also helps to control the means to access an object.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class DataBaseService {
    public DataBaseService(){
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Creating a DataBaseService class like the example above,&lt;br&gt;
every time the class is instantiated, a new instance of the DatabaseService class is created, as shown below, which makes the class inconsistent for the intended use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dataBase3: singleton.DataBaseService@135fbaa4
dataBase4: singleton.DataBaseService@45ee12a7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the implementation of a singleton pattern with respect to multithreaded environments like a Java program. This code below exposes a &lt;em&gt;getInstance()&lt;/em&gt; method, which is called when we want to access the DataBaseService object, and it handles creating or providing an existing instance of the DataBaseService object. The private constructor in the class ensures the creation of an object using the regular constructor calling is controlled internally by the class. If &lt;em&gt;dataBaseService&lt;/em&gt; is null, the class has not been created before and the regular constructor will be called.&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 DataBaseService {

    private static volatile DataBaseService dataBaseService = null;

    private DataBaseService() {
        if (dataBaseService != null) {
            throw  new RuntimeException("There can only be one instance of Database");
        }
    }

    public static DataBaseService getInstance() {
        if (dataBaseService == null){
        synchronized (DataBaseService.class) {
            if (dataBaseService == null) {
                dataBaseService = new DataBaseService();
            }
        }
    }

        return dataBaseService;
    }

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

&lt;/div&gt;



&lt;p&gt;When we want to access the &lt;em&gt;DataBaseService&lt;/em&gt;, we simply call the getInstance method, which creates or returns one instance of the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; DataBaseService dataBaseService1 = DataBaseService.getInstance();
        DataBaseService dataBaseService2 = DataBaseService.getInstance();
        System.out.println("dataBaseService1: "+ dataBaseService1);
        System.out.println("dataBaseService2: " + dataBaseService2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the two instances are logged, it is evidently the same, which shows that our singleton pattern is properly implemented as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dataBaseService1: singleton.DataBaseService@135fbaa4
dataBaseService12: singleton.DataBaseService@135fbaa4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is sure that there is only one instance of a class.&lt;/li&gt;
&lt;li&gt;You can provide a global instance to a class.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Violates single responsibility principles&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Use cases&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Databases&lt;br&gt;
Logging system&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-Life Examples&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;Java Runtime Library&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Singleton Pattern's approach to instance control makes it significant, but it's equally crucial to understand its strengths and weaknesses. Here's a &lt;a href="https://github.com/falayy/design-patterns-example/tree/main/src/singleton"&gt;link&lt;/a&gt; to check the full implementation. &lt;/p&gt;

&lt;p&gt;Shout out to &lt;a href="https://unsplash.com/@markusspiske"&gt;Markus Spiske&lt;/a&gt; for the &lt;a href="https://unsplash.com/photos/colorful-software-or-web-code-on-a-computer-monitor-Skf7HxARcoc"&gt;cover photo&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>mobile</category>
      <category>designpatterns</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Design Patterns: Proxy</title>
      <dc:creator>Iyanu Falaye</dc:creator>
      <pubDate>Sat, 11 Nov 2023 23:03:47 +0000</pubDate>
      <link>https://dev.to/falay/design-patterns-proxy-46b0</link>
      <guid>https://dev.to/falay/design-patterns-proxy-46b0</guid>
      <description>&lt;p&gt;The proxy design pattern is a structural design pattern where a class often known as the intermediary represents the functionality of another class. the main object is known as the &lt;strong&gt;real object&lt;/strong&gt; while the intermediary is known as the &lt;strong&gt;proxy&lt;/strong&gt;. The proxy when invoked can access the real object behind the scenes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Motivation&lt;/strong&gt;&lt;br&gt;
When creating an object is resource-intensive or requires significant overhead. It can be inefficient to instantiate these objects when they might not be needed. Or, take into account scenarios where you need to restrict access to an object for security, logging, or other reasons. To tackle these situations, the Proxy Pattern can be used without changing the code of the real object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;br&gt;
A proxy implements the same interface as the real object. Considering a system that fetches images from a server. Here is the &lt;strong&gt;Image&lt;/strong&gt; Interface that the proxy (&lt;strong&gt;ProxyImage&lt;/strong&gt;) and the real object (&lt;strong&gt;RemoteImage&lt;/strong&gt;) implement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface Image{
    void loadImage(String fileName);
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;RemoteImage&lt;/strong&gt; is the concrete implementation of the Image interface and the &lt;em&gt;loadImage&lt;/em&gt; method fetches the image from the server.&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 RemoteImage implements Image {
    @Override
    public void loadImage(String fileName) {
        System.out.println("Fetching From server..." + fileName +".png");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ProxyImage class is the object that the client interacts with, when rhe &lt;em&gt;loadImage&lt;/em&gt; method is called, the system attempts to fetch the image from the cache by checking if the filename exists if it exists it prints the image if the image doesn't exist it fetches from the remote source.&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 ProxyImage implements Image {

    private final Map&amp;lt;String, String&amp;gt; imageCache = new HashMap&amp;lt;&amp;gt;();

    @Override
    public void loadImage(String fileName) {
        if (imageCache.containsKey(fileName)) {
            String image = imageCache.get(fileName);
            System.out.println("Loading Image From cache : " + image);
        } else {
            RemoteImage remoteImage = new RemoteImage();
            remoteImage.loadImage(fileName);
            imageCache.put(fileName, fileName + ".png");
            System.out.println("No Image From Cache... Loading From remote" + remoteImage);

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

&lt;/div&gt;



&lt;p&gt;This is the client communicating with the proxy and the output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ProxyImage proxyImageService = new ProxyImage();
        proxyImageService.loadImage("image");
        proxyImageService.loadImage("image");
&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;Loading From Url...image.png
No Image From Cache... Loading From remoteproxy.RemoteImage@2503dbd3
Loading Image From cache : image.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The proxy can control access to the real object, adding security.&lt;/li&gt;
&lt;li&gt;The real object is shielded from direct exposure to the system.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The code can become more complicated, as you need to introduce additional classes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The proxy pattern regulates access to objects by serving as an intermediary. It adds an abstraction layer that can be applied to access control, logging, and other things. The Proxy Pattern can dramatically improve the architecture and functionality of software systems when implemented wisely.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Design Patterns: Adapter</title>
      <dc:creator>Iyanu Falaye</dc:creator>
      <pubDate>Sat, 11 Nov 2023 23:03:42 +0000</pubDate>
      <link>https://dev.to/falay/design-patterns-adapter-1om1</link>
      <guid>https://dev.to/falay/design-patterns-adapter-1om1</guid>
      <description>&lt;p&gt;It is a structural pattern that allows objects with incompatible interfaces to work together. It helps by converting a class’ interface to a target. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Motivation&lt;/strong&gt;&lt;br&gt;
Where there’s a need to integrate classes with incompatible interfaces.&lt;br&gt;
To reuse an existing code that does not align with a current system’s interface.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Considering there is an existing payment system that supports cards, with the increase in the need for digital wallets, we want to provide support for payment from wallet in our payment system. instead of rewriting the whole system to fit this new mode of payment, we can make use of the adapter pattern to integrate the wallet mode seamlessly.&lt;/p&gt;

&lt;p&gt;This is the existing &lt;em&gt;PaymentProcessor&lt;/em&gt; interface that handles the card payment. it defines the &lt;em&gt;processPayment&lt;/em&gt; method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface PaymentProcessor {
    void processPayment();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the proposed Digital Wallet interface that different wallets can implement e.g. binance, coinbase, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface DigitalWallet {
    void makePaymentFromWallet();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using Binance as an example;&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 BinanceWallet implements DigitalWallet{
    @Override
    public void makePaymentFromWallet() {
        System.out.println("making payment from my binance wallet");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Adapter wraps &lt;em&gt;DigitalWallet&lt;/em&gt; and provides an implementation that is compatible with &lt;em&gt;PaymentProcessor&lt;/em&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 DigitalWalletAdapter implements PaymentProcessor {
    private DigitalWallet binanceWallet;

    public DigitalWalletAdapter(DigitalWallet binanceWallet) {
        this.binanceWallet = binanceWallet;
    }

    @Override
    public void processPayment() {
        binanceWallet.makePaymentFromWallet();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Single Responsibility: you can separate the interface and the code that converts from the logic of the program.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open-Closed principle: you can keep adding adapters without breaking or modifying the existing code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Complexity increases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;RecylerView Adapter in Android&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
When a system needs to evolve, the need for adapter pattern cannot be overstated to make it easier to integrate new functionalities with existing components. It ensures that a system is resilient and adaptable when requirements change.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
