I have an abstract superclass and quite a few subclasses of it. There are multiple instances of each subclass.
Each subclass needs to use some files unpacked from a tar.gz archive. Each subclass has its own separate tar.gz. Even though I have multiple instances of a subclass, I only need to unpack the tar.gz once for all instances of the subclass. I'm using a static boolean variable in each subclass to coordinate this.
Each subclass uses the same (duplicated) code to figure out whether the files have been unpacked before trying to unpack them. What I'm asking: is there a way to move the code to the superclass instead of having a static variable and duplicated code per subclass to figure out whether that subclasses tar.gz has been unpacked yet?
Here's some code to hopefully explain a bit better.
public abstract class SuperClassA
{
public void unpack() throws IOException
{
// default implementation to unpack the files
// without checking if they've been unpacked
}
}
public class SubClassA extends SuperClassA
{
private static Boolean m_firmwareUnpacked = false;
// Ensures the files are unpacked only once for this subclass
@Override
protected void unpack() throws IOException
{
synchronized( m_firmwareUnpacked )
{
if( m_firmwareUnpacked == false )
{
super.unpack();
m_firmwareUnpacked = true;
}
}
}
}
public class SubClassB extends SuperClassA
{
private static Boolean m_firmwareUnpacked = false;
// Ensures the files are unpacked only once for this subclass
@Override
protected void unpack() throws IOException
{
synchronized( m_firmwareUnpacked )
{
if( m_firmwareUnpacked == false )
{
super.unpack();
m_firmwareUnpacked = true;
}
}
}
}
Note each subclass uses a different path to the file to unpack.
As you can see each subclass shares the same code to ensure the archive is just unpacked once for all the instances of that subclass. I'm wondering if there's a good way to move this code to the superclass (but keep the same functionality, i.e. unpacking SubClassA's archive won't stop SubClassB from unpacking its archive)?