Wednesday, 4 May 2016

Log interface

Hi Everyone,

I am writing this to explain what is Log class that might help me later for other tutorials.

Purpose:-
Well Log class wil be used to log the information and abstract out the details to print information.
I know we don't require this class as these kind of things are already available in programming languages but this one is for me so that I can abstract out the details to log everything and can clear them with simple interface.

Responsibility:-
Responsibility of  Log will be to store the message and log the information when required.

OOD:-
State:-
Well I'll use one buffer to hole the message stored yet in log.

Behaviour:-
Class must provide its client the services to add logs, delete everything and when asked return everything available in buffer.

I love abstraction but I also feel that I must know what's happening behind the stage.
So once implemented I am satisfied now that in upcoming blog writes I can use this Log without worrying about how it works.

Implementation:-

class Log {
//buffer that will hold the log messages
private StringBuffer message;

//to initialise buffer
public Log() {
      message = new StringBuffer("");
}

//appends the information
public void appendInfo(String info) {
      message.append(info);
}


//clears the log
public void clearLogs() {
       message =  new StringBuffer("");
}

//returns the information available in log
public String getinfo() {
    return message.toString();
}
}

In short, Today I wrote the class which helps to add the log information and give me logs available when demanded.





No comments:

Post a Comment