<?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: Varun</title>
    <description>The latest articles on DEV Community by Varun (@varun21vaidya).</description>
    <link>https://dev.to/varun21vaidya</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%2F999903%2F23281cbc-9512-4555-b134-d191a2babf23.png</url>
      <title>DEV Community: Varun</title>
      <link>https://dev.to/varun21vaidya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/varun21vaidya"/>
    <language>en</language>
    <item>
      <title>Singleton Design Pattern</title>
      <dc:creator>Varun</dc:creator>
      <pubDate>Thu, 16 May 2024 10:37:06 +0000</pubDate>
      <link>https://dev.to/varun21vaidya/singleton-design-pattern-4bc0</link>
      <guid>https://dev.to/varun21vaidya/singleton-design-pattern-4bc0</guid>
      <description>&lt;p&gt;There are many scenarios where we need to restrict the object creation to 1. So, ensure that a class has only one instance and provide a global point of access to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Restrict instantiation of a class to a single object and provide a global access point to that instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, Real-life Application Examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Logger:&lt;/strong&gt; Ensuring only one logger instance exists across the application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DB Connection&lt;/strong&gt; : It requires only 1 object for db connection&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration Settings:&lt;/strong&gt; Having a single object to manage configuration settings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. There are several variations of implementing the Singleton pattern in Java, each with its own characteristics and considerations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Eager Initialization:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In this approach, the singleton instance is created eagerly, i.e., at the time of class loading.&lt;/li&gt;
&lt;li&gt;It guarantees thread safety since the instance is created before any thread can access it.&lt;/li&gt;
&lt;li&gt;static associates with class, which means the class cannot be reinitialized and all instances of class share same static variable&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;getInstance() Method exposed as public will return same object also static so only 1 object is returned.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// Private constructor&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

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

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lazy Initialization :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In this approach, the singleton instance is created only when needed, i.e., on the first call to &lt;code&gt;getInstance()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now when instance is equal to null then only new object is created.
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// Private constructor&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;:  But It is not thread-safe, meaning multiple threads can potentially create multiple instances if called simultaneously.&lt;br&gt;
Suppose there 2 threads coming at same time they both will check that its null and create 2 objects.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Synchronized Method:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;This approach improves performance by avoiding synchronization after the singleton instance is created.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Synchronized puts lock when one thread comes and creates instance it locks for other threads.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// Private constructor&lt;/span&gt;

      &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Its very expensive when 1000 threads come, 1st will lock create, 2 will lock, 3rd lock every time it will lock.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Double Locking&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It uses double-checked locking to ensure thread safety, but it's susceptible to the "Double-Checked Locking Problem" in Java versions prior to Java 5.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Working&lt;/strong&gt;: When 2 threads come at same time in initially as null, so they both enter into if condition, because of synchronized only t1 will be allowed then it checks object is null and creates the object now t2 comes it denies that object==null and goes out and returns the object.&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// Private constructor&lt;/span&gt;

            &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                            &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                        &lt;span class="o"&gt;}&lt;/span&gt;
                    &lt;span class="o"&gt;}&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Problems&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reordering of instruction:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Suppose we are creating new variable with member variable value,  like&lt;br&gt;
 instance = new Singleton(10);&lt;br&gt;
 so during this the process are &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;allocation of memory&lt;/li&gt;
&lt;li&gt;Initialize Member variable if not available&lt;/li&gt;
&lt;li&gt;assign reference of memory to object&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;now for performance improvement CPU reorders these steps, so it can assign the reference of memory before initializing, and it will be assigned some random compiler value.&lt;br&gt;
        Now even before initializing variable with value 10, the object will be pointed to memory pointer, and for 2nd thread when it comes in 1st if for object==null it will be false as its got some random value. and it will return same object.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Memory Issue: Suppose 1 thread has started the process to allocate memory and creation of object but at same time thread 2 accessed by another core.&lt;br&gt;
    Now look this structure.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    | Core1 | Core2 |
    | --- | --- |
    | Cache1 | Cache2 |
    | Common Memory Common Memory |
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But when thread 2 accesses the core, both the cache was not synced so it will check for obj==null which will be true and so it will ask to memory which was not synced by cache, and will create another Object.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using Volatile:&lt;/p&gt;

&lt;p&gt;using the &lt;strong&gt;&lt;code&gt;volatile&lt;/code&gt;&lt;/strong&gt; keyword is a solution to address some of the potential issues associated with the double-checked locking pattern. By declaring the reference to the singleton instance as &lt;strong&gt;&lt;code&gt;volatile&lt;/code&gt;&lt;/strong&gt;, you ensure that changes to the instance are immediately visible to all threads. This helps prevent the visibility problem that can occur due to optimizations performed by the Java Virtual Machine (JVM) and CPU.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;javaCopy&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Declare as volatile&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// Private constructor&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

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



&lt;p&gt;With the &lt;strong&gt;&lt;code&gt;volatile&lt;/code&gt;&lt;/strong&gt; keyword, changes made to the &lt;strong&gt;&lt;code&gt;instance&lt;/code&gt;&lt;/strong&gt; variable are immediately visible to other threads, ensuring that they see the fully initialized singleton instance. This helps prevent the possibility of a partially constructed instance being visible to other threads.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Lazy Initialization with Static Inner Class (Thread-Safe, Post-Java 5):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This approach provides thread safety with lazy initialization and is guaranteed to work in all versions of Java.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;It leverages the behavior of static inner classes to ensure lazy loading and thread safety.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// Private constructor&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SingletonHolder&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="no"&gt;INSTANCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SingletonHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;INSTANCE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

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

&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each approach has its trade-offs in terms of thread safety, performance, and compatibility. Choosing the appropriate Singleton implementation depends on the specific requirements of your application.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>oop</category>
      <category>solidprinciples</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Easiest explanations for SOLID Principles</title>
      <dc:creator>Varun</dc:creator>
      <pubDate>Wed, 08 May 2024 07:31:31 +0000</pubDate>
      <link>https://dev.to/varun21vaidya/easiest-explanations-for-solid-principles-4pn2</link>
      <guid>https://dev.to/varun21vaidya/easiest-explanations-for-solid-principles-4pn2</guid>
      <description>&lt;p&gt;The SOLID principles are a set of guidelines for writing software that is more understandable, flexible, and maintainable. The principles conceptualized by Robert C. Martin have become foundational in object-oriented design and programming. Each letter in "SOLID" stands for a different principle:&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Single Responsibility Principle (SRP)&lt;/strong&gt;:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A class should have only one reason to change, meaning it should have only one job or responsibility.&lt;/li&gt;
&lt;li&gt;Basically, A class should not contain an excessive number of methods that handle disparate concerns or responsibilities. Ideally, each class should encapsulate a single responsibility, ensuring that all its methods are cohesive and related to that responsibility. If multiple responsibilities emerge, consider refactoring them into separate classes.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;WHY&lt;/strong&gt;: This principle aims to reduce the complexity of the class by limiting its responsibilities. It helps in minimizing the impact of changes by isolating them to specific components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Earlier Problem&lt;/strong&gt;: A class that handles both user data management and user notification responsibilities. Now, if we need to fix one feature rest of the code can be prone to bugs. So, its recommended to have one responsibility.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to save the user&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to send an email&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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


&lt;p&gt;&lt;strong&gt;Solution it provides&lt;/strong&gt;: Split the class into two distinct classes, each handling a single responsibility.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserDataManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to save the user&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to send an email&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Open/Closed Principle (OCP)&lt;/strong&gt;:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
Suppose, a class with methods is developed, tested and live then why would you modify it, you should extend it and add methods related to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modifying Existing class to add new methods is prone to bug.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;WHY&lt;/strong&gt;: This principle promotes the use of interfaces and abstract classes to enable the behavior of a module to be extended without modifying the source code of the module itself. This is useful for maintaining code health over time, particularly when changes are required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Earlier Problem&lt;/strong&gt;: A class that needs modification every time a new filter condition needs to be added for a product.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProductFilter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;filterByColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;filterBySize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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


