Bundling (encapsulation)


Bundling as a part of encapsulation is a process of gathering in one place the related things. What this means? Let’s see.

Data and methods bundling

Data and methods are gathered in place called … class. What a discovery 🙂

class Person {
  String name, surname; //data
  public getFullName () { //method
     return name + surname;
  }
}

Ok, but, what data and which methods should be put/gather into class? The answer is not simple. For now, let’s assume that class contains a data (related properties) and methods that operate on that data. What are related properties? Properties in some meaningful relation, like in the example above: name and surname.

That simple rule is often broken in many projects. Let’s see that examples:

class Person {
  static String getAddressAsString (Address address) {
     return address.city() + address.street() + address.no();
  }
}

The Person’s getAddressAsString method operates on another concept (address) properties. This is bad bundling. Method should operate on properties on his own class.

How to make it better?

By gathering related methods and properties together in appropriate concept/class:

class Address {
   private String city, street, no;
   String getAddressAsString () {
     return city + street + no;
  }
}

Now the getAddressAsString() method operates on data of the same class. Correct bundling.

Another, typical example you can encounter in spring applications:

@GetterAndSetter
class Person {
   private name, surname;
}

@Service
class PersonService {
   
   String getFullName (Person person) {
      return person.getName() + person.getSurname();
   }
}

Class Person is a structure – contains only fields with getters and setters generated by @GetterAndSetter annotation, but do not contains logic. PersonService class has a logic that use Person class properties. Bundling is not good.

How to make it better?

Move logic/methods where the data/properties are defined:

class Person {
   private name, surname;
   String getFullName () { name + surname; }
}

Great. That simple ‘bundling’ provides a lot of benefits: clarity in code, avoiding duplication and avoiding code scattering.

Of course the @Service classes have a lot of logic and use many different classes. It does not means that you should move all the logic from Service’s, but the one that clearly belongs to other class.

Not always clear …

In above examples you saw that methods were in not proper place. But, very often we have a lot of code scattered around:

...
  Person person = getPerson();
  report.setName (person.getName() + person.getSurname())
... 
  delivery.recipientName (person.getName() + person.getSurname())
...

You may noticed that the same logic, the same information about full name is used in many places. You should to identify that logic and put where fit the best. Then usage will be more clear and straightforward:

...
  Person person = getPerson();
  report.setName (person.getFullName())
... 
  delivery.recipientName (person.getFullName())
...

We avoid duplication, maintenance is easier. Also, testing this extracted logic is very simple.

Classes bundling

Definition of encapsulation mention about data and methods bundling, but bundling has wider meaning and applicability.

Encapsulation is a technique that encourages decoupling. … A supposed benefit of encapsulation is that it can reduce system complexity, and thus increase robustness, by allowing the developer to limit the interdependencies between software components”

We should to use bundling on higher level – on package level. Here we have such example:

The payment package contains classes relate to payment functionality. This functional way of grouping classes brings a lot of benefits. We discuss it in ‘packaging by functionality’ post. But to summarize, we can repeat from the definition – reduce system complexity by lowering decoupling.

Packages bundling

Bundling should also be applied to packages, to bundle module/artifact:

Here we have a packages that together they provide some bigger functionality/feature.

Why bundling is so important?

Bundling increase the cohesiveness of class. You saw it on examples that we moved method to the class where the used properties are defined.

cohesion refers to the degree to which the elements inside a module belong together
https://en.wikipedia.org/wiki/Cohesion_(computer_science)

Bundling reduce the coupling. You should believe me that bundling reduce coupling or you can check by applying the bundling/encapsulation.

In software engineeringcoupling is the degree of interdependence between software modules;
https://en.wikipedia.org/wiki/Coupling_(computer_programming)

High cohesion and low coupling are most fundamental aspects of high quality code.

Summary

The bundling is a process of moving and gathering related things together. By doing that we increase cohesiveness and lowering coupling. In summary, bundling is a tool to clean the mess in many projects

thx

,

Leave a Reply

Your email address will not be published. Required fields are marked *