一、
1.单例模式:单例模式可以保证在同一时刻,某个类的实例(对象)只有1个
饿汉式: 一开始就已经创建好了`King`的对象,随时可供调用/获取public class King public class King { private static King king = new King(); private King() { } public static King getInstance() { return king; } } 懒汉式: 它的特点是“不到逼不得已的时候,并不会创建对象” public class King { private static King king = null; private static final Object lock = new Object(); private King() { } public static King getInstance() { if (king == null) { sychronized (lock) { if (king == null) { king = new King(); } } } return king; } }