<?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: Rahul Dwivedi</title>
    <description>The latest articles on DEV Community by Rahul Dwivedi (@rdrahul).</description>
    <link>https://dev.to/rdrahul</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%2F378485%2F896908f4-f951-47b5-bcb3-b25da762a62f.jpeg</url>
      <title>DEV Community: Rahul Dwivedi</title>
      <link>https://dev.to/rdrahul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rdrahul"/>
    <language>en</language>
    <item>
      <title>What is a network socket?</title>
      <dc:creator>Rahul Dwivedi</dc:creator>
      <pubDate>Sat, 16 May 2020 07:44:59 +0000</pubDate>
      <link>https://dev.to/rdrahul/what-is-a-network-socket-5d4</link>
      <guid>https://dev.to/rdrahul/what-is-a-network-socket-5d4</guid>
      <description>&lt;p&gt;This article explains the network socket in an easy way&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The definition explains socket in basic terms but to get a crystal clear picture of it, let's understand this with the help of an analogy.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Think you are in your house due to lock down and you are prohibited to go out of your house, Let's build on this. one day since you were super bored you decided to have a chat with your neighbor, we will assume your house has a window. ( &lt;em&gt;No! you don't jump from it&lt;/em&gt;). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In order to have a chit chat with your neighbor you go to the window and wave hello. Luckily, your neighbor is standing right on the window and he has his window open. he waves back and you guys start communication. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's compare it with our definition of network sockets &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Here, House is analogous to a computer. &lt;/li&gt;
&lt;li&gt;The window we are talking about is like a socket, an endpoint from where you can send messages or receive one&lt;/li&gt;
&lt;li&gt;the conversation you are having is data.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;A socket is one endpoint of a two-way communication link between two programs running on the network.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Like in our example windows are endpoints. if we close the windows or don't have windows, then we won't be able to transfer our messages. (&lt;em&gt;oh please! you are not allowed to call.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;So I think this gives us a pretty clear picture of what a network socket is. Let's move on to the other parts.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NzqXhmrh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/f5g5zCV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NzqXhmrh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/f5g5zCV.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A socket is bound to a port number so that the Network layer can identify the application that data is destined to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's also assume that the house has 6 rooms and there are 6 family members one in each room. All the rooms have a window and a number (1 to 6). Now if a neighbor wants to talk to one of the family members they will have to specify room number as well. &lt;/p&gt;

&lt;p&gt;Similarly, on a computer, there can be many applications running. Each socket must be attached to a port ( which will represent the application ). This way Network layer knows where to send incoming data to.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An endpoint is a combination of an IP address and a port number.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With the help of port, we only know which application to send data to, but in a network, there could be many computers hence we need an address to uniquely identify the host we want to talk to.&lt;/p&gt;

&lt;p&gt;In our analogy, there could be many houses. In order to talk to someone, you need their house address and the window number you want to talk to. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is an IP Address?
&lt;/h3&gt;

&lt;p&gt;An IP address is an address assigned to each device within a network to be able to uniquely identify it. It can be of the form &lt;code&gt;172.16.254.1&lt;/code&gt; in IPv4, and  &lt;code&gt;2001:db8:0:1234:0:567:8:1&lt;/code&gt; in IPv6. you can read more &lt;a href="https://en.wikipedia.org/wiki/IP_address"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is IP?
&lt;/h3&gt;

&lt;p&gt;IP refers to internet protocol. It is a set of rules that dictates how the communications should happen in a network.&lt;/p&gt;

&lt;p&gt;Let's look at the whole flow step by step.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your application can listen to the messages or send messages to or from the network by creating a socket. &lt;/li&gt;
&lt;li&gt;In order to create a socket, we need to bind it to a port. &lt;/li&gt;
&lt;li&gt;Someone in a network wants to communicate. They need an IP address ( to uniquely identify our device) and a port number( the port we have bound our socket to or basically where the application is listening ).&lt;/li&gt;
&lt;li&gt;They send a message to us. The network first recognizes the IP and tries to find the device from it. it sends the message to the device.&lt;/li&gt;
&lt;li&gt;The device has a network layer that looks at the incoming messages from the internet. It receives the messages and parses the port number and passes it to that socket. &lt;/li&gt;
&lt;li&gt;The socket receives the message and the application which created the socket, now has access to the message and everyone is happy. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The flow depicted here is an ideal flow. There are many other protocols involved like how the message will be packed and how they will be routed. How will the sender know if the package is received or is lost somewhere in between? For now, let's look at TCP or UDP.&lt;/p&gt;

&lt;h2&gt;
  
  
  TCP and UDP
&lt;/h2&gt;

&lt;p&gt;Once we receive messages there has to be a way to pass them to the application. We have another layer that sits in between the application and the network layer which is called the transport layer. It is referred to as the transport layer because it handles how the packages will be sent to the application from the network layer and vice versa. This is required because at a point there could be multiple sockets open and a lot of data that the device must be receiving. Its job is to collect all the packets meant for sockets and transfer them respectively.&lt;/p&gt;

&lt;p&gt;There are two basic protocols for this: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TCP&lt;/strong&gt;: Transmission Control Protocol is a connection-oriented protocol i.e it ensures that a proper connection exists before the communication can begin. Think of it as you want to talk to the neighbor but you will only start a conversation once you are sure they are able to listen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UDP&lt;/strong&gt;: User Datagram Protocol is a connectionless protocol that does not care for a proper connection which makes it fast but less reliable. For example, you might have experienced game lags that because some of the messages were lost. Most Real-time communication and VoIP are done through UDP.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KOnCdIIG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/zSZWqde.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KOnCdIIG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/zSZWqde.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now since there are two types of protocols you also must specify which protocol you want to use that why the socket you will create will either be a UDP socket or a TCP socket ( I am lying there are more!!). basically a listening socket looks like&lt;/p&gt;


&lt;center&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g6A1LkLg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/TQzO673.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g6A1LkLg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/TQzO673.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;/center&gt;
&lt;br&gt;&lt;br&gt;
(Please do not try to hack my pc).

&lt;p&gt;Here we can see the protocol in the first column and the port along with IP in the 4th column.&lt;/p&gt;

&lt;p&gt;I think by now you would have got a clear idea about network sockets and other basic concepts. we have just scratched the surface there is a lot you can know. I would encourage you to read more about the TCP/IP model and the OSI model. Learning about TCP and IP in detail once. &lt;/p&gt;

&lt;p&gt;Feel free to ask any questions or in case you want to have any discussion do comment below.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>basecs</category>
    </item>
    <item>
      <title>Dependency Injection an Introduction</title>
      <dc:creator>Rahul Dwivedi</dc:creator>
      <pubDate>Tue, 05 May 2020 18:44:57 +0000</pubDate>
      <link>https://dev.to/rdrahul/dependency-injection-an-introduction-4424</link>
      <guid>https://dev.to/rdrahul/dependency-injection-an-introduction-4424</guid>
      <description>&lt;p&gt;In this article, we are going to discuss Dependency injection. We will also learn what are the benefits of using dependency injection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Dependency injection&lt;/strong&gt; a type of design pattern that allows classes to receive dependencies rather than creating them on their own.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Dependency?
&lt;/h3&gt;

&lt;p&gt;A Dependency refers to any object that a class needs in order to be fully functional. An example would be, suppose a car manufacturer produces a car. For this example, suppose the car manufacturer produces everything needed to manufacture car other than the tyres. For tyres, It depends on some other company. Without tyres, the car is incomplete. Tyres here are a &lt;strong&gt;dependency&lt;/strong&gt; for the car. &lt;/p&gt;

&lt;p&gt;We have now a clear idea of what dependency is. Let's look at this through some code.&lt;/p&gt;

&lt;p&gt;Suppose you have class that depends on some other object for its execution. Lets looks at the following example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ChocolateCone&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;GetCone&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Chocolate cone&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="nx"&gt;IceCreamParlour&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;cone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ChocolateCone&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&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;cone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ChocolateCone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;    
    &lt;span class="nx"&gt;Buy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flavour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="s2"&gt;`Icecream with a scoop of &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;flavour&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and &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;cone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getCone&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&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;const&lt;/span&gt; &lt;span class="nx"&gt;icecreamParlour&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;IceCreamParlour&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;icecreamParlour&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;strawberry&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//Icecream with a scoop of strawberry and Chocolate cone.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's look at the above code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We have a class ChocolateCone that gives us Chocolate cone.&lt;/li&gt;
&lt;li&gt;We have an ice cream parlour that gives icecreams 😋. Here you can see it &lt;strong&gt;Depends&lt;/strong&gt; on the ChocolateCone class for providing cones for the ice cream. &lt;/li&gt;
&lt;li&gt;In order to buy ice cream, we create an IceCreamParlor object and then call the Buy method on it and we get our Strawberry ice cream with a chocolate cone. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This class works well for this specific scenario but let's suppose, we don't want chocolate cones, we want waffle cones, we realize that there is a problem. IceCreamParlour is tightly coupled with ChocolateCone. To get a waffle cone, we will have to change the constructor and create a waffle cone instead of Chocolate Cone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="p"&gt;...&lt;/span&gt;
        &lt;span class="kd"&gt;constructor&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;cone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;WaffleCone&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;p&gt;As we can see this won't be ideal scenario to start modifying the code everytime dependency requirement changes. &lt;/p&gt;

&lt;p&gt;This is where Dependency injection comes into play.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency Injection
&lt;/h2&gt;

&lt;p&gt;Since we have understood the problem, let's look at how we can use Dependency injection to solve this.&lt;br&gt;
Dependency Injection states &lt;br&gt;
If we change the constructor of our parlour class and supply the dependencies to it rather than letting it create itself, we can overcome this problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ICone&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;GetCone&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="nx"&gt;string&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="nx"&gt;IceCreamParlour&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;cone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ICone&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ICone&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;cone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cone&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;    
    &lt;span class="nx"&gt;Buy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flavour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="s2"&gt;`Icecream with a scoop of &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;flavour&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and &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;cone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GetCone&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&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;/div&gt;



&lt;p&gt;Here, the IceCreamParlour expects a cone and does not creates it itself. This is how we have resolved the problem. Whatever cone we want we can supply it to IceCreamParlour at the time of object creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ChocolateCone&lt;/span&gt; &lt;span class="kr"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;ICone&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;GetCone&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Chocolate cone&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="nx"&gt;WaffleCone&lt;/span&gt; &lt;span class="kr"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;ICone&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;GetCone&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Waffle cone&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;const&lt;/span&gt; &lt;span class="nx"&gt;waffleParlour&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;IceCreamParlour&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;WaffleCone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;waffleParlour&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;strawberry&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Icecream with a scoop of strawberry and Waffle cone;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chocolateParlour&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;IcecreamParlour&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ChocolateCone&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;chocolateParlour&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Buy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vanilla&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Icecream with a scoop of Chocolate Cone.&lt;/span&gt;

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



&lt;p&gt;When we are creating an object of the class, we are injecting the dependencies into the object. The class does not have to worry about it. If you observe, we have taken control of creating dependency &lt;strong&gt;from the class&lt;/strong&gt;. This is also called &lt;em&gt;Inversion of Control&lt;/em&gt;. The class does not have control over the creation of the object. It just specifies in constructor its dependencies and at the time of creating class instance dependency is created and served to it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Some Benefits of using DI
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Helps in reusability and readability.&lt;/li&gt;
&lt;li&gt;Solves the problem of tight coupling, produces loosely coupled code.&lt;/li&gt;
&lt;li&gt;Helps in writing Unit tests or mocking behaviours.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With this, you have a clear idea of what Dependency injection and how you can use it to create decoupled and reusable classes. It is also mentioned in &lt;strong&gt;S.O.L.I.D&lt;/strong&gt; principles, the D stands for DIP - Dependency inversion Principle.&lt;/p&gt;

&lt;p&gt;Most of the time you will end up using a DI container. You can easily find a dependency injection container according to your language preference if you want to use this principle in your projects. One such DI container is &lt;a href="https://github.com/microsoft/tsyringe"&gt;tsyringe&lt;/a&gt; for Typescript projects.&lt;/p&gt;

</description>
      <category>codequality</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
