EX00 - Logger

Moderatoren: pmueller, SE - Design and Construction

Benutzeravatar
UdoWeber
Endlosschleifenbastler
Endlosschleifenbastler
Beiträge: 156
Registriert: 8. Nov 2009 15:13

EX00 - Logger

Beitrag von UdoWeber »

Hi,

I have some question:

"The logger should be cached inside of the interface and it should not be looked up every time it is used"

If I understand this right, cached means the logger should be a final static field inside the interface, as in the example?
If yes, I'm not sure how to initialize the log4j logger correctly inside the interface?
How do I get the class which implements the interface? (naturally in this case this.getClass() does not work)

Thanks for your help.
Greets Udo

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: EX00 - Logger

Beitrag von AizazZaidee »

I wrote an initialize method inside an interface and called it in the constructor of the class. I don't know if that's the right thing to do but I cannot think about anything else.

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: EX00 - Logger

Beitrag von AizazZaidee »

How do I get the class which implements the interface? (naturally in this case this.getClass() does not work)
Use static method instead of using this.getClass

Benutzeravatar
UdoWeber
Endlosschleifenbastler
Endlosschleifenbastler
Beiträge: 156
Registriert: 8. Nov 2009 15:13

Re: EX00 - Logger

Beitrag von UdoWeber »

How do I get the class which implements the interface? (naturally in this case this.getClass() does not work)
Use static method instead of using this.getClass
Ok, but then you have a fixed class, but you normally want the class that implements the interface and this class can change.
I wrote an initialize method inside an interface and called it in the constructor of the class. I don't know if that's the right thing to do but I cannot think about anything else.
This seems to be a possible idea, thanks :)

pmueller
Moderator
Moderator
Beiträge: 87
Registriert: 25. Jan 2018 11:14

Re: EX00 - Logger

Beitrag von pmueller »

Hi,

for this task it is fine if the logger is initialized with the Class of the Interface statically.

Best,
Patrick

nknauber
Erstie
Erstie
Beiträge: 11
Registriert: 25. Apr 2018 16:48

Re: EX00 - Logger

Beitrag von nknauber »

Hi,
for this task it is fine if the logger is initialized with the Class of the Interface statically.
So it would be fine to initialize the logger simply with the name of the interface as getting the classname of the class that implements the interface would be "too hard" for this task? Do I understand this correctly? Even though this way, the logmessage will only contain the name of the interface?

Thanks,
Nicolas

pmueller
Moderator
Moderator
Beiträge: 87
Registriert: 25. Jan 2018 11:14

Re: EX00 - Logger

Beitrag von pmueller »

Hi,

yes this correct.

Best,
Patrick

topracer
Mausschubser
Mausschubser
Beiträge: 53
Registriert: 10. Jan 2014 19:14

Re: EX00 - Logger

Beitrag von topracer »

I implemented a nested "cache class" inside the interface which holds the current logger as a static variable. The logger is returned by a static getter method and is only exchanged if the class of the requesting object differs from a previous one. That way, displaying the particular class name works, but the logger is actually not really cached "inside" the interface...

Is this still a valid solution for the task or should I go for the static initialization instead?

Regards,
Frederic

pmueller
Moderator
Moderator
Beiträge: 87
Registriert: 25. Jan 2018 11:14

Re: EX00 - Logger

Beitrag von pmueller »

Hi,

since I have not seen your solution, I think it would be a good Idea to revert to the simpler solution.

Best,
Patrick

Antworten

Zurück zu „Software Engineering - Design and Construction“