A brilliant example of effective encapsulation through information hiding?

后端 未结 5 931
旧时难觅i
旧时难觅i 2021-02-04 12:13

\"Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation fo

5条回答
  •  无人及你
    2021-02-04 12:48

    The example given in my first OO class:

    Imagine a media player. It abstracts the concepts of playing, pausing, fast-forwarding, etc. As a user, you can use this to operate the device.

    Your VCR implemented this interface and hid or encapsulated the details of the mechanical drives and tapes.

    When a new implementation of a media player arrives (say a DVD player, which uses discs rather than tapes) it can replace the implementation encapsulated in the media player and users can continue to use it just as they did with their VCR (same operations such as play, pause, etc...).

    This is the concept of information hiding through abstraction. It allows for implementation details to change without the users having to know and promotes low coupling of code.

提交回复
热议问题