Personal tools

Abstraction Layers

FitzRandolph Gate, Princeton University
(FitzRandolph Gate, Princeton University - Kimberly Chen)
 

 

- What is Abstraction?

Before discussing layers of abstraction in programming code, let's briefly discuss abstractions in general and what they are. 

Abstraction can be defined as the process of simplifying entities by:

  • Omit unimportant details.
  • Expose an interface.

 

Creating abstraction layers helps improve your code drastically by providing three major benefits: centralization, simplicity and better testing.

All abstractions are similar in this respect. Autonomous cars are a great example of abstraction in the real world. In this case, the clutch is abstracted and the driver can shift gears more easily.

Please refer to the following for more details:

 

- Abstraction Layers

An abstraction layer, or abstraction level, is a way to hide the details of a subsystem in computing. It can also create a separation between two things, such as splitting tasks into separate entities. 

For example, an abstraction layer can split an entity that fetches an image from a database into two entities:

  • Entity A: Fetches the resource
  • Entity B: Depends on the image to return from the first operation to then perform its second operation

 

Abstraction layers can be used in many programming domains, such as APIs. For example, a developer might use Twitter's APIv2 to get tweet data for an application. 

Abstraction layers can also hide the complexity of a system by providing an interface that eases its manipulation. For example, an abstraction layer can abstract away all the different things a program needs to think about when communicating with different banks. 

Here are some examples of abstraction layers:

  • Hardware abstraction layer (HAL): A reusable hardware interface in software that provides an interface between hardware and software so applications can be device independent. HALs are becoming more common in embedded systems and can simplify software development, improve portability, and improve system performance. HAL supports a wide range of hardware devices, including printers, keyboards, mice, network adapters, display monitors, and storage devices.
  • Data abstraction layer: Can consistently apply data quality and validation rules across all data sources, and consistently apply data security rules across all data sources and consumers via a unified security framework.
  • OSI Model" Split into seven abstraction layers: Physical, data link, network, transport, session, presentation, and application. 
  • OS Abstraction Layer (OSAL): A software library that isolates embedded software from the real time operating system. The OSAL provides an Application Program Interface (API) to an abstract real time operating system. 
  • Device Abstraction Layer specification: Provides a unified interface for application developers to interact with sensor, devices, etc. connected to a gateway. Application developers don't have to deal with protocol specific details which simplifies the development of their applications.

 

Here are some other abstraction layers:

  • Applications that you write and use
  • Programming languages that are used to write them
  • Libraries of useful functions that hide messy details
  • Operating systems that interface with the hardware

 

Moscow_Russia_120820A
[Moscow, Russia - elena.krizhevskaya]

- Why Your Code Needs an Abstraction Layer

Abstraction is one of the most important aspects of writing well-designed software. Understanding the basic concepts will give you a system to follow and a clear mental model of how to create good abstractions. 

Good abstraction reduces complexity and allows developers to change code more easily and reduce errors. But creating abstractions is not easy. 

So how exactly do you do this, and what steps do you need to take?

All code can be categorized into either policy or detail.

  • Policy: These are entities and business logic.
  • Detail: This is the implementation of the policy. The detail enforces the policy.


Abstraction layers work by separating two things, often by splitting tasks into separate entities. For example, an entity might be assigned the task of fetching an image from a database, and then processing the image when retrieved. 

This would split the entity into entities A and B, where Entity A fetches the resource, and Entity B depends on the image to return from the first operation to then perform its second operation. 

Abstraction can also help with: problem-solving, efficiency, and scalability.

 

- Benefits to Creating Abstraction Layers

Creating abstraction layers helps improve your code drastically by providing three major benefits: centralization, simplicity and better testing. 

In your code, you want to expose the policy while hiding the detail. This decoupling between your policy and detail allows you to switch and easily refactor implementation. 

If your policy and detail are coupled, you’ll have a hard time refactoring, as they will be mixed and changes will propagate from one to another. 

In a well-designed system, a separation between policy and detail is key.

There are three major benefits to creating abstraction layers:

  • Centralization: By creating your abstraction in one layer, everything related to it is centralized so any changes can be made in one place. 
  • Simplicity: By creating the abstraction layer, you expose a specific piece of functionality and hide implementation details. Now code can interact directly with your interface and avoid dealing with irrelevant implementation details. Because policy is less complex than its details, so interacting with it is more straightforward. 
  • Testing: Abstraction layers are great for testing, as you get the ability to replace details with another set of details, which helps isolate the areas that are being tested and properly create test doubles.

 

Abstraction layers can improve code in several ways, including:

  • Centralization
  • Simplicity
  • Better testing
  • Reduced development time and cost
  • Improved readability and understandability
  • Enhanced maintainability and scalability
  • Increased reliability and security



[More to come ...]


 

 

Document Actions