0

i am writing an application for android but i am planning to build this for the pc, too. So i want to reuse as much code as possible. But there are some cases where i have to use platform-specific functions. For example for logging-output and loading resources. I am searching for an easy solution to abstract these features. My current solution is that i have written this class

public class LoggerAndroid{

public static void debug(String tag, String msg) {
    Log.d(tag, msg);
}

public static void error(String tag, String msg) {
    Log.e(tag, msg);
}

public static void info(String tag, String msg) {
    Log.i(tag, msg);
}

public static void warn(String tag, String msg) {
    Log.w(tag, msg);
}

 }

and this one

public class Logger extends LoggerAndroid{

 }

In my code i use expressions like

Logger.info("test", "HelloWorld");

and for writing the pc application i write another log class with the same methods and let Logger inherit from that one. So there are very few changes to make and using the log-methods is still easy. But i think maintaining the code and be quite heavy-handed. Are there better solutions for that?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Jay
  • 87
  • 1
  • 1
  • 6

2 Answers2

3

I think an interface would operate better.

Take a look at this code:

public interface ILogger {
    public void debug(String tag, String msg);

    public void error(String tag, String msg);

    public void info(String tag, String msg);

    public void warn(String tag, String msg);
}

public class AndroidLogger implements ILogger {

    @Override
    public void debug(String tag, String msg) {
        Log.d(tag, msg);
    }

    @Override
    public void error(String tag, String msg) {
        Log.e(tag, msg);
    }

    @Override
    public void info(String tag, String msg) {
        Log.i(tag, msg);
    }

    @Override
    public void warn(String tag, String msg) {
        Log.w(tag, msg);
    }

}

Then, all you have to do is:

ILogger log = new AndroidLogger();

And in windows, that'd be:

ILogger log = new WindowsLogger();

This code makes it have no difference at all. Is this what you meant in your question?

Jong
  • 9,045
  • 3
  • 34
  • 66
  • +1, but please don't call it ILogger. I'd also remove the "tag" part and have that be part of the log factory call as most Java log implementations do, and get that part out of the mainline code. – Dave Newton Oct 31 '11 at 18:51
  • I've actually never used any type of logging for PC, so I'm not sure how it goes. – Jong Oct 31 '11 at 18:58
  • Thank you for your Answer! I thought about such a solution but i think it does not remove the disadvantages. Additionally i did not want to create a class because the Android Log instance can be accessed in a static way and i wanted to keep this – Jay Oct 31 '11 at 19:20
0

Favor Composition over Inheritance. Use Inversion of Control and the Strategy Pattern to hot swap out the Logger instance. For an illuminating example, see this on the related concept of toString().

Community
  • 1
  • 1