<?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>Forem: Ghazanfar Mumtaz</title>
    <description>The latest articles on Forem by Ghazanfar Mumtaz (@ghazanfarofficial01).</description>
    <link>https://forem.com/ghazanfarofficial01</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%2F887021%2Fe57ef75e-b7be-465f-9cfa-e3f4193faeb5.png</url>
      <title>Forem: Ghazanfar Mumtaz</title>
      <link>https://forem.com/ghazanfarofficial01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ghazanfarofficial01"/>
    <language>en</language>
    <item>
      <title>DAO Design Pattern</title>
      <dc:creator>Ghazanfar Mumtaz</dc:creator>
      <pubDate>Sat, 16 Dec 2023 15:01:21 +0000</pubDate>
      <link>https://forem.com/ghazanfarofficial01/dao-design-pattern-2g53</link>
      <guid>https://forem.com/ghazanfarofficial01/dao-design-pattern-2g53</guid>
      <description>&lt;p&gt;Data Access Object design pattern or DAO pattern is a fundamental structural pattern commonly used in Java Applications.&lt;/p&gt;

&lt;p&gt;DAO Design Pattern is used to separate low-level data accessing API or operations from high-level business logics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why did we need DAO Design Pattern?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before the DAO pattern, applications often directly embedded data access logic within their business logic. This led to tight coupling and several downsides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Difficult Maintenance:&lt;/strong&gt; Changes in the database structure or access methods required modifications throughout the application, making maintenance cumbersome.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limited Reusability:&lt;/strong&gt; The Data access code was specific to a particular database and couldn't be easily reused in other parts of the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced Testability:&lt;/strong&gt; Testing business logic became more complex due to intertwined data access operations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Data Access Object Design Pattern addresses these issues by introducing a dedicated layer for data access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Following are the components in DAO Pattern.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DAO Interface:&lt;/strong&gt; Defines a standard set of operations that can be performed on a specific data model. This interface remains independent of the underlying database implementation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DAO Implementation:&lt;/strong&gt; - This class implements above interface. This class is responsible to get data from a data source which can be database / xml or any other storage mechanism.&lt;br&gt;
It Provides concrete implementations of the DAO interface using &lt;br&gt;
the chosen data access framework (e.g., JDBC)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Model Object:&lt;/strong&gt; Represents the data model with appropriate properties and methods.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Business Logic:&lt;/strong&gt; Utilizes the DAO interface to access and manipulate data without any knowledge of the underlying database technology.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lKKylRUw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cah0zppu7ny0o2ive01r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lKKylRUw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cah0zppu7ny0o2ive01r.jpg" alt="Image description" width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Define the DAO Interface:&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;interface PersonDao{
    public Person getName(int id) throws SQLException;

    public Person getDetails(int Personid) throws SQLException;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Implement the DAO:&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;class PersonDaoImplementation implements PersonDao{
    public Person getName(int id) throws SQLException {
        Person p = new Person();
        p.PersonId = id;

        //fetch data from the database
        String name = DataRequestHandler.getNamefromDataBase(int PersonId) //just an example 

        p.name = name;
        return p;
    }

    @Override
    public Person getDetails(int Personid) throws SQLException {
        return null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Model Object:&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;class Person{
    private int PersonId;
    private String name;

    public int getPersonId() {
        return PersonId;
    }

    public void setPersonId(int personId) {
        PersonId = personId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Utilize the DAO in Business Logic:&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;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcDaoDemo {

    public static void main(String[] args) throws Exception {
        PersonDao dao = new PersonDaoImplementation();
        Person p1 = dao.getName(9);
        System.out.println(p1.getName());
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Separation of Concerns:&lt;/strong&gt; Isolates data access logic, making the business logic cleaner and easier to maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Database Independence:&lt;/strong&gt; Enables switching databases without affecting the business logic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Encapsulation:&lt;/strong&gt; Protects the application from changes in the underlying database implementation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Promotes code reuse for data access operations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The DAO design pattern plays a crucial role in building clean and maintainable Java applications. Separating data access concerns and promoting abstraction, leads to improved code quality, flexibility, and testability. By understanding and implementing the DAO pattern effectively, developers can build robust and efficient applications that are easier to maintain and adapt to future changes.&lt;/p&gt;

</description>
      <category>java</category>
      <category>softwaredevelopment</category>
      <category>programming</category>
      <category>softwaredesign</category>
    </item>
    <item>
      <title>HOISTING in javascript</title>
      <dc:creator>Ghazanfar Mumtaz</dc:creator>
      <pubDate>Fri, 08 Dec 2023 14:35:58 +0000</pubDate>
      <link>https://forem.com/ghazanfarofficial01/hoisting-in-javascript-26ib</link>
      <guid>https://forem.com/ghazanfarofficial01/hoisting-in-javascript-26ib</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Hoisting?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hoisting is the behavior in JavaScript where variable and function declarations are lifted to the top of their respective scopes during the compilation phase. This means that, regardless of where the declarations appear in the code, they are effectively moved to the top of their containing scope, making them available for use even before their actual placement in the code.&lt;/p&gt;

&lt;p&gt;Take a look at the code below and think what will its output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(notEqual(1, '1')); // =&amp;gt; false
// Function declaration
function notEqual(item1, item2) {
   return item1 === item2;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are able to use &lt;strong&gt;notEqual&lt;/strong&gt; function even before it is declared.This is the magic of hoisting in javascript&lt;/p&gt;

&lt;p&gt;The code works nicely because notEqual() is created by a function &lt;strong&gt;declaration&lt;/strong&gt; and hoisted to the top of the scope.&lt;/p&gt;

&lt;p&gt;Ok, so lets again look at an example and guess its output.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(name);&lt;br&gt;
 var name = "John";&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
It might surprise you that this code outputs &lt;strong&gt;undefined&lt;/strong&gt; and doesn't fail or throw an error neither it prints the value 'John' assigned to name variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's understand this behavior of js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hoisting influences the variable life cycle, which consists of 3 steps:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Declaration - create a new variable.&lt;br&gt;
Initialization - initialize the variable with a value.&lt;br&gt;
Usage - access and use the variable value.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Declaration –&amp;gt; Initialisation/Assignment –&amp;gt; Usage&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;code&gt;// Declare&lt;br&gt;
var name; &lt;br&gt;
// Initialize&lt;br&gt;
name = 'John;&lt;br&gt;
// Use&lt;br&gt;
console.log(name); // =&amp;gt; John&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Hoisting  moves the variables and functions &lt;strong&gt;declaration&lt;/strong&gt; to the top of the function scope (or global scope if outside any function).&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;So in this code &lt;code&gt;console.log(name); var name = "John"&lt;/code&gt;&lt;br&gt;
only declaration &lt;code&gt;var name&lt;/code&gt; is moved to the top of scope.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;var name&lt;/code&gt; default value is undefined..&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Actually Hoisting was created for &lt;code&gt;functions&lt;/code&gt; only and its applicability to &lt;code&gt;var&lt;/code&gt; is just the by product.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/BrendanEich/status/522395336615428097"&gt;JS creator Brendan Eich once said on X (the social media platform formerly known as Twitter)&lt;/a&gt;&lt;/p&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://twitter.com/aravind030792?ref_src=twsrc%5Etfw"&gt;@aravind030792&lt;/a&gt; var hoisting was thus unintended consequence of function hoisting, no block scope, JS as a 1995 rush job. ES6 'let' may help.&lt;/p&gt;— BrendanEich (@BrendanEich) &lt;a href="https://twitter.com/BrendanEich/status/522395336615428097?ref_src=twsrc%5Etfw"&gt;October 15, 2014&lt;/a&gt;
&lt;/blockquote&gt; 

&lt;p&gt;&lt;strong&gt;Variable hoisting with let and const&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are hoisted but not initialized with a default value. Accessing a let or const variable before it is declared will result in a ReferenceError:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function learnHoisting(value) {
  if (value) {
    //temporal dead zone for name

   // Throws ReferenceError: name is not defined
    console.log(name);
    let name = 'foo';
    // end of temporary dead zone for name
    console.log(name); // =&amp;gt; 'foo'
    return true;
  }
  return false;
}
learnHoisting(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The interpreter still hoists name: the error message tells us that variable is initialized somewhere.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note - From the declaration statement up to the beginning of the block the let and const variable are in a temporal dead zone (TDZ) and cannot be accessed.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
