Skip to content

Java 最佳实践与设计模式

编码规范

  • 使用有意义的变量名。
  • 遵循 Java 命名约定。

常见设计模式

  • 单例模式:确保一个类只有一个实例。
  • 工厂模式:提供创建对象的接口。

示例

java
// 单例模式示例
public class Singleton {
    private static Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}