&lt;p&gt;&lt;strong&gt;Solution it provides&lt;/strong&gt;: Use a specification pattern that allows for extending filter capabilities without modifying existing code. ie create interface of that class and with new classes implement or extend original class for standard method and for new methods separate new classes for each.&lt;br&gt;
So, in future when you want other new feature, create another class which will extend original class with new feature.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;isSatisfied&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ColorSpecification&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="nf"&gt;isSatisfied&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;product&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SizeSpecification&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="nf"&gt;isSatisfied&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;product&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProductFilter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isSatisfied&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Liskov Substitution Principle (LSP)&lt;/strong&gt;:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program.
If Class B is subclass of Class A then we should be able to replace object of A with B without breaking behavior of our program.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;WHY&lt;/strong&gt;: This principle ensures that a subclass can stand in for its superclass without errors or unexpected behavior, promoting the reliability and reusability of components. Subclass should extend capability of parent class, should not narrow it down.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Earlier Problem&lt;/strong&gt;: A subclass that throws an exception for a method inherited from its superclass, which is not expected behavior.&lt;br&gt;
If a program is using class A now B is subclass of A with different implementation of methods extended from A. Then program should be able to replace A with B. Yes, B can have different outputs but it should not remove existing methods for B, ie raise errors for them / breaking the code.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cannot fly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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


&lt;p&gt;&lt;strong&gt;Solution it provides&lt;/strong&gt;: Restructure the class hierarchy to ensure that subclasses can fully substitute their superclasses.&lt;br&gt;
Here Penguin will break the code for implementing the fly method which its extending from Bird Class.&lt;br&gt;
So, make parent class such that it has only common methods, and for additional methods create another subclass with the features required for other classes.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

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

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Interface Segregation Principle (ISP)&lt;/strong&gt;:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: No client should be forced to depend on methods it does not use.
Interfaces should be such that the client should not implement unnecessary functions that they don't even need.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;WHY&lt;/strong&gt;: This principle encourages creating more specific interfaces rather than a general-purpose, do-it-all interface. It promotes smaller, more focused interfaces that better cater to specific clients, reducing the dependencies between clients and their classes.&lt;/p&gt;

&lt;p&gt;Interface is used for general template which will be used by specific classes based on their features.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Earlier Problem&lt;/strong&gt;: A bulky interface that forces implementations to implement methods they don't use.&lt;br&gt;
Generic interface should not implement any specific methods else those classes that extend this interface would need to implement these methods unnecessarily.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Machine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;fax&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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


&lt;p&gt;&lt;strong&gt;Solution it provides&lt;/strong&gt;: Break the interface into more specific parts. Different interfaces for different methods and these interfaces can be used as base templates for future classes.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;Printer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;Scanner&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;FaxMachine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fax&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AllInOnePrinter&lt;/span&gt; &lt;span class="n"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FaxMachine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;/*&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="o"&gt;*/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;/*&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;scan&lt;/span&gt; &lt;span class="o"&gt;*/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;fax&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;/*&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;fax&lt;/span&gt; &lt;span class="o"&gt;*/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Dependency Inversion Principle (DIP)&lt;/strong&gt;:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces). Abstractions should not depend on details. Details (concrete implementations) should depend on abstractions.&lt;/p&gt;

&lt;p&gt;Basically, class should depend on interfaces rather than concrete classes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;WHY&lt;/strong&gt;: By depending on abstractions rather than concrete classes, this principle helps to decouple the high-level components from the low-level components, making the system easier to manage and modify.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Earlier Problem&lt;/strong&gt;: High-level modules directly depend on low-level modules.&lt;br&gt;
Suppose interfaces have defined certain classes, now a class wants to implement and use these classes/methods. Should it directly use these classes/methods? No.&lt;br&gt;
Instead of interface if we use concrete classes/methods directly then we won't be able to modify or extend it based on needs.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LightBulb&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Light bulb turned on&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;turnOff&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Light bulb turned off&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Switch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;bulb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;LightBulb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LightBulb&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;press&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bulb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;turnOff&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bulb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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


&lt;p&gt;&lt;strong&gt;Solution it provides&lt;/strong&gt;: Use abstractions to decouple high-level modules from low-level modules.&lt;br&gt;
The class should implement the interface and from that it should decouple interface from specific methods. Create interface objects and using constructor injection we can use classes of that interface for implementation.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Switchable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;turnOff&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LightBulb&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Switchable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Light bulb turned on&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;turnOff&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Light bulb turned off&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Switch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;device&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Switchable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="nf"&gt;press&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;device&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;turnOff&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;device&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following the SOLID principles helps in creating software that is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scalable&lt;/strong&gt;: Easier to scale the software with fewer issues as new requirements arise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Manageable&lt;/strong&gt;: Simpler to manage as the software complexity grows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusable&lt;/strong&gt;: Increases the reusability of code through cleaner, more organized structures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extensible&lt;/strong&gt;: Facilitates the extension of software functionalities without a significant rework.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testable&lt;/strong&gt;: Improves testability due to reduced dependencies and better separation of concerns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we will go through real life application integrated with SOLID Principles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Web Application: User Account Management
&lt;/h3&gt;

&lt;h3&gt;
  
  
  1. Single Responsibility Principle (SRP)
&lt;/h3&gt;

&lt;p&gt;We'll create separate classes for handling user data management and user authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// UserDataManager.ts&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserDataManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to save user data&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;deleteUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to delete user data&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// UserAuthenticator.ts&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserAuthenticator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;authenticateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to authenticate user&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// or false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Open/Closed Principle (OCP)
&lt;/h3&gt;

&lt;p&gt;We'll create an interface for authentication strategies, allowing us to add new authentication methods without modifying existing code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// AuthenticationStrategy.ts&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;AuthenticationStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// UsernamePasswordStrategy.ts&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UsernamePasswordStrategy&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;AuthenticationStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to authenticate using username and password&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// or false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// OAuthStrategy.ts&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OAuthStrategy&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;AuthenticationStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic to authenticate using OAuth&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// or false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Liskov Substitution Principle (LSP)
&lt;/h3&gt;

&lt;p&gt;Our authentication strategies can be used interchangeably without affecting the correctness of the authentication process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;usernamePasswordStrategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AuthenticationStrategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UsernamePasswordStrategy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oauthStrategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AuthenticationStrategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OAuthStrategy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;usernamePasswordStrategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;oauthStrategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Interface Segregation Principle (ISP)
&lt;/h3&gt;

&lt;p&gt;We'll create separate interfaces for user data management and authentication to avoid implementing unnecessary methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// UserDataManagement.ts&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;UserDataManagement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;deleteUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// UserAuthentication.ts&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;UserAuthentication&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;authenticateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Dependency Inversion Principle (DIP)
&lt;/h3&gt;

&lt;p&gt;Our high-level modules will depend on abstractions rather than concrete implementations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// UserAccountController.ts&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserAccountController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;userDataManager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserDataManagement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;userAuthenticator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserAuthentication&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userDataManager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserDataManagement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userAuthenticator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserAuthentication&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userDataManager&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userDataManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAuthenticator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userAuthenticator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;registerUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userDataManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;loginUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAuthenticator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation of SOLID Principles Integration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SRP&lt;/strong&gt;: &lt;code&gt;UserDataManager&lt;/code&gt; is responsible for managing user data, while &lt;code&gt;UserAuthenticator&lt;/code&gt; handles user authentication. Each class has a single responsibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OCP&lt;/strong&gt;: By introducing the &lt;code&gt;AuthenticationStrategy&lt;/code&gt; interface, we can add new authentication methods like OAuth without modifying existing authentication logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LSP&lt;/strong&gt;: The &lt;code&gt;UsernamePasswordStrategy&lt;/code&gt; and &lt;code&gt;OAuthStrategy&lt;/code&gt; can be used interchangeably, fulfilling the same contract of the &lt;code&gt;AuthenticationStrategy&lt;/code&gt; interface.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ISP&lt;/strong&gt;: We split interfaces &lt;code&gt;UserDataManagement&lt;/code&gt; and &lt;code&gt;UserAuthentication&lt;/code&gt; into smaller, focused interfaces to avoid implementing unnecessary methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DIP&lt;/strong&gt;: The &lt;code&gt;UserAccountController&lt;/code&gt; depends on abstractions (&lt;code&gt;UserDataManagement&lt;/code&gt; and &lt;code&gt;UserAuthentication&lt;/code&gt; interfaces) rather than concrete implementations, allowing for easy substitution and testing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, by using these principles, our web application becomes more modular, easier to maintain, and less prone to bugs when adding new features or changing existing ones. Each component is highly cohesive and loosely coupled, promoting flexibility and scalability.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Angular Crash Course in One Shot🚀🔥</title>
      <dc:creator>Varun</dc:creator>
      <pubDate>Mon, 31 Jul 2023 09:55:25 +0000</pubDate>
      <link>https://dev.to/varun21vaidya/angular-crash-course-for-your-next-interview-4jpj</link>
      <guid>https://dev.to/varun21vaidya/angular-crash-course-for-your-next-interview-4jpj</guid>
      <description>&lt;p&gt;Hi Guys👋, Today We will go through Angular for an Experienced DEV. This will be useful to go through after learning everything or if you are preparing for an Interview. So lets Start 🚀🔥&lt;/p&gt;

&lt;h2&gt;
  
  
  Components in ANGULAR Folder :
&lt;/h2&gt;

&lt;p&gt;package.json: &lt;/p&gt;

&lt;p&gt;It contains basic info about project.&lt;br&gt;
Required for every project&lt;br&gt;
records imp metadata about project, such as name, description, script, dependencies.&lt;/p&gt;

&lt;p&gt;package.lock.json:&lt;/p&gt;

&lt;p&gt;automatically generates for operations with chanage in node_modules&lt;br&gt;
allows to install same dependencies for future devs&lt;br&gt;
contains name, dependencies, locked version of the project&lt;/p&gt;
&lt;h2&gt;
  
  
  ROUTE GUARD:
&lt;/h2&gt;

&lt;p&gt;Angular route guards can be used to control whether the user can navigate to or away from the given route based on some condition.&lt;br&gt;
Used to secure route or perform some operation before navigating to a route or leaving the route.&lt;/p&gt;

&lt;p&gt;e.g where can we use the route guards? &lt;br&gt;
suppose we are using the application to buy some stuff, but we havent logged in. Now when a user clicks on buy button, he should be redirected to checkout page only if &lt;br&gt;
he is a logged in. otherwise first he should be sent to login, and then only to checout page., so here route guard is helpful&lt;/p&gt;

&lt;p&gt;route gaurd can also be used to &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Confirm the navigation operation&lt;/li&gt;
&lt;li&gt;allow  access to certain parts of application to specific users.&lt;/li&gt;
&lt;li&gt;validating certain parameters before navigating to the root.&lt;/li&gt;
&lt;li&gt;fetch data before showing component view&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Types of Route Guards:&lt;/p&gt;

&lt;p&gt;CanActivate:&lt;br&gt;
CanDeactivate:&lt;br&gt;
Resolve:&lt;br&gt;
CanLoad:&lt;br&gt;
CanActivateChild:&lt;/p&gt;

&lt;p&gt;Process: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a route guard as service&lt;/li&gt;
&lt;li&gt;The Service class should implement route guard interface.&lt;/li&gt;
&lt;li&gt;Take interface and provide the method inside the service class&lt;/li&gt;
&lt;li&gt;from this method we need to return True or False.&lt;/li&gt;
&lt;li&gt;if true navigation process will continue else it will stop.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cmd: ng g g guardname

@Injectable()
export class StuffRouteService implements CanActivate{

    // this is service class which implements CanActivate Interface 
    // and below is method to be provided

