java - Should I use inheritance or composition in my case? -
i'm creating class shares common codes class , not sure pattern should use. class i've have:
public class teama{ private static final logger logger = logger.getlogger(teama.class); @autowired private utility util; public void proceedwitha(){ // useful here updateprogress(); } private void updateprogress(){ logger.info("updating progress!"); // update progress } }
the class teamb i'm creating same thing class teama except in proceedb()
different before calling updateprogress()
.
public class teamb{ private static final logger logger = logger.getlogger(teamb.class); @autowired private utility util; public void proceedwithb(){ // useful here updateprogress(); } private void updateprogress(){ logger.info("updating progress!"); // update progress } }
so @ first i'm inclined use inheritance creating super class team
them extend from:
public class team{ // how should define logger? @autowired private utility util; protected void updateprogress(){ // logger.info("updating progress!"); // update progress } }
how should use logger? in class teama/b defined private static final
, apparently can't same in superclass since want logger teama , teamb respectively.
i thought composition. seems have pass logger parameter updateprogress
method. ok or there better way?
public class teama{ private static final logger logger = logger.getlogger(teama.class); @autowired private team teamutil; public void proceedwitha(){ // useful here teamutil.updateprogress(logger); } } public class team{ @autowired private utility util; protected void updateprogress(logger logger){ logger.info("updating progress!"); // update progress } }
i'm new design patterns , logger thing getting me confused. give me advice please? :)
public abstract class team{ protected abstract logger getlogger(); protected void updateprogress(){ getlogger().info("updating progress!"); // update progress } }
your child classes can implement getlogger return static final logger
.
Comments
Post a Comment