0

I'm creating a class which shares common codes with another class and am not sure which pattern I should use. The class I've already have:

public class TeamA{
    private static final Logger LOGGER = Logger.getLogger(TeamA.class);

    @Autowired
    private Utility util; 

    public void proceedWithA(){
        // do something useful here
        updateProgress();
    }

    private void updateProgress(){
        LOGGER.info("Updating progress!");
        // do something to update the progress
    }
}

The class TeamB I'm creating does almost the same thing as class TeamA except that in proceedB() it does something different before calling updateProgress().

public class TeamB{
    private static final Logger LOGGER = Logger.getLogger(TeamB.class);

    @Autowired
    private Utility util;

    public void proceedWithB(){
        // do something useful here
        updateProgress();
    }

    private void updateProgress(){
        LOGGER.info("Updating progress!");
        // do something to update the progress
    }
}

So at first I'm inclined to use inheritance by creating a super class Team for them to extend from:

public class Team{
    // How should I define the Logger?

    @Autowired
    private Utility util;

    protected void updateProgress(){
        // LOGGER.info("Updating progress!");
        // do something to update the progress
    }
}

How should I use the Logger? In class TeamA/B it is defined as private static final, but apparently I can't do the same in the superclass since I want a logger for TeamA and TeamB respectively.

I also thought about composition. But it seems that I have to pass the Logger as a parameter to the updateProgress method. Is this OK or is there a better way?

public class TeamA{
    private static final Logger LOGGER = Logger.getLogger(TeamA.class);

    @Autowired
    private Team teamUtil;

    public void proceedWithA(){
        // do something useful here
        teamUtil.updateProgress(LOGGER);
    }
}

public class Team{
    @Autowired
    private Utility util;

    protected void updateProgress(Logger LOGGER){
        LOGGER.info("Updating progress!");
        // do something to update the progress
    }
}

I'm new to design patterns and this logger thing is getting me confused. Could someone give me some advice please? Thanks :)

2 Answers2

0
public abstract class Team{
    protected abstract Logger getLogger();

    protected void updateProgress(){
        getLogger().info("Updating progress!");
        // do something to update the progress
    }
}

Your child classes can now implement getLogger to return their static final Logger.

Aurand
  • 5,487
  • 1
  • 25
  • 35
0

" protected void updateProgress " location needs to be fixed since its functionality is changing with class. What you can do here is create a abstract super class and make the updateProgress abstract (this is template pattern) . Let the base class give independent functionality for it. this way you would be able to use it either way by sub-class/super-class variable. There is no law that you need to use composition every where , it depends .. in your case you should use template method pattern.

Manish
  • 99
  • 8