Java的DRY(不要重复自己)原则和示例
DRY 只是一种方法,或者我们可以说对程序员来说是一种不同的感知。 DRY 代表不要重复自己。在Java,这意味着不要重复编写相同的代码。假设您在程序中的许多地方都有相同的代码,那么它被称为 DRY,您在不同的地方重复重复相同的代码。因此,使用 DRY 概念通过将方法放置在所有重复代码的位置并在一种方法中定义代码来获得解决方案。所以通过调用方法,我们将达到 DRY 原则。
应用:
- 网络营销应用
- 教育
- 金融应用
插图:
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.
实施:没有 DRY 方法
例子:
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();
}
}
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");
// Callling 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();
}
}
Java
// Java Program with Use of DRY Concept
// Importing input output classes
import java.util.*;
// Main class
public class GFG {
// Method 1
// For person1
public void person1()
{
// Print statement
System.out.println("sravan");
// Calling Method 3
bank();
}
// Method 2
// For person 2
public void person2()
{
// Print statement
System.out.println("ramya");
// Calling method 3
bank();
}
// Method 3
// For bank
public void bank()
{
// Print statement
System.out.println("SBI");
}
// Method 4
// Main driver method
public static void main(String[] args)
{
// Creating objectof class in main() method
GFG s = new GFG();
// Calling the methods one by one
s.person1();
s.person2();
}
}
输出
This is computer science
IIT - Madras
This is electronics
IIT - Madras
This is Information Technology
IIT - Madras
实施:应用 DRY 原则
- 这里我们只创建了一个名为 College 的方法,然后在所有系中调用该方法。
示例 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");
// Callling 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
实施:申请后与一家银行有关联的人的银行名称 该干的应用[R oach
示例 2:
Java
// Java Program with Use of DRY Concept
// Importing input output classes
import java.util.*;
// Main class
public class GFG {
// Method 1
// For person1
public void person1()
{
// Print statement
System.out.println("sravan");
// Calling Method 3
bank();
}
// Method 2
// For person 2
public void person2()
{
// Print statement
System.out.println("ramya");
// Calling method 3
bank();
}
// Method 3
// For bank
public void bank()
{
// Print statement
System.out.println("SBI");
}
// Method 4
// Main driver method
public static void main(String[] args)
{
// Creating objectof class in main() method
GFG s = new GFG();
// Calling the methods one by one
s.person1();
s.person2();
}
}
输出
sravan
SBI
ramya
SBI