2

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)?

4
  • you can use a template method Commented Jul 19, 2013 at 20:04
  • If you want to stick with fields, you could always use reflection. Just have the field declared and this will do the rest: stackoverflow.com/questions/15439730/… Commented Jul 19, 2013 at 20:13
  • So each SubClass has its own file it needs to unpack? Commented Jul 19, 2013 at 20:13
  • Yes, each subclass is unpacking a different file. Once it has been unpacked by one instance of the subclass, it doesn't need to be unpacked again by any of the other instances. Commented Jul 19, 2013 at 20:38

2 Answers 2

2

You can take approach of Template Method

public abstract class SuperClassA
{
    //this is the template method
   public void unpack() throws IOException
   {
             if(isValid()){
                templateUnpack();
                setValid(false);
             }

    }

    protected abstract boolean isValid();

    public void templateUnpack() throws Exception {
        //unpack code here
    }
    protected abstract void setValid(boolean b);

}

And in subclass

public class SubClassA extends SuperClassA
{
    private static Boolean m_firmwareUnpacked = false;

    @Override
    public boolean isValid(){
      return m_firmwareUnpacked;
    }


    @Override
    protected void setValid(boolean b){
       m_firmwareUnpacked = b;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

yes, the static variables of parent can be used by all the child classes as long as it is declared public or protected (not private). Then will maintain consistent value unless one of the child object modifies it.

2 Comments

i think he is not looking for that
If you read carefully, I don't want SubClassA to be able to affect SubClassB's status of the file being unpacked. I know they can all share the same static variable in the superclass. I'm looking for a good way to avoid code duplication in the subclasses as shown above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.