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