Home » Software Development » Why Abstraction is Really Important

About Eyal Golan


Why Abstraction is Really Important


Abstraction is one of the key elements of good software design. It helps encapsulate behavior. It helps decouple software elements. It helps having more self-contained modules. And much more.

Abstraction makes the application extendable in much easier way. It makes refactoring much easier. When developing with higher level of abstraction, you communicate the behavior and less the implementation.





Case study description

Let’s assume that we have a domain object called RawItem.

public class RawItem {
    private final String originator;
    private final String department;
    private final String division;
    private final Object[] moreParameters;

        this.originator = originator;
        this.department = department;
        this.division = division;
        this.moreParameters = moreParameters;

The three first parameters represent the item’s key. I.e. An item comes from an originator, a department and a division. The “moreParameters” is just to emphasize the item has more parameters.

This triplet has two basic usages:

  1. As key to store in the DB
  2. As key in maps (key to RawItem)

Storing in DB based on the key

The DB tables are sharded in order to evenly distribute the items. Sharding is done by a hash key modulo function. This function works on a string.

then we’ll distribute the items based on some function and modulo:

// Representation of the key is described below
int shard = func(key)%N;

Using the key in maps

The second usage for the triplet is mapping the items for fast lookup. So, when NOT using abstraction, the maps will usually look like:

// Fill the map...

“Improving” the class


// new member
private final String key;

// in the constructor:

// and a getter
public String getKey() {
  return key;

Assessment of the design

There are two flows here:

  1. Coupling between the sharding distribution and the items’ mapping

And then comes a new requirement

Up until now, the triplet: originator, department and division made up a key of an item. But now, a new requirement comes in. A division can have subdivision. It means that, unlike before, we can have two different items from the same triplet. The items will differ by the subdivision attribute.

Difficult to change

Regarding the DB distribution, we’ll need to keep the concatenated key of the triplet. We must keep the modulo function the same. So distribution will remain using the triplets, but the schema will change and hava ‘subdivision’ column as well. We’ll change the queries to use the subdivision together with original key.


Abstraction of the key

Let’s create ItemKey

public class ItemKey {
    private final String originator;
    private final String department;
    private final String division;
    private final String subdivision;

        this.originator = originator;
        this.department = department;
        this.division = division;
        this.subdivision = subdivision;

    public String asDistribution() {


// Fill the map...
// new constructor for RawItem
    // fill the fields

Lesson Learned and conclusion

I wanted to show how a simple decision can really hurt.

And, how, by a small change, we made the key abstract. In the future the key can have even more fields, but we’ll need to change only the inner implementation of it. The logic and mapping usage should not be changed.

Regarding the change process, I haven’t described how to do the refactoring, as it really depends on how the code looks like and how much is it tested. In our case, some parts were easy, while others were really hard. The hard parts were around code that was looking deep in the implementation of the key (string) and the item.

This situation was real

We actually had this flow in our design. Everything was fine for two years, until we had to change the key (add the subdivision). Luckily all of our code is tested so we could see what breaks and fix it. But it was painful.

There are two abstraction that we could have initially implement:



So, do abstraction, tell a story and use the interfaces and don’t get into details while telling it.


Subscribe to our newsletter to start Rocking right now!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....



  1. Great post, is really useful for novice and expert programmers, thanks for sharing.

  2. Thanks !

    Glad you liked it.

Leave a Reply



Want to take your Java Skills to the next level?
Grab our programming books for FREE!
Last Step ...

Where should we send the free eBooks?

Good Work!