What is Information Hiding Principle?

Jun Bang
3 min readNov 6, 2020

--

Definition of Information Hiding principle

Information Hiding hides the design decision of a component that is likely to change in the future from other components who depends on it. As such, the programmer only needs to know the input required by the functions in the component and the expected output that the functions will produce. This also means that the programmer does not need to know how the functions are implemented to use them.

How can Information Hiding principle be applied?

To illustrate information hiding, we will use a scenario of a House Buyer and a Builder. The buyer wants to build a house of its own, so he employs the construction builder to build it for him. Below is a simplified version of the scenario mentioned.

UML Sequence diagram before Information Hiding applied

From the above sequence chart, you can tell that we are exposing too much information about building a house to the Buyer. The Buyer has to know what functions to call and the sequence of functions to execute to be able to build a house. However, the Buyer should not need to know how a house is built. Also, this makes it difficult to accommodate any changes to the design decision of how a house is being built because whenever you make a change to the design decision, you have to modify Buyer component as well. As such, information hiding principle is not utilized.

Applying Information Hiding principle

As mentioned above, the Buyer component should not need to know how a house is built. As such, we can encapsulate the functions used to build a house into another component called Builder. Below is the modified version.

UML Sequence diagram after Information Hiding applied

From the sequence chart above, you can see that information regarding how a house is built is hidden from the Buyer. The Buyer only needs to know what materials it needs to pass into the Builder component to get a house. If the builder is to change the way it builds the house, the modification will only be done in the Builder component and it will not affect the Buyer component. As such, this structure uses the Information Hiding principle.

Importance of Information Hiding principle

Flexibility

Information Hiding principle provides flexibility to the programmer to modify underlying design decisions made in a certain component. Since the implementation details are hidden from the users of the component, changes made will be localized to that component only. This also makes debugging and maintaining the program easier.

Information Hiding vs Encapsulation vs Abstraction

These three terms are used commonly when discussing good practices in software development. Although they are similar to each other conceptually, they are not the same. Below is the difference between these three terms.

Information Hiding principle emphasizes on restricting the access to information such as critical design decisions to allow implementations details to be modified without affecting its users. For example, hiding a computation algorithm in a component and allowing users to access its function through an interface.

Encapsulation principle emphasizes on controlling the access to information by grouping or wrapping related items together to hide the complexity. For example, creating a Car component that wraps up all mechanical parts of a car in a single unit.

Abstraction principle emphasizes on hiding unwanted details so that a programmer can focus on information of greater importance. It is usually used in high-level components to give an overview of the component where we abstract the nitty-gritty details about the implementation away from our core components. For example, in the main function of a driver class, it calls a ‘run’ function of a component to get it to work and ‘stop’ function if we want to stop it.

Conclusion & Remarks

Information Hiding principle hides design decisions away from its users, allowing its users to use the components without understanding its underlying implementation and architecture. This makes life a lot easier for a programmer as he does not need to understand how it works. The only thing he has to know is how to use it.

--

--

Responses (1)