简体   繁体   中英

Logging class with static members

I created a class that roughly looks like this.

public class CodingLogger {
    private static StringBuffer buffer = new StringBuffer();
    public static void println(Object o) {
        System.out.println(String.valueOf(o));
        buffer.append(String.valueOf(o) + LINE_SEPARATOR);
    }
}

Is there a specific name for this kind of class (accessible everywhere, without instance)? Or this is typical Utility pattern?

There's no name for such a class.

This is not the Singleton, because there is no problem with creating an instance of the class.

You should ask yourself why you need such a class? What is the purpose? Why it is static? Because it is easier to use without injection? If so, you are doing nothing more, but hidding dependencies. You need to know the motivations and double check whether the reasons are good.

Remember that you should write Object -Orieted Code. If there is no instance of the class you cannot name it Object-Oriented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM