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
// Java Program without DRY approach // Main class public class GFG { // Method 1 // For cse department public void CSE() { System.out.println( "This is computer science" ); } // Method 2 // For cse dept. college public void college() { System.out.println( "IIT - Madras" ); } // Method 3 // ece dept method public void ECE() { System.out.println( "This is electronics" ); } // Method 4 // For ece dept college 1 public void college1() { System.out.println( "IIT - Madras" ); } // Method 5 // For IT dept public void IT() { System.out.println( "This is Information Technology" ); } // Method 6 // For IT dept college 2 public void college2() { System.out.println( "IIT - Madras" ); } // Method 7 // Main driver method public static void main(String[] args) { // Creating object of class in main() method GFG s = new GFG(); // Calling above methods one by one // as created above s.CSE(); s.college(); s.ECE(); s.college1(); s.IT(); s.college2(); } } |
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
// Java Program with Use of DRY Concept // Importing input output classes import java.util.*; // Main class public class GFG { // Method 1 // For cse department public void CSE() { // Print statement System.out.println( "This is computer science" ); // Calling method college(); } // Method 2 // For ece dept method public void ECE() { System.out.println( "This is electronics" ); // Calling method college(); } // Method 3 // For IT dept public void IT() { // Print statement System.out.println( "This is Information Technology" ); // Calling method college(); } // Method 4 // For college dept public void college() { // Print statement System.out.println( "IIT - Madras" ); } // Method 5 // Main driver method public static void main(String[] args) { // Creating object of class in main() method GFG s = new GFG(); // Calling the methods one by one // as created above s.CSE(); s.ECE(); s.IT(); } } |
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
// Program to calculate x3 and y5 for given inputs x and y import java.util.*; class GFG { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Taking inputs as x and y System.out.print( "Enter the value of x: " ); int x = sc.nextInt(); System.out.print( "Enter the value of y: " ); int y = sc.nextInt(); // Calculating x raised to 3 int ans1 = 1 ; for ( int i = 1 ; i <= 3 ; i++) { ans1 *= x; // means ans1=ans1*x; } System.out.println( "x raised to the power 3 = " + ans1); // Calculating y raised to 5 int ans2 = 1 ; for ( int i = 1 ; i <= 5 ; i++) { ans2 *= y; // means ans2=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
// Same program with DRY approach 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(); // calling calcPow() method 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