After completing the Object-Oriented Programming in Java, if you want to deep dive into the Advanced Java Coursework, then learning about the “GRASP and SOLID in Java” will be ideal.
If you want to write Clean and Maintainable Java Code in OOP, then you should strengthen yourself in the Design Principles. Together, the GRASP and SOLID will help us in that field.
In this article, we will first discuss the GRASP and its different principles. After that, we will let you know about SOLID and its different principles. So, let us start our discussion.
Summary Or Key Highlights:
- With the help of GRASP, we can assign the responsibilities of Classes and Objects in OOP.
- There are 9 Principles of the GRASP that we have to know in the Java Coursework.
- With the help of SOLID, we can increase the Maintainability of the OOP Code during development.
- With the First Character of 5 different principles, the SOLID Name has been defined.
- We will use a Real-life Case Study to demonstrate the use of SOLID and GRASP in Java.
What Is GRASP In Java Programming?
The GRASP in Java Programming stands for the General Responsibility Assignment Software Patterns. Here, we will find a Set of Rules that help to assign tasks to classes or objects in Java OOP.
While working with OOP, we create several classes for different tasks. However, we can’t decide which class will do which work, and hence, the code becomes confusing and hard to maintain.
To remove such confusion, we have to use the GRASP. If we use the GRASP, the code will become clear, logical, and easier to maintain. That can be done by following the Principles of GRASP.
What Are The GRASP Principles Checklist In Java?
Now, after clearing the definition of the GRASP, it is time to move ahead to the next section. In this section, we will let you know about the Principles of the GRASP, which are very important to understand.
There are 9 Different Principles present in GRASP, most of which are Concept-oriented. Let us check the Principles of the GRASP in the following checklist one by one.
1. Information Expert:
The very first Principle in GRASP talks about the Information Expert. That means, in the code, we have to make a Class an Information Expert, which will have all the data.
In simple terms, we have to give responsibilities to a class that has the most important information in the code. Let us understand the principle with the following code snippet.
class Student // Student Class
{
private List gd;
public Student(List gd)
{
this.gd = gd;
}
public double GPA() // Function To Calculate GPA
}
The above code can be used to calculate the GPA of any student. In most cases, we implement the function in the Main. Instead of that, as the Student Class has all the data, we can create that function inside.
2. Creator:
The Creator Principle says that a Class can only create the Instances that it contains. A Class can also create instances that are closely related to each other.
If we follow the Creator Principle, the code will look nice, and the logic can be understood without issues. Also, it helps to improve the Clarity and Coupling of the code.
class Assignment // Class Assignment
{
private String zap;
public Assignment(String zap)
{
this.zap = zap;
}
}
class Course // Class Course
{
private List agn = new ArrayList<>();
}
Here, the Class Assignment and Class Course are closely related. So, we can create instances of Class Assignment in the Class Course. This demonstrates the Creator Principle.
3. Controller:
According to the Controller Principle, there should be a class that will control the entire code. The Controller Principle states that, based on the need, the class can pass the Control to other classes.
In any Java Code, if there is a Class that is controlling the User Input or Graphical Interface, then that class can easily be made a Controller by following this GRASP Principle.
class Controller
{
private Course zap;
public Controller(Course cr) // Accepting The Input
{
this.cr = cr;
}
public void Add(String Name) // Function Taking Input
{
cr.Add(Name);
}
}
In the above code snippet, the Controller Class is taking the User Input and working on it. So, we can say the Class is following the GRASP Controller Principle.
4. Low Coupling:
Now, it is time to understand the Low Coupling Principle. The Low Coupling Principle tells us that, in Java Code, each Class should depend on other classes as little as possible.
As this principle ensures that the Classes should depend less on each other, the Coupling between the classes will be less. That is the reason; it is known as the Low Coupling Principle.
If we follow this principle, the changes in one class will not affect the other classes as they are less dependent.
5. High Cohesion:
The next GRASP Principle is about High Cohesion. High Cohesion Principle says that a Class should have a focused responsibility and can only do a single task.
According to the High Cohesion Principle, we can’t force a Class to work multiple tasks. Whatever the name of the Class is given based on that, we have to provide its task.
The High Cohesion makes the code Easy to read and maintain, which is very necessary for modifications.
6. Polymorphism:
The next principle will talk about Polymorphism. Here, we will define the same polymorphism that is considered one of the important pillars of Java OOP. The GRASP places more importance on it.
As per the Polymorphism Principle, we can use different behaviors of a Class by making it a Common Interface or Base Class. This helps to change the Class Functionality without changing the entire code.
interface Submission // Common Interface With Function
{
void zap();
}
class OnTime implements Submission // On Time Polymorphism
{
public void zap()
{
System.out.println("On Time");
}
}
class Late implements Submission // Late Polymorphism
{
public void zap() {
System.out.println("Late");
}
}
In the above example, the Submission() Common Interface is developed. Now, two Polymorphisms are created for On-Time and Late Submissions. Both are working differently on the Common Interface.
7. Pure Fabrication:
The Pure Fabrication Principle tells us that, in our Java Code, there should be some Classes that will separate the responsibilities between two or more classes. This will make the code cleaner.
The Class that is being used as the Pure Fabrication in the Java Code may or may not have the necessary logic there. We can implement the Pure Fabrication Principle with a Class without any domain.
8. Indirection:
Principle Number 4 was stated about the Low Coupling. To do the Low Coupling, we have to follow the GRASP Principle Number 8, which is the Indirection Principle.
The Indirection Principle States that no two or more classes can get direct coupling in the Java Code. We have to use an Intermediary to avoid the Direct Coupling in the code.
class Manager
{
public void Assign(Student s, Assignment a, double grd)
{
s.setGrade(a, grd);
}
}
In the above code snippet, the Manager() Class or Intermediator is defined, which will avoid the Direct Coupling between the Student Class and the Assignment Class.
9. Protected Variations:
Last but not least GRASP Principle will talk about the Protected Variations. The Protected Variations Principle states that the Java Application should be designed in such a way that modifications become easier.
According to the Protected Variations Principle, the Java Application Design should protect the rest of the parts of code that is not going through modification or alteration.
The Protected Variations Principle makes sure that, after modification of the application, it should work.
What Is SOLID In Java Programming?
After discussing the GRASP, it is time to understand the SOLID. The SOLID is the main foundation on which the Software Design is done. The SOLID involves Code Implementations.
The SOLID helps to write code in such a way that Modification of the Code becomes easier. In the future, if we want to add a new feature to a code or fix a bug, it will be easily done if the SOLID is followed.
SOLID is the Acronym that is developed from the First Words of its principles.
What Are The SOLID Principles Checklist In Java?
After clarifying the definition of SOLID, it is time to understand the principles of SOLID, such as the GRASP. In this section, we will let you know the Principles of the SOLID.
There are five different principles in the SOLID. Let us check the following checklist, where we have discussed the SOLID Principles one by one with their code snippet as examples.
1. Single Responsibility Principle (S):
The “S” in the SOLID comes from the Single (S) Responsibility Principle. The Single Responsibility Principle states that a Class should be modified for a Single Need at a time.
That means a programmer should have a Single Reason to change the class. This principle indirectly says that a Class should have a Single Responsibility, which resonates with the High Cohesion Principle of GRASP.
class Student // Class Student With Student Data
{
private String name;
private int id;
}
We can consider the above example where a Student Class only has the Student Data. It is not making any more operations.
2. Open And Closed Principle (O):
The Next Principle of SOLID is the Open and Close Principle. The Open and Close Principle states that Software Entities, like the Classes and Methods, are open to modification through Extensions.
However, the Classes and Methods are Closed for Direct Modification. This SOLID Principle follows the GRASP Polymorphism Principle. We have to create the extensions by creating the Common Interfaces.
interface Submission // Common Interface With Function
{
void zap();
}
class OnTime implements Submission // On Time Extension
{
public void zap()
{
System.out.println("On Time");
}
}
Like the above code snippet, we have created an extension “Late” of the Submission Class where we can do the Modifications.
3. Liskov Substitution Principle (L):
Now, it is time to understand the Liskov Substitution Principle of SOLID from where the “L” letter is coming. The Liskov Substitution Principle talks about the use of Inheritance in the code.
The Liskov Substitution Principle states that whenever there is a need to use a Class for its behavior, we have to use its Subclasses or Child Classes. So, this principle states about the Parent-Child Class relation.
class Student // Parent Class
{
void zap()
{
System.out.println("Studnet");
}
}
class NewStudent extends Student // Child Class
{
void zap()
{
System.out.println("New Student");
}
}
In the above code snippet, whenever we have to access the behaviors of the Parent Class Student, we can access its Child Class NewStudent.
4. Interface Segregation Principle (I):
The next principle of SOLID is the Interface Segregation Principle. From the Interface Segregation Principle, the word “I” comes in the SOLID. This principle is the most important one.
The Interface Segregation Principle states that we have to create simple and short classes in the Java Code. Instead of creating a Large Class that will work on multiple tasks, we should focus on Segregated Classes.
The Interface Segregation Principle will increase the Code Readability and Maintainability.
5. Dependency Inversion Principle (D):
The Dependency Inversion Principle tells us that a Class should not depend on other classes. Rather, it can depend on the Abstraction or Interfaces of different classes.
The Dependency Inversion Principle somehow resonates with the Low Coupling Principle of the GRASP. This SOLID Principle also follows the Polymorphism Technique to work on.
interface Grade // Grade Class Interface
{
double Calculate(Student s);
}
class Report
{
private Grade calculator; // Creating The Instance
}
Case Study On GRASP And SOLID Usage In Java Project:
We hope the principles of the GRASP and SOLID should become clear to you after such extensive discussion. Now, we would like to share a Case Study to make you understand the usage of GRASP and SOLID.
Here, we will take a Real-life Project Example. On that, we will show you the usage of GRASP and SOLID Principles. So, let us start with the Project Title first.
Project Title:
We have to create a Project on a Simple Calculator using Java. The Calculator will only perform Simple Addition and Subtraction. The code with GRASP and SOLID will be like the following.
import java.util.Scanner;
// Open And Closed Principle In SOLID
interface Operation
{
double exe(double a, double b);
}
// Single Responsibility Principle For Addition
class Add implements Operation
{
public double exe(double a, double b) {
return a + b;
}
}
// Single Responsibility Principle For Subtraction
class Subtract implements Operation
{
public double exe(double a, double b)
{
return a - b;
}
}
// Calculator Is The Information Expert Of GRASP
class Calculator
{
public double zap(Operation one, double a, double b)
{
return one.exe(a, b);
}
}
// The Main Class Is The Controller Of GRASP
public class Main
{
public static void main(String[] args)
{
Scanner sg = new Scanner(System.in);
Calculator cl = new Calculator(); // Creating Calculator Object
System.out.print("Enter First Number: ");
double a = sg.nextDouble();
System.out.print("Enter Operation (+, -): ");
String sy = sg.next();
System.out.print("Enter Second Number: ");
double b = sg.nextDouble();
Operation one;
switch (sy) // Calling The Interfaces
{
case "+": one = new Add(); break;
case "-": one = new Subtract(); break;
default:
System.out.println("Invalid Request");
return;
}
double coding = cl.zap(one, a, b); // Calling The Information Expert
System.out.println("Result Is: " + coding);
}
}
GRASP Principles Checklist:
- The Calculator Class is the Information Expert that accepts all the Data and the Operation.
- The Main Class is the Controller because it takes input from the user and decides the operation.
- The Calculator Class and the Operation Interface are loosely connected using Low Coupling.
SOLID Principles Checklist:
- Single Responsibility can be seen for each operation, like Addition, Subtraction, etc.
- We can add New Operations like Multiplication and Division by creating extensions.
- The Operation Interface is simple and clean, which follows the Interface Segregation principle.
Final Outcome:
As we have followed the GRASP and SOLID Principles to develop the Java Project, the Project Code is now Easy to read, Test, and maintain. Also, we can easily extend the code without issues.
Conclusion:
In the end, we can say that it is very important to understand the “GRASP and SOLID in Java”. You can master these design principles with the help of a Java tutor at CodingZap.
However, we will advise you to clear the basics of Java Object-Oriented Programming before starting such a complicated topic. If your Java Basics are clear, then you can understand the GRASP and SOLID easily.
Takeaways:
- The GRASP helps to design the Java Code that will be easier to understand.
- Information Expert, Controller, Creator, Low Coupling, etc., are some of the principles of the GRASP.
- The SOLID helps to write the Java Code in such a way, extending the code will become easier.
- Single Responsibility, Open-Closed, Liskov Substitution, etc, are some SOLID Principles.
- The SOLID and GRASP Principles are used to increase Code Readability, Maintainability, and Testing.


