Software Engineering

Adapter Pattern applied

When you’re applying an Adapter Pattern, you’re generally grappling with classes and method signatures. I just had to deal with packages and package visibility in Java where I found the Adapter Pattern to be an interesting solution.
I was accessing a class A in a package com.example from the outside (an RMI call). The method I called had to return an instance of a class B in a subpackage, say com.example.sub, which in turn had to access a class C in package com.example. The trouble with class C was that it had only package-visible methods so that I could not call them from class B which was in a subpackage. Exposing C’s method to the public wasn’t desireable at all, so changing the visibility wasn’t a choice.
I wrote an adapter class in package com.example which had publicly visible methods but a package-visible constructor. Now I was able to instantiate class B in class A and provide an instance of my adapter class which in turn received a reference to class C and then delegated all calls to C. This way, I didn’t reveal C’s methods to the public, since my adapter class could only be instantiated in the package of class A, com.example.