GRASP Principles for Android Development — Information Expert | Part 1
The critical design tool for software development is a mind well educated in design principles. It is not a technology.
Many articles explain SOLID principles but few articles explain GRASP. GRASP is actually as powerful as SOLID principles are. But for some reason, software engineers don't pay enough attention to it, especially in the Android Development world.
Even though, if you didn’t learn GRASP principles on purpose, you would probably use some of the patterns without knowing that they were in fact GRASP principles. GRASP principles are pretty easy to understand.
In this series of articles, I will try to show the greatness and simplicity of GRASP principles and why we should apply them as much as possible.
What is GRASP?
GRASP is short for General Responsibility Assignment Software Patterns.
GRASP is a collection of high-level patterns for an OOP that are aimed to help assign responsibilities for modules.
Modules can be:
- Classes
- Packages
- Gradle modules
Craig Larman firstly described GRASP principles in his book “Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development”. He published the book 3 years after GoF patterns.
GRASP principles became the generalization of GoF patterns and implementation of OOP.
GRASP principles consist of:
- Information Expert
- High Cohesion
- Low Coupling
- Polymorphism
- Pure Fabrication
- Creator
- Indirection
- Controller
- Protected Variation
Information Expert
Problem: What is a basic principle by which to assign responsibilities to objects?
Solution: Assign responsibility to the class that has the information needed to fulfil it.
What it means: Information must be processed there where it is. Calculate information in the methods of a class.
Android-World Example:
Let’s say you are developing an E-commerce application and you need to calculate the approximate price of all products in the cart.
The approximate price of all products in the cart must be shown on the screen.
So where method that calculates the approximate price of all products should be put?
According to The Information Expert principle, we should look for a class that has all the necessary information to calculate the approximate price of the product in the cart.
The Cart
class itself has all the necessary information to do it. So that the method that calculates the approximate price of all products should be put in the Cart
class.
Pretty simple, isn’t it?
To fully understand the greatness of The Information Expert let’s violate the principle and see how we might end up.
Putting the method in ViewModel
Advantages:
- The task can be finished quickly.
Disadvantages:
- If a screen consists of a lot of views and has complex logic, ViewModel becomes huge and it is impossible to write and maintain unit tests for ViewModel.
- Code duplication. The same logic might be required in another part of an application.
Putting the method in CartInteractor
Advantages:
- Solves code duplication problem that we faced in the ViewModel case.
- The task can be finished quickly as well.
- Easy to write and maintain unit tests.
Disadvantages:
- Additional dependencies. Every time client’s code depends on
Cart
class, it will depend onCartInteractor
Compare:
Interactor
might become huge.Interactor
might have a lot of public and private methods that are used for different purposes e.g. public methods that send requests to a server, database and public methods that calculate and return new data that are based on the domain model. This case violates The Single Responsibility Principle.
How to Eliminate violation of The Information Expert?
- Look for a piece of logic that does calculations based on members of a class outside of the class
- Move that logic in the class
Take a look at the code snippet below. This snippet violates The Information Expert principle.
Let’s fix that! We should move logic to the Product
class.
Conclusion
Information Expert, resulting from encapsulation, is one of the fundamental principles of GRASP.
Violation of The Information Expert is easy to identify and eliminate.
Applying Information Expert makes it easier to understand the code, eliminates code duplication and reduces the number of relationships between classes.
Related Principles: Encapsulation, Don’t repeat yourself, Law of Demeter.