Meaning of Leaky Abstraction?

后端 未结 11 1124
面向向阳花
面向向阳花 2020-12-23 02:33

What does the term \"Leaky Abstraction\" mean? (Please explain with examples. I often have a hard time grokking a mere theory.)

11条回答
  •  余生分开走
    2020-12-23 03:29

    Assume, we have the following code in a library:

    Object[] fetchDeviceColorAndModel(String serialNumberOfDevice)
    {
        //fetch Device Color and Device Model from DB.
        //create new Object[] and set 0th field with color and 1st field with model value. 
    }
    

    When the consumer calls the API, they get an Object[]. The consumer has to understand that the first field of the object array has color value and second field is the model value. Here the abstraction has leaked from library to the consumer code.

    One of the solutions is to return an object which encapsulates Model and Color of the Device. The consumer can call that object to get the model and color value.

    DeviceColorAndModel fetchDeviceColorAndModel(String serialNumberOfTheDevice)
    {
        //fetch Device Color and Device Model from DB.
        return new DeviceColorAndModel(color, model);
    }
    

提交回复
热议问题