Abstraction vs Encapsulation: Can You Quickly Tell What the Difference Is? same? different? tutorial

preview_player
Показать описание
#coding #codingbootcamp #softwaredeveloper

Summarizing the Differences between Abstraction and Encapsulation
Encapsulation ensures that the state of an object is always valid and consistent. Abstraction ensures that consumers have the simplest possible interface to work with an object.
Abstraction can be implemented without implementing encapsulation and vice versa.
Encapsulation is a qualitative unit (Is object encapsulated? — Yes or No), while abstraction is a quantitative one (How easy is the interface to use? —Easy, Not so easy, Hard, Quite Hard…).

Encapsulation and abstraction are fundamental programming concepts. Understanding and applying them regularly can greatly improve the quality of your software and make it more maintainable. However, often the two terms are used interchangeably, even though there is a clear line between them.

I propose to understand the difference between the concepts of encapsulation and abstraction, without resorting to any Wikipedia definitions or smart words, but simply using small code examples.

What Exactly is Encapsulation?
Encapsulation helps to maintain the state of an object in a valid and consistent state.

Is the Email object properly encapsulated? Definitely no. Consumers (any code that deals with an object) of Email object can literally set EmailAddress property not only to a valid email address, but to any value allowed by a string type in C#: null, empty string, ‘qwerty’ etc.

This is a problem because anyone who will be using the Email type must always validate the EmailAddress property to make sure there is always a valid value. Even if the Email object now contains a valid email address, there is no guarantee that no one will later set an invalid value.

How about allowing the object to be initialized only once on creation so that no one can change the object property after it has been created?

The setter is private, and the only way to initialize an object is to pass an email address to the constructor. This is better than the public setter, but encapsulation is still not achieved. Consumers can still set the invalid email value during object creation.

In order to achieve proper encapsulation in our case, the Email type itself must maintain its data in a correct and consistent state, as in the following example:
Encapsulation is achieved! Now an object cannot be created in an invalid state, and the state cannot be changed from valid to invalid by external code for the lifetime of the object.

Objects shouldn’t necessarily validate the data in constructors and throw exceptions if the data is bad. There are some other ways to maintain the valid and consistent state of the object, such as accepting already valid data, returning true/false or instead of throwing exceptions, validating data in methods, and so on.

What Exactly is Abstraction?
The principle of abstraction is about creating and exposing a simple interface to the consumers and hiding unnecessary details from them.

IFileParser interface can be used by consumers who need to parse some JSON file content to an object. However, this interface is not really convenient because consumer can forget to call Exists method, or pass the wrong Stream object to the method, forget to call Dispose method, mix up the call sequence and so on.

In addition, the interface exposes unnecessary implementation details of file parsing to the consumers, such as knowledge about Stream type.

The interface probably does abstract some details from the consumers, such as opening files, reading their content, deserializing it into an object. However, there is much more room for proper abstraction here, so let’s improve the IJsonFileParser interface:

Now there is only method that the consumers should deal with. All other details that the old interface with 4 methods exposed, such as checking for the existence of a file, creating / disposing streams should be completely hidden from consumers in the implementation of a method. This will greatly simplify the consumer code.
Рекомендации по теме
Комментарии
Автор

What videos would you like to see us make? What would be the most helpful?

STARTUPHAKK_sh