    canActivate()
       { 
        //check if route can be activated
     if (condition)
        { return true;}

     else { return false}

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

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Use route guard on the route which required to be guard in routers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;eg. {path: 'stuff', component: BuyStuffComponent, canActivate: [StufffRouteService] }&lt;/p&gt;
&lt;h2&gt;
  
  
  DATA BINDING
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Interpolation Binding: 
data is being taken from compoenent to HTML view.
Allows user to bind values to user interface
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    comoponent: 
    public data= "this is the req data"

    HTML: 
        `&amp;lt;h1&amp;gt;{{data}}&amp;lt;\h1&amp;gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Property Binding:&lt;br&gt;
one way data binding, sets property for HTML elements&lt;br&gt;
updating property value in component for HTML element&lt;/p&gt;

&lt;p&gt;eg. public image = "/assets/Logo.png"&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Event Binding: &lt;br&gt;
one way data binding, opposite of property binding&lt;br&gt;
when user clicks a button, event bind to it inform of method&lt;br&gt;
in component gets a call.&lt;br&gt;
so data flows from view to component&lt;/p&gt;

&lt;p&gt;eg. Subscribe&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Two Way Binding:&lt;br&gt;
data flows from compoenent to view and back.&lt;br&gt;
this binding ensures changes made in app, gets reflected&lt;br&gt;
immediately on both ends. general syntax: [()]&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    component:
    public data= ""

    HTML:
    &amp;lt;input [(ngModel)]="data" type="text"]&amp;gt;

    {{data}}

&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;so here data is defined in .ts file and input is taken and it is
stored as data, and again data is interpolated in view.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Single Page Applications:
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;applications which loads only once and to display new features,
new page does not need to be loaded completely, instead with the
DOM manipulation of JS, content is generated dynamically, on the 
existing page itself.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Decorators:
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;design pattern that define how an angular feature works. They make 
prior modifications to class, service.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Class Decorators: &lt;br&gt;
    this tells to angular if perticular class is component or module. &lt;br&gt;
    eg @Component and @NgModule&lt;/p&gt;

&lt;p&gt;Property Decorators: &lt;br&gt;
    allows to decorate some properties within our classes&lt;br&gt;
    eg: @Input, @Output, @Override&lt;br&gt;
    for @Input angular compiler will create an input binding with the &lt;br&gt;
    property name and link it&lt;/p&gt;

&lt;p&gt;Method Decorators:&lt;br&gt;
    Method decorators are used to decorate the method defined in class with functionality, eg: @HostListener&lt;/p&gt;

&lt;p&gt;Parameter Decorators:&lt;br&gt;
    Applied to constructor parameter of the class and &lt;br&gt;
    used generally to inject a provider in constructor.&lt;br&gt;
    eg.&lt;br&gt;&lt;br&gt;
    export class ParameterExample{&lt;br&gt;
        constructor(@Inject(ExampleService) exampServ)&lt;br&gt;
        console.log(exampServ);}&lt;/p&gt;
&lt;h2&gt;
  
  
  Dependency injection:
&lt;/h2&gt;

&lt;p&gt;Components dependent on other components can be easily worked around using this feature. &lt;/p&gt;
&lt;h2&gt;
  
  
  Angular Forms:
&lt;/h2&gt;

&lt;p&gt;to build angular forms, import forms module&lt;br&gt;
and create a form in HTML and define #formID = "ngForm" and create a submit event (ngSubmit)="getData()"&lt;br&gt;
and define getData method in .ts file.&lt;/p&gt;

&lt;p&gt;and at the end make a register button.&lt;/p&gt;

&lt;p&gt;SO this is basic structure of form.&lt;/p&gt;
&lt;h2&gt;
  
  
  Pipes:
&lt;/h2&gt;

&lt;p&gt;pipes are used to transform values from one type to other&lt;br&gt;
eg, we want to change lowercase to uppercase, change date format, change currency format etc&lt;/p&gt;

&lt;p&gt;eg.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;{{title | uppercase}}&amp;lt;/h1&amp;gt;

&amp;lt;h1&amp;gt;{{today | date: 'fullDate'}}&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now these operations can be made by creating functions and calling those here, but it takes times&lt;br&gt;
so we use pipes to save time and make code simpler.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Slicing:&lt;/strong&gt;&lt;br&gt;
if title= 'ANGULAR TUTORIAL'&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;h1&amp;gt;{{title | slice: 3: 6 }}&amp;lt;/h1&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;it will show, 'ULA'&lt;/p&gt;

&lt;p&gt;JSON pipe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user= {name: 'Raj'}

&amp;lt;h1&amp;gt;{{user | json}}&amp;lt;/h1&amp;gt; 

output will show: {name: 'Raj'}

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

&lt;/div&gt;



&lt;p&gt;Decimal Pipe:&lt;br&gt;
&lt;code&gt;&amp;lt;h1&amp;gt;{{2000.30 | number: 2.3-4}}&amp;lt;/h1&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;max digits will shown&lt;br&gt;
before decimal 2 ie if there was 002000 it will remove first zeros&lt;br&gt;
after decimal min 3 max 4&lt;/p&gt;

&lt;p&gt;&lt;code&gt;op 2000.300&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;currency pipe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;h1&amp;gt;{{50 | currency: 'USD' }}&amp;lt;/h1&amp;gt;

op: $50.00

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

&lt;/div&gt;



&lt;p&gt;Create Custom PIPE:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng g p pipename&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we can give arguments to custom Pipe, like we have given in currency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html:
`&amp;lt;h1&amp;gt;{{50 | newpipe: 100 }}&amp;lt;/h1&amp;gt;`

pipe file:

transform (value: number, ...args: number[]):
unknown{
    let x = args;
    return value* x[0]
    //here in arguments we can also add multiple arguments 
}

op: 5000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Angular Directives:
&lt;/h2&gt;

&lt;p&gt;First Of all Angular has 3 type of Directives&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Structural Directives&lt;/li&gt;
&lt;li&gt;Component Directives&lt;/li&gt;
&lt;li&gt;Attribute Directives&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  1. Structural Directives:
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. NgIf
2. NgFor
3. NgSwitch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  2. Component Directives:
&lt;/h3&gt;

&lt;p&gt;These are most common and widely used Directives. components are like main builiding block of an angular project comoponents are used with templates, which consists of &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HTML template that declared what need to be rendered on the page&lt;/li&gt;
&lt;li&gt;a typescript class which defines the behaviour of elements and methods&lt;/li&gt;
&lt;li&gt;CSS selector which defines how the component is used in the template&lt;/li&gt;
&lt;li&gt;and optionally, css styles are used on the template.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  3. Attribute Directives:
&lt;/h3&gt;

&lt;p&gt;These Directives changes appearances of elements, comoponents and other Directives.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;NgClass - adds and removes set of css classes&lt;/li&gt;
&lt;li&gt;NgStyle - adds and removes HTML styles&lt;/li&gt;
&lt;li&gt;ngModel - adds two way databiding to HTML form element&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;so these Directives listen and modify behaviour of HTML elements, Attributes and properties.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;NgClass:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;for single style we can use class binding&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;h1 [style.background]="white"&amp;gt; element &amp;lt;/h1&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
otherwise for multiple styles to be added use a style class and assign it to NgClass&lt;br&gt;
&lt;code&gt;&amp;lt;h1 [ngClass]= "logged" ? 'loggedstyles' : '' &amp;gt; element &amp;lt;/h1&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will apply loggedstyles to h1 element if the user is logged in else nothing Applied&lt;/p&gt;

&lt;p&gt;FOR METHODS:&lt;/p&gt;

&lt;p&gt;in the comoponent file:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;applyClasses= Record&amp;lt;string, boolean&amp;gt;= {}                       

setapplyClasses(){ 
// this will apply css classes based on the boolean values 
// if the boolean value is True, class will be applied 
// else if its False not applied

this.applyClasses= {
    save: this.canSave,
    update: !this.notChanged,
       };
}   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In HTML:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div [ngClass]= "applyClasses"&amp;gt; save or update &amp;lt;/div&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Note:&lt;br&gt;
In example Angular applies classes on initialization and in case of changes. the example calls the setapplyClasses initially with ngOnInit(), and when properties changes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;NgStyle:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With NgStyle we can define multiple inline styles simultaneously, with state of the component add method to component class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;applyStyles= Record&amp;lt;string, boolean&amp;gt;= {}                        

setapplyStyles(){

// this will apply css style based on the boolean values 
// if the boolean value is True, first style will be applied 
// else if its False other style will be applied

this.applyStyles= {
    'font-weight': this.canSave ? 'bold': 'normal',
    'font-style': !this.logged ? 'italic' : 'normal'
    };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In HTML:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div [ngStyle]= "applyStyles"&amp;gt; Change Styles &amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note:&lt;br&gt;
In example Angular applies styles on initialization and in case of changes, the example calls the setapplyStyles initially with ngOnInit(), and when properties changes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;NgModel:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;NgModel directive is used to disply a data property and update that property when the user makes changes.&lt;br&gt;
first of all include FormsModule in app.module.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="example"&amp;gt;[(ngModel)]:&amp;lt;/label&amp;gt;
&amp;lt;input [(ngModel)]="currentItem.name" id="example"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for Now🌱. Hope this helps in your Web Dev Journey, Happy Learning 🚀!!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>angular</category>
      <category>programming</category>
      <category>interview</category>
    </item>
    <item>
      <title>MEAN Stack Project: Create Quick File Share Application: Part 2 Backend</title>
      <dc:creator>Varun</dc:creator>
      <pubDate>Fri, 06 Jan 2023 17:15:37 +0000</pubDate>
      <link>https://dev.to/varun21vaidya/mean-stack-project-create-quick-file-share-application-part-2-backend-22c3</link>
      <guid>https://dev.to/varun21vaidya/mean-stack-project-create-quick-file-share-application-part-2-backend-22c3</guid>
      <description>&lt;p&gt;This is continuation of the File Share Application using MEAN Stack.&lt;br&gt;
If you have not read it, its Important to understand the structure:&lt;br&gt;
&lt;a href="https://dev.to/varun21vaidya/mean-stack-project-create-quick-file-share-application-878"&gt;https://dev.to/varun21vaidya/mean-stack-project-create-quick-file-share-application-878&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we will use NodeJS and ExpressJS on server side.&lt;/p&gt;

&lt;p&gt;lets start by running this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be our Project Structure:&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%2Fuznyw5ei3bg5tz9kfpz3.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%2Fuznyw5ei3bg5tz9kfpz3.png" alt="Image description" width="681" height="632"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now first install all the required dependencies, we will see their use along the way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i cors dotenv ejs express mongoose multer nodemon uuid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Start with &lt;strong&gt;Server.js&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;const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const path = require('path');

app.use(express.json());

app.listen(PORT, console.log(`Listening on port ${PORT}.`));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;follow this to create your Mongo Atlas Cluster:&lt;br&gt;
&lt;a href="https://www.mongodb.com/docs/drivers/node/current/quick-start/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/drivers/node/current/quick-start/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and Put the MONGO_CONNECTION_URL in .env file.&lt;/p&gt;

&lt;p&gt;and now create &lt;strong&gt;db.js&lt;/strong&gt; in config folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
require("dotenv").config();
const mongoose = require("mongoose");
function connectDB() {
    mongoose
        .connect(process.env.MONGO_CONNECTION_URL, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        })
        .then(() =&amp;gt; console.log('DB connection successful!')).catch((e) =&amp;gt; {
            console.log('error while connencting database');
            console.log(e);
        });
}

// mIAY0a6u1ByJsWWZ
module.exports = connectDB;

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

&lt;/div&gt;



&lt;p&gt;now use this db in &lt;strong&gt;server.js&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;const dotenv = require('dotenv')
dotenv.config({ path: './.env' })
const connectDB = require('./config/db');
connectDB();

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

&lt;/div&gt;



&lt;p&gt;Now we need to create a Schema for our filesystem, which will contain&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;filename&lt;/li&gt;
&lt;li&gt;path: file stored location&lt;/li&gt;
&lt;li&gt;size: filesize that should be shown on view&lt;/li&gt;
&lt;li&gt;uuid: encrypted file name&lt;/li&gt;
&lt;li&gt;sender&lt;/li&gt;
&lt;li&gt;reciever&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;file.js&lt;/strong&gt; in Models Folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const fileSchema = new Schema({
    filename: { type: String, required: true },
    path: { type: String, required: true },
    size: { type: Number, required: true },
    uuid: { type: String, required: true },
    sender: { type: String, required: false },
    receiver: { type: String, required: false },
}, { timestamps: true });

module.exports = mongoose.model('File', fileSchema);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in the routes folder, create *&lt;em&gt;files.js&lt;br&gt;
*&lt;/em&gt; &lt;br&gt;
this will be controller, and will contain post method to upload the file.&lt;/p&gt;

&lt;p&gt;Now we are going to use uuid which stores unique uuid for file to store in database.&lt;br&gt;
Because uuid is so unique that no one with one download link would be able to change the id and download other previous documents.&lt;br&gt;
Otherwise if it was just 1,2,3.. others could be able to download by entering previous index.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const { v4: uuidv4 } = require('uuid');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now to store and upload the file we will use multer.&lt;br&gt;
Multer is a node.js middleware for handling multipart/form-data , which is primarily used for uploading files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// diskStorage Returns a StorageEngine implementation configured
// to store files on the local file system.
let storage = multer.diskStorage({

    // destination stores file at given folder
    destination: (req, file, cb) =&amp;gt; cb(null, 'uploads/'),
    // gives filename
    filename: (req, file, cb) =&amp;gt; {

        // create a unique file name
        // path.extname takes extension name from file and saves it as same
        // 38974612841-129834729432.png
        const uniqueName = `${Date.now()}-${Math.round(Math.random() * 1E9)}${path.extname(file.originalname)}`;
        cb(null, uniqueName)
    },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First with multer we will set the file limits to be uploaded, which will handle uploading of file and store it in uploads section.&lt;br&gt;
Storage returns multer instance providing methods for generating middleware that process uploaded files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let upload = multer({ storage, limits: { fileSize: 1000000000 * 100 }, }).single('myfile'); //100mb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now its time to create POST request API.&lt;br&gt;
we will create post request and create an upload method, which will create and make db model through schema with the filename, created uuid, path and size and returns a downloadable link which will be fetched by the client side in Angular.&lt;br&gt;
which will look like this &lt;a href="http://localhost:3000/files/234dfhsebrg-2342dvsdvsdk" rel="noopener noreferrer"&gt;http://localhost:3000/files/234dfhsebrg-2342dvsdvsdk&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
router.post('/', (req, res) =&amp;gt; {
    upload(req, res, async (err) =&amp;gt; {
        if (err) {
            return res.status(500).send({ error: err.message });
        }
        const file = new File({
            filename: req.file.filename,
            // gets unique uuid 

            uuid: uuidv4(),

            // gets path from destination + filename recieved from multer
            path: req.file.path,
            size: req.file.size
        });

        // response will give download link

        // save this with await while upload is async
        const response = await file.save();
        res.json({ file: `${process.env.APP_BASE_URL}/files/${response.uuid}` });
        // download link will look like
        // http://localhost:3000/files/234dfhsebrg-2342dvsdvsdk

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

&lt;/div&gt;



&lt;p&gt;This is the full code for controller.&lt;br&gt;
&lt;strong&gt;file.js&lt;/strong&gt; in router.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = require('express').Router();
const multer = require('multer');

// path gives extension of the uploaded file
const path = require('path');
const File = require('../models/file');

// stores unique uuid for file to store in db

// WHY UUID: cz its so unique that no one with one download link
// would be able to change the id and download other previous documents
// if it was just 1,2,3.. others could be able to download by entering previous index
const { v4: uuidv4 } = require('uuid');


// diskStorage Returns a StorageEngine implementation configured
// to store files on the local file system.
let storage = multer.diskStorage({

    // destination stores file at given folder
    destination: (req, file, cb) =&amp;gt; cb(null, 'uploads/'),
    // gives filename
    filename: (req, file, cb) =&amp;gt; {

        // create a unique file name
        // path.extname takes extension name from file and saves it as same
        // 38974612841-129834729432.png
        const uniqueName = `${Date.now()}-${Math.round(Math.random() * 1E9)}${path.extname(file.originalname)}`;
        cb(null, uniqueName)
    },
});

// handle uploading of file and store it in uploads section with multer

// middleware for handling form-data, ie used for uploading files

// storage returns multer instance providing
// methods for generating middleware that process uploaded files
// storage: storage,
let upload = multer({ storage, limits: { fileSize: 1000000000 * 100 }, }).single('myfile'); //100mb

router.post('/', (req, res) =&amp;gt; {
    upload(req, res, async (err) =&amp;gt; {
        if (err) {
            return res.status(500).send({ error: err.message });
        }
        const file = new File({
            filename: req.file.filename,
            // gets unique uuid 

            uuid: uuidv4(),

            // gets path from destination + filename recieved from multer
            path: req.file.path,
            size: req.file.size
        });

        // response will give download link

        // save this with await while upload is async
        const response = await file.save();
        res.json({ file: `${process.env.APP_BASE_URL}/files/${response.uuid}` });
        // download link will look like
        // http://localhost:3000/files/234dfhsebrg-2342dvsdvsdk

    });
});
module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to create a server side rendering view to show the filesystem and download button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Show.js&lt;/strong&gt; in routes &lt;br&gt;
This will create a rendering view when the user takes download link after completing the upload button.&lt;br&gt;
Here we need to create GET API which will show the view  showing the filename, size, and a download button to download the actual file.&lt;br&gt;
IT will take uuid and find the file data in database.&lt;br&gt;
and if file is present will generate the view.&lt;/p&gt;

&lt;p&gt;This will also send the download link to the frontend to show when the user completes file upload.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// router
const router = require('express').Router()
// model import
const File = require('../models/file')

// function to try and get data from database with dynamic parameter uuid
// and render download page else in catch return err
router.get('/:uuid', async (req, res) =&amp;gt; {
    try {
        const file = await File.findOne({ uuid: req.params.uuid })

        // if file not present return error
        // in download section
        if (!file) {
            return res.render('download', { error: 'link has expired' })
        }

        // generate a download page with the details to download the file
        return res.render('download', {
            uuid: file.uuid,
            fileName: file.filename,
            fileSize: file.size,
            // download link
            downloadLink: `${process.env.APP_BASE_URL}/files/download/${file.uuid}`
        })
    }
    catch (err) {
        return res.render('download', { err: 'something went wrong' })
    }
});

module.exports = router;

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

&lt;/div&gt;



&lt;p&gt;But first create GET API to download the file by giving the uuid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;download.js&lt;/strong&gt; in routes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = require('express').Router();
const File = require('../models/file');


router.get('/:uuid', async (req, res) =&amp;gt; {
    const file = await File.findOne({ uuid: req.params.uuid })


    // if file not present return error
    // in download section
    if (!file) {
        return res.render('download', { error: 'link has expired' })
    }

    // to download file we need filepath
    // if you check database you will find each file has mentioned file.path
    const filePath = `${__dirname}/../${file.path}`;

    // downloading in express just require res.download(filepath)
    res.download(filePath);

})

module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have to create a view to show all the data and donwload button.We can create it with &lt;code&gt;ejs&lt;/code&gt; and &lt;code&gt;css&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;first create ejs in Views folder as shown in file structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Download Page&amp;lt;/title&amp;gt;
    &amp;lt;link href="../public/css/style.css" rel="stylesheet" type="text/css"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;section class="container"&amp;gt;
        &amp;lt;div class="drop-zone"&amp;gt;
            &amp;lt;!-- if there is error in link or uuid has expired or invalid
            show.js will give error and this error is printed here--&amp;gt;
            &amp;lt;% if(locals.error) { %&amp;gt;
                &amp;lt;h4&amp;gt;
                    &amp;lt;%= locals.error %&amp;gt;
                &amp;lt;/h4&amp;gt;
                &amp;lt;% } else { %&amp;gt;

                    &amp;lt;!-- else provide the link to download file --&amp;gt;
                    &amp;lt;div class="icon-container"&amp;gt;
                        &amp;lt;img src="../public/img/download-icon.png" alt="download-icon"&amp;gt;
                    &amp;lt;/div&amp;gt;
                    &amp;lt;div class="content"&amp;gt;
                        &amp;lt;h3&amp;gt;Your File is Ready to Download&amp;lt;/h3&amp;gt;
                        &amp;lt;p&amp;gt;Link Exprires in 24 Hours&amp;lt;/p&amp;gt;
                        &amp;lt;!-- file name and filesize are in show.js --&amp;gt;
                        &amp;lt;h4&amp;gt;
                            &amp;lt;%- fileName -%&amp;gt;
                        &amp;lt;/h4&amp;gt;
                        &amp;lt;!-- filesize is available in bytes so convert it using /1000
                and to remove decimal places use parseInt --&amp;gt;
                        &amp;lt;small&amp;gt;
                            &amp;lt;%- parseInt(fileSize/1000) -%&amp;gt;KB
                        &amp;lt;/small&amp;gt;
                        &amp;lt;div class="send-btn-container"&amp;gt;
                            &amp;lt;a href="&amp;lt;%- downloadLink %&amp;gt;"&amp;gt;Download File&amp;lt;/a&amp;gt;
                        &amp;lt;/div&amp;gt;
                    &amp;lt;/div&amp;gt;
                    &amp;lt;% } %&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/section&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And Inside the public folder, create css file, which will give styles to our ejs view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body,
html {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  background-color: rgb(189, 255, 255);

  height: 100%;
  width: auto;
  padding: 0;
  margin: 0;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  background-color: white;
  border-radius: 30px;
  box-shadow: 0px 15px 30px 5px rgba(0, 0, 0, 0.2);
}

.drop-zone {
  width: 300px;
  height: 300px;
  margin: 30px;
  border-radius: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.content {
  line-height: 30%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.send-btn-container {
  background-color: DodgerBlue;
  border: none;
  margin-top: 20px;
  padding: 12px 30px;
  cursor: pointer;
  border-radius: 20px;
}
.send-btn-container a {
  text-decoration: none;
  color: white;
}
.icon-container {
  width: 75px;
  height: 75px;
  position: relative;
}

.icon-container img {
  position: absolute;
  height: 60px;
  width: 60px;
}

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

&lt;/div&gt;



&lt;p&gt;Now we need to set ejs in our server.js file&lt;br&gt;
and also need to configure &lt;code&gt;cors&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So finally our server.js will have all the routes and cors and is ready to be tested.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const path = require('path');
const dotenv = require('dotenv')
dotenv.config({ path: './.env' })
const connectDB = require('./config/db');
connectDB();

var cors = require('cors');

// use it before all route definitions
// app.use(cors({ origin: ['https://share-now-backend-gi2ea360d-varun21vaidya.vercel.app/', 'https://share-now-file-sharing-app.vercel.app/', 'http://localhost:4200'] }));

// cors policy
app.use(cors({
    origin: ['https://share-now-backend.vercel.app/https://share-now-file-sharing-app.vercel.app/', 'https://share-now-backend.vercel.app/http://localhost:4200'], // use your actual domain name (or localhost), using * is not recommended
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'],
    allowedHeaders: ['Content-Type', 'Origin', 'X-Requested-With', 'Accept', 'x-client-key', 'x-client-token', 'x-client-secret', 'Authorization'],
    credentials: true
}))


app.use(express.json());

// setup static files
// using app.use to serve up static CSS files in public/css/ folder
//  when /public link is called in ejs files
// app.use("/route", express.static("foldername"));
app.use('/public', express.static('public'));

//template engine
app.set('views', path.join(__dirname, '/views'));
app.set('view engine', 'ejs');


// Routes 
// to upload file use this route
app.use('/api/files', require('./routes/files'));

// when file is uploaded it returns this route
app.use('/files', require('./routes/show'));


// to download file use this route
app.use('/files/download', require('./routes/download'));


app.listen(PORT, console.log(`Listening on port ${PORT}.`));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we will test our APIs in POSTMAN:&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%2Fnt3nubua66u07qo137lg.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%2Fnt3nubua66u07qo137lg.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So as you can see it will return a download link.&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%2Fyuyvtg9154e2wj24ws3u.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%2Fyuyvtg9154e2wj24ws3u.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So our Final output will look like this !! And we have completed our application.&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%2Fskfbpuq607nvthwlo52u.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%2Fskfbpuq607nvthwlo52u.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;run both frontend and backend to get working Application.&lt;br&gt;
as like this: &lt;a href="https://share-now-file-sharing-app.vercel.app/" rel="noopener noreferrer"&gt;https://share-now-file-sharing-app.vercel.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also get the code from github.&lt;/p&gt;

&lt;p&gt;Github link (Don't forget to leave a star🌟):&lt;br&gt;
Frontend Repo: &lt;a href="https://github.com/varun21vaidya/ShareNow" rel="noopener noreferrer"&gt;https://github.com/varun21vaidya/ShareNow&lt;/a&gt;&lt;br&gt;
Backend Repo: &lt;a href="https://github.com/varun21vaidya/ShareNow-Backend" rel="noopener noreferrer"&gt;https://github.com/varun21vaidya/ShareNow-Backend&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you Enjoyed and learned from this useful project !! Happy Coding !!&lt;/p&gt;

</description>
      <category>ai</category>
      <category>discuss</category>
    </item>
    <item>
      <title>MEAN Stack Project: Create Quick File Share Application</title>
      <dc:creator>Varun</dc:creator>
      <pubDate>Fri, 06 Jan 2023 15:24:05 +0000</pubDate>
      <link>https://dev.to/varun21vaidya/mean-stack-project-create-quick-file-share-application-878</link>
      <guid>https://dev.to/varun21vaidya/mean-stack-project-create-quick-file-share-application-878</guid>
      <description>&lt;p&gt;Hey, remember that day when you wanted to share that video or different format file but WhatsApp would reduce its quality or wouldn't support that format. Well we all have faced similar situations when we wanted to share file with our friend or family folder to cousins. But now we are going to make an application where we can directly send Any Type of File to any user and he can download it with just a link.&lt;/p&gt;

&lt;p&gt;So here we are going to use Angular 14 for Frontend, NodeJS for Backend, and MongoDB Atlas as Backend and we can even Host this project.&lt;/p&gt;

&lt;p&gt;so here I am giving Project Repo to go to if you face any problem&lt;br&gt;
just clone it in your folder by writing this in terminal&lt;br&gt;
&lt;code&gt;git clone repolink&lt;/code&gt;&lt;br&gt;
Frontend Repo: &lt;a href="https://github.com/varun21vaidya/ShareNow" rel="noopener noreferrer"&gt;https://github.com/varun21vaidya/ShareNow&lt;/a&gt;&lt;br&gt;
Backend Repo: &lt;a href="https://github.com/varun21vaidya/ShareNow-Backend" rel="noopener noreferrer"&gt;https://github.com/varun21vaidya/ShareNow-Backend&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets First Decide Features we want to build:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;User story: I can drag and drop an image to upload it&lt;/li&gt;
&lt;li&gt;User story: I can choose to select an image from my folder&lt;/li&gt;
&lt;li&gt;User story: When the image is uploaded, It shows that and Provides a Link to Download&lt;/li&gt;
&lt;li&gt;User story: I can choose to copy the Link to the clipboard&lt;/li&gt;
&lt;li&gt;User story: Download Page with Encoded File Name but Accurate File size to confirm our File&lt;/li&gt;
&lt;li&gt;User Story: Download button directly downloads the required File&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;ANGULAR Part:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng new frontend&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we have to create a project structure:&lt;br&gt;
which includes &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;component 'upload' which will be homepage&lt;/li&gt;
&lt;li&gt;component 'completed' which will be shown when we upload file&lt;/li&gt;
&lt;li&gt;'fileupload' service which will be used to upload file through backend api and get download link from backend.&lt;/li&gt;
&lt;/ol&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%2Fou06cb9flkfjhn0hvu5d.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%2Fou06cb9flkfjhn0hvu5d.png" alt="Image description" width="659" height="766"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng g c upload
ng g c completed
ng g s fileupload
ng g s sendurl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will also use bootstrap:&lt;br&gt;
so put this in index.html&lt;/p&gt;

&lt;p&gt;for header:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"&amp;gt;

&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;&amp;lt;body&amp;gt;
  &amp;lt;app-root&amp;gt;&amp;lt;/app-root&amp;gt;
  &amp;lt;script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;app.module will look 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;import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UploadComponent } from './upload/upload.component';
import { HttpClientModule } from '@angular/common/http';
import { CompletedComponent } from './completed/completed.component';
@NgModule({
  declarations: [
    AppComponent,
    UploadComponent,
    CompletedComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

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

&lt;/div&gt;



&lt;p&gt;Now we also need to decide the routes in app-routing.module.ts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CompletedComponent } from './completed/completed.component';
import { UploadComponent } from './upload/upload.component';

const routes: Routes = [
  { path: 'uploaded', component: CompletedComponent },
  { path: '', component: UploadComponent },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will make global styles.css:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* You can add global styles to this file, and also import other style files */
html,
body {
  height: 100%;
  background-color: rgb(189, 255, 255);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Create View for a home page component in &lt;strong&gt;upload.component.html&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;&amp;lt;div class="container"&amp;gt;
    &amp;lt;div class="upload-container"&amp;gt;
        &amp;lt;div class="headline py-3 px-5 mb-2 bg-primary text-white"&amp;gt;
            &amp;lt;h5&amp;gt;Share Your Files Superquickly&amp;lt;/h5&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="uploader"&amp;gt;
            &amp;lt;h3&amp;gt;Upload Your File&amp;lt;/h3&amp;gt;
            &amp;lt;!-- &amp;lt;p&amp;gt;File should be jpeg,png..&amp;lt;/p&amp;gt; --&amp;gt;
            &amp;lt;div class="drag-container"&amp;gt;

                &amp;lt;div class="dropdiv" [class.processing]="processing" (dragover)="onDrag($event)"
                    (drop)="onDrop($event)"&amp;gt;
                    &amp;lt;input type="file" (change)="onChange($event)" style="display:none"&amp;gt;
                    &amp;lt;img src="../../assets/files.jpg" alt="icon" class="files rounded mb-3 d-block"&amp;gt;
                    &amp;lt;p class="msg mx-2"&amp;gt;Drag and Drop your File here&amp;lt;/p&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;p&amp;gt;Or&amp;lt;/p&amp;gt;

            &amp;lt;div class="upload"&amp;gt;
                &amp;lt;input type="file" name="uploadfile" id="img" style="display:none;" (change)="selectFile($event)" /&amp;gt;
                &amp;lt;label for="img" class="btn btn-primary"&amp;gt;Choose a file&amp;lt;/label&amp;gt;
            &amp;lt;/div&amp;gt;

            &amp;lt;div class="col-2"&amp;gt;
                &amp;lt;button class="btn btn-success btn-sm" [hidden]="!selectedFiles" (click)="upload()"&amp;gt;
                    Upload
                &amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;


            &amp;lt;div *ngIf="errmsg" class="alert alert-secondary" role="alert"&amp;gt;{{ errmsg }}&amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;css file&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;.container,
.drag-container,
.upload-container,.uploader {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.container {
  height: 100vh;
}

.uploader {
  width: 350px;
  height: 420px;
  justify-content: space-evenly;
  border: none;
}

.upload-container{
  border-radius: 10px;
  background-color: white;
  box-shadow: 1px 2px 20px 0px rgb(0 0 0 / 20%);
}

.drag-container {
  box-sizing: border-box;
  background: #f6f8fb;
  border: 2px dashed #97bef49c;
  border-radius: 12px;
}

.files {
  width: 225px;
  height: 130px;
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Upload Component ts file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we have to create two methods, first using choose file from folders and second to drag and drop.&lt;/p&gt;

&lt;p&gt;now for drag and drop, use event binding&lt;br&gt;
&lt;code&gt;(dragover)="onDrag($event)" (drop)="onDrop($event)"&lt;/code&gt;&lt;br&gt;
which detects a change when a file is dragged into the input section.&lt;br&gt;
while choose file uses &lt;code&gt;(change)="selectFile($event)"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and on submit button upload method is called, which uses upload method from fileupload service to upload file to the server.&lt;br&gt;
The function first checks if file is selected or not and then subscribes to the service method which returns the event and checks the progress  with &lt;code&gt;event.type ===HttpEventType.UploadProgress&lt;/code&gt; and when it completes  it navigates to completed component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { HttpEventType, HttpResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { FileuploadService } from '../services/fileupload.service';
import { Router } from '@angular/router';
import { SendurlService } from '../services/sendurl.service';

@Component({
  selector: 'app-upload',
  templateUrl: './upload.component.html',
  styleUrls: ['./upload.component.css'],
})
export class UploadComponent implements OnInit {
  selectedFiles?: FileList;
  currentFile?: File;
  progress = 0;
  message = '';
  errmsg = '';

  fileInfos?: Observable&amp;lt;any&amp;gt;;

  constructor(
    private uploadService: FileuploadService,
    private router: Router,
    private sendurl: SendurlService
  ) {}

  ngOnInit(): void {}

  // drag and drop files
  processing?: boolean;

  onDrag(event: any) {
    event.preventDefault();
  }

  onDrop(event: any) {
    event.preventDefault();

    this.onFileChange(event.dataTransfer.files);
  }

  onChange(event: any) {
    this.onFileChange(event.target.files);
  }

  private onFileChange(files: any) {
    this.processing = true;
    this.selectedFiles = files;
    setTimeout(() =&amp;gt; {
      console.log('processed');
      this.processing = false;
    }, 1000);
  }

  // select file
  selectFile(event: any): void {
    this.selectedFiles = event.target.files;
  }
  upload(): void {
    this.progress = 0;

    if (this.selectedFiles) {
      const file: File | null = this.selectedFiles.item(0);
      console.log('file has been selected');
      if (file) {
        this.currentFile = file;

        this.uploadService.upload(this.currentFile).subscribe({
          next: (event: any) =&amp;gt; {
            if (event.type === HttpEventType.UploadProgress) {
              this.progress = Math.round((100 * event.loaded) / event.total);
              console.log(this.progress);
              // console.log(event.loaded);
            } else if (event instanceof HttpResponse) {
              console.log('this is file link', event.body.file);
              this.message = event.body.file;

              // this service will send the download link from the server
              // to child component of upload complete
              this.sendurl.link = this.message;
              this.router.navigate(['/', 'uploaded']);
            }
          },
          error: (err: any) =&amp;gt; {
            console.log(err);
            this.progress = 0;

            if (err.error &amp;amp;&amp;amp; err.error.message) {
              this.errmsg = err.error.message;
            } else {
              this.errmsg = 'Could not upload the file!';
            }

            this.currentFile = undefined;
          },
        });
      }

      this.selectedFiles = undefined;
    }
  }
}

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

&lt;/div&gt;



&lt;p&gt;So now your webapp would look like this:&lt;br&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%2Fh2hm638wozazt2wtyln3.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%2Fh2hm638wozazt2wtyln3.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in sendurl service add this:&lt;br&gt;
&lt;code&gt;link?:string;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now create upload service:&lt;/p&gt;

&lt;p&gt;The service takes file and returns an observable httpevent which uses POST Request to send the file to server. Also create getfiles method to get the link.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@angular/core';
import { HttpClient, HttpRequest, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class FileuploadService {
  // base url
  // host = 'https://share-now-backend.vercel.app/';
  host = 'https://sharenow.onrender.com/';
  // host = 'http://localhost:3000/';
  uploadURL = `${this.host}api/files`;

  constructor(private http: HttpClient) {}

  upload(file: File): Observable&amp;lt;HttpEvent&amp;lt;any&amp;gt;&amp;gt; {
    const formData: FormData = new FormData();
    formData.append('myfile', file);

    const req = new HttpRequest('POST', `${this.uploadURL}`, formData, {
      reportProgress: true,
      responseType: 'json',
    });

    return this.http.request(req);
  }

  getFiles(): Observable&amp;lt;any&amp;gt; {
    return this.http.get(`${this.uploadURL}`);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to have a component when we upload file successfully, and it should provide the link to download.&lt;/p&gt;

&lt;p&gt;completed html:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container"&amp;gt;
    &amp;lt;div class="upload-container"&amp;gt;

        &amp;lt;h4 class="mt-2"&amp;gt; &amp;lt;img src="../../assets/checkmark.png" alt="checkmark"&amp;gt; Uploaded Successfully&amp;lt;/h4&amp;gt;
        &amp;lt;br&amp;gt;
        &amp;lt;img src="../../assets/completed.jpg" id="showimg" alt="image"&amp;gt;
        &amp;lt;br&amp;gt;
        &amp;lt;div class="card-body"&amp;gt;
            &amp;lt;div class="input-group"&amp;gt;
                &amp;lt;input type="text" value={{link}} #userinput&amp;gt;
                &amp;lt;button type="button" class="btn btn-primary" (click)="copyInputMessage(userinput)"&amp;gt;Copy
                    Link&amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;button class="btn btn-success" [routerLink]="['/']"&amp;gt;Home&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;completed css:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.container,
.drag-container,
.upload-container {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
}

.container {
  height: 100vh;
}

.upload-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid black;
  padding: 20px 15px;
  width: 350px;
  height: 500px;
  border: none;
  box-shadow: 1px 2px 20px 0px rgb(0 0 0 / 20%);
  border-radius: 10px;
  background-color: white;
}

#showimg {
  width: 330px;
  height: auto;
  border-radius: 8px;
}

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

&lt;/div&gt;



&lt;p&gt;Now to show download link we need to get it from sendurl service&lt;/p&gt;

&lt;p&gt;completed component ts file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, OnInit, Input } from '@angular/core';
import { SendurlService } from '../services/sendurl.service';

@Component({
  selector: 'app-completed',
  templateUrl: './completed.component.html',
  styleUrls: ['./completed.component.css'],
})
export class CompletedComponent implements OnInit {
  constructor(private geturl: SendurlService) {}
  link = this.geturl.link;
  ngOnInit(): void {}
  /* To copy Text from Textbox */
  copyInputMessage(inputElement: any) {
    inputElement.select();
    document.execCommand('copy');
    inputElement.setSelectionRange(0, 0);
  }
}

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

&lt;/div&gt;



&lt;p&gt;This will show the file upload completed view with the link to download the file.&lt;br&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%2F3nnxeba05a0jke1qxbut.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%2F3nnxeba05a0jke1qxbut.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This Completes our Angular App !! Hope you Enjoyed working on this app, and learned something new.&lt;/p&gt;

&lt;p&gt;Further We will have to make Backend APIs with NodeJS and Express.&lt;br&gt;
Here is Part 2 where we will complete our FULL STACK APP:&lt;br&gt;
&lt;a href="https://dev.to/varun21vaidya/mean-stack-project-create-quick-file-share-application-part-2-backend-22c3"&gt;https://dev.to/varun21vaidya/mean-stack-project-create-quick-file-share-application-part-2-backend-22c3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HAPPY CODING !!&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>🎯How WEB Works and Intro To Frontend 🌱</title>
      <dc:creator>Varun</dc:creator>
      <pubDate>Fri, 06 Jan 2023 14:42:09 +0000</pubDate>
      <link>https://dev.to/varun21vaidya/new-post-2g51</link>
      <guid>https://dev.to/varun21vaidya/new-post-2g51</guid>
      <description>&lt;p&gt;Hi Guys👋, Today we will see How to Start Web Dev Journey🧑‍💻&lt;br&gt;
And if you have already started this can be your first lesson 🌟&lt;/p&gt;
&lt;h2&gt;
  
  
  0️⃣ Pillars of Front End:
&lt;/h2&gt;

&lt;p&gt;HTML: hypertext markup language, to define building blocks of web pages (markup language)&lt;/p&gt;

&lt;p&gt;CSS: cascading style sheet and used for styling and making it beautiful (styling language)&lt;/p&gt;

&lt;p&gt;JAVASCRIPT: gives functionality to webpages (&lt;strong&gt;programming language&lt;/strong&gt;)&lt;/p&gt;
&lt;h2&gt;
  
  
  1️⃣ Frontend Frameworks:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;REACT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ANGULAR&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VUE&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  2️⃣ HOW THE WEB WORKS
&lt;/h2&gt;

&lt;p&gt;the address we type in address bar is URL (Uniform Resource Location), it is a way to locate a resource on the internet like web pages, images videos..&lt;/p&gt;

&lt;p&gt;CLIENT SERVER MODEL:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WWW&lt;/strong&gt; -world wide web is a collection of web documents and resourses which are accessible over the internet. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;URL&lt;/strong&gt;: uniform resource locator uniformly locates a particular in the internet. it has following components&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;protocol://host:port/path&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;http : // &lt;a href="http://www.myapp.com/" rel="noopener noreferrer"&gt;www.myapp.com/&lt;/a&gt; results&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;protocol&lt;/strong&gt;: used for client server communication&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;host and port&lt;/strong&gt; : name of the connected server along with port number&lt;/p&gt;

&lt;p&gt;each device connected on the internet will be accessible with unique host name also called as &lt;strong&gt;domain name&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;and different servises will be provided through specific  port numbers, if explicit port is not mentioned default will be used&lt;/p&gt;

&lt;p&gt;standard port for HTTP is 80 and 443 for HTTPS&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;path:&lt;/strong&gt; path to access the resource / web root&lt;/p&gt;
&lt;h2&gt;
  
  
  3️⃣ HTTP:
&lt;/h2&gt;

&lt;p&gt;HTTP is a request transfer based protocol as client establishes a connection with server and sends a request over the same connection server sends the response &lt;/p&gt;

&lt;p&gt;when client wants to send request again new connection request will be created&lt;/p&gt;

&lt;p&gt;A client requests a service and server provides it.. so here browser asks a server about info of specific website.&lt;/p&gt;

&lt;p&gt;now this msg is formatted according to a protocol called HTTP (Hypertext Transfer Protocol) , so its a plain language for communicating over the internet&lt;/p&gt;

&lt;p&gt;HTTPS - HTTP with encryption&lt;/p&gt;

&lt;p&gt;SO when browser asks a request like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="nx"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="nx"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt;
&lt;span class="nx"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;www&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;com&lt;/span&gt;
&lt;span class="nx"&gt;Accept&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;en&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;us&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;browser sends a response like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="nx"&gt;OK&lt;/span&gt;
&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nx"&gt;Jan&lt;/span&gt; &lt;span class="mi"&gt;2021&lt;/span&gt; &lt;span class="mi"&gt;09&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;
&lt;span class="nx"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nx"&gt;DOCTYPE&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

    ...

&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So when the browser reads this HTML documents it creates DOM - Document Object Model&lt;/p&gt;

&lt;p&gt;this is model that represents objects and elements in HTML document ie elements like building blocks, paragraphs of text, images links etc..&lt;/p&gt;

&lt;p&gt;So when browser reads the doc, it sends requests to access the images videos link as separate HTTP request to server, and to increase loading speeds, they are sent parallelly&lt;/p&gt;

&lt;p&gt;one the browser has all the necessary resources the browser renders HTML document.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MIME&lt;/strong&gt; : Multipurpose Internet Mail Extension&lt;/p&gt;

&lt;p&gt;indicates the format of the resource content from server to the client&lt;/p&gt;

&lt;p&gt;contents such as text, image, videos, application (json, sql, javascript) . &lt;/p&gt;

&lt;h2&gt;
  
  
  4️⃣ HTML &amp;amp; CSS
&lt;/h2&gt;

&lt;p&gt;The following are some key elements in HTML that form the basic structure of a web page.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&amp;lt;!DOCTYPE html&amp;gt; declaration update the browser about the version of HTML being used. By default, it points to HTML5, the latest version.&lt;/li&gt;
&lt;li&gt;The  tag encapsulates the complete web page content. All other tags are 'nested' within the  tag.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Any HTML document or web page consists of two main sections the 'head' and the 'body'.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The head section starts with the start tag  and ends with the end tag .&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;HTML is not case sensitive language but generally we write in lowercase&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;IN css style, if if border-radius is more than half of width it can become circle.&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%2F0vouc5suxkh777l318ta.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%2F0vouc5suxkh777l318ta.png" alt="difference between container elements and empty elements" width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a&gt; represents anchor element (link to another website)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;href="&lt;a href="https://www.youtube.com/%22%3E" rel="noopener noreferrer"&gt;https://www.youtube.com/"&amp;gt;&lt;/a&gt; this represents HTML attribute which modifies how an element behaves&lt;/p&gt;

&lt;p&gt;href is attribute name and “...” is value.&lt;/p&gt;

&lt;p&gt;target=”_blank” ensures the link will open in new tab if its not present link will open in current page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://www.youtube.com/"&lt;/span&gt; &lt;span class="na"&gt;target=&lt;/span&gt;&lt;span class="s"&gt;”_blank”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Link to Youtube&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 Extra spaces are ignored in HTML&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here button is CSS Selector &lt;/p&gt;

&lt;p&gt;and background-color is a property.&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%2Fbvuqv1u48zqk01q2loki.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%2Fbvuqv1u48zqk01q2loki.png" alt="difference between inline element and block element" width="620" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TO CREATE EFFECTS ON BUTTONS WHEN WE HOVER ON THEM,&lt;/p&gt;

&lt;p&gt;to change color when we hover on them,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.subcribe-btn&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;252&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;48&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;48&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.747&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 :hover is psudo-class, these psudo classes adds extra styles in certain situations&lt;/p&gt;

&lt;p&gt;Similarly,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;:active&lt;/strong&gt; Psudo-class adds functionalities when we click on the button&lt;/p&gt;

&lt;p&gt;to change the opacity we can use opacity between 0 to 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.join-btn&lt;/span&gt;&lt;span class="nd"&gt;:active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to adjust transition speed of the hover property ie not instantaneously (but use this in base css style not in hover style)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;transition&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;opacity&lt;/span&gt; &lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="nt"&gt;s&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to create shadows after hovering&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.Tweet-btn&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;15px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should not set the height and width to the buttons, as if the text length is more it would flow out of box.&lt;/p&gt;

&lt;p&gt;instead we can use inside space of button, ie  padding.&lt;/p&gt;

&lt;p&gt;span is generic element used to give special characteristics to certain text in paragraph using class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"offer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Shop early for best selection of holiday favourites.
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"shop"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Shop Now !&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and in css we can give styles&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.shop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nc"&gt;.shop&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;underline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for now⌚, as you ready to get into this Web Dev Field.&lt;br&gt;
Further, there are tons of things to learn from this vast internet 🕸️. I hope I have made you more curious about them, Happy Learning 🚀!!&lt;/p&gt;

</description>
      <category>website</category>
      <category>launch</category>
    </item>
  </channel>
</rss>
