DRY is simply an approach, or we can say, a different perspective to programmers. DRY stands for Don’t Repeat Yourself. In Java, it means don’t write the same code repeatedly. Suppose you are having the same code at many places in your program, then it means you are not following the DRY approach; You are repeating the same code at different places. Hence, the solution is obtained using the DRY concept by placing the methods in place of all repeated codes and defining the code in one method. So by calling methods, we will reach the principle DRY. The DRY concept is very important to make the code better by reducing code redundancy and to encourage its reusability.
Applications:
- Online marketing applications
- Education
- Financial applications
Illustration 1:
Consider the scenario of the college student system. The college contains many departments. So each department has different people, but the college name is the same. So no need to specify the college name for each department by writing the code for the display of the college name.
Implementation: Without DRY approach
Example 1:
Java
public class GFG {
public void CSE()
{
System.out.println( "This is computer science" );
}
public void college()
{
System.out.println( "IIT - Madras" );
}
public void ECE()
{
System.out.println( "This is electronics" );
}
public void college1()
{
System.out.println( "IIT - Madras" );
}
public void IT()
{
System.out.println(
"This is Information Technology" );
}
public void college2()
{
System.out.println( "IIT - Madras" );
}
public static void main(String[] args)
{
GFG s = new GFG();
s.CSE();
s.college();
s.ECE();
s.college1();
s.IT();
s.college2();
}
}
|
Output
This is computer science
IIT - Madras
This is electronics
IIT - Madras
This is Information Technology
IIT - Madras
Implementation: Applying the DRY principle
- Here we create only one method named college and then call the method in all the departments.
Example 1:
Java
import java.util.*;
public class GFG {
public void CSE()
{
System.out.println( "This is computer science" );
college();
}
public void ECE()
{
System.out.println( "This is electronics" );
college();
}
public void IT()
{
System.out.println(
"This is Information Technology" );
college();
}
public void college()
{
System.out.println( "IIT - Madras" );
}
public static void main(String[] args)
{
GFG s = new GFG();
s.CSE();
s.ECE();
s.IT();
}
}
|
Output
This is computer science
IIT - Madras
This is electronics
IIT - Madras
This is Information Technology
IIT - Madras
Illustration 2:
Consider a program to calculate x3 and y5 for given inputs x and y.
Implementation : (Without DRY approach)
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print( "Enter the value of x: " );
int x = sc.nextInt();
System.out.print( "Enter the value of y: " );
int y = sc.nextInt();
int ans1 = 1 ;
for ( int i = 1 ; i <= 3 ; i++)
{
ans1 *= x;
}
System.out.println( "x raised to the power 3 = " + ans1);
int ans2 = 1 ;
for ( int i = 1 ; i <= 5 ; i++)
{
ans2 *= y;
}
System.out.println( "y raised to the power 5 = " + ans2);
}
}
|
Output:
Enter the value of x: 3
Enter the value of y: 2
x raised to the power 3 = 27
y raised to the power 5 = 32
Explanation: In the above program, the logic to calculate the power of x and y is same for both the inputs. But this code is repeatedly used twice: once for x and once for y. So, to avoid this redundancy (repetition) of code, we will create a method to calculate power and place those repetitive lines of code into that method. And then we can call the method whenever and as many times we need to calculate the power.
Implementation: (After applying DRY approach)
Java
import java.io.*;
class GFGCalc
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print( "Enter the value of x: " );
int x = sc.nextInt();
System.out.print( "Enter the value of y: " );
int y = sc.nextInt();
GFGCalc obj = new GFGCalc();
int ans1 = obj.calcPow(x, 3 );
int ans2 = obj.calcPow(y, 5 );
System.out.println( "x raised to 3 = " + ans1);
System.out.println( "y raised to 5 = " + ans2);
}
private int calcPow( int base, int pow)
{
int ans = 1 ;
for ( int i = 1 ; i <= pow; i++)
{
ans = ans * base;
}
return ans;
}
}
|
Output:
Enter the value of x: 3
Enter the value of y: 2
x raised to 3 = 27
y raised to 5 = 32
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Apr, 2023
Like Article
Save Article