The Knowledge Principle in the context of GRASP is known as the Information Expert principle.
It guides the assignment of responsibilities to classes in object-oriented design by stating that a responsibility should be given to the class that has the most relevant information needed to fulfill it.
Overview of the Information Expert Principle
The Information Expert principle states: assign a responsibility to the class that has the necessary information to fulfill it. This helps minimize dependencies between classes, leading to code that is easier to maintain and more robust. (Low Coupling) For instance, if an Order class contains all the order items, the Order class should calculate the total price, since it has all the necessary data. Why the Information Expert Principle Matters
By following this principle, you keep related data and behavior together, resulting in high cohesion and low coupling in your system. It improves encapsulation, because the knowledge needed to perform operations is localized within the class itself rather than scattered throughout the codebase. Example
If you are designing a library system:
When a user wants to check if a book is available, the Book class should be responsible for that, since it knows its own availability status. This reduces the need for other components to access or manipulate the internal state of the Book object, thus reducing coupling and increasing maintainability. In summary, the Knowledge Principle (Information Expert) helps determine where to assign responsibilities based on where the needed knowledge is already present, streamlining object-oriented system design.