燕郊网站制作多少钱,网站编辑难做吗,网站多数关键词,网站怎么做外联单例设计模式
目的#xff08;使用场景#xff09;
在实际开发下#xff0c;会存在一种情况#xff1a;某一种类在程序的整个生命周期中#xff0c;只需要实例化一次就足够了。例如#xff0c;系统数据类#xff0c;由于操作系统只有一个#xff0c;因此在程序初始化…单例设计模式
目的使用场景
在实际开发下会存在一种情况某一种类在程序的整个生命周期中只需要实例化一次就足够了。例如系统数据类由于操作系统只有一个因此在程序初始化时该类只需要实例化一次之后的系统数据更改都是在这一个实例化对象中进行就可以。
功能
主要是一种控制实例化对象产生个数的设计操作。
方法
禁止主程序类中通过 new 实例化对象 正常来说定义了类之后如果在主程序类之中利用 new 进行实例化的话其个数是无法进行限制的。
class Singleton {public void print() {System.out .println(Hello,World)}
}
public class JavaDemo {public static void main(String args[]) {Singleton instanceA new Singleton();Singleton instanceB new Singleton();Singleton instanceC new Singleton();instanceA.print();instanceB.print();instanceC.print();}
}那么首先就应该禁止单例类在主程序类中的实例化我们知道在主程序中的实例化是通过调用构造方法来实现的。而所有的类又默认提供的无参构造因此首先应在类定义中私有化构造方法。 private Singleton() {} // 构造方法私有化 这样我们在若依然主程序类中使用 new 来实例化对象时就会报错 错误: Singleton() 可以在 Singleton 中访问 private instance new Singleton(); 实例化放在单例类中进行 无法在外部类中直接使用 new 进行实例化后实例化过程只能放在自身类中进行因此可以定义一个私有化成员属性即声明一个单例类引用。 private Singleton instance; 但外部类依然要有途径来进行访问因此可以使用 static 属性进行标注 private static Singleton instance; 又要求控制其数量只能有一个因此使用 final 定义 private static final Singleton INSTANCE new Singleton();
该引用的赋值放在一个方法中该方法也要定义为类方法
public static Singleton getInstance() {return INSTANCE;
}两种单例设计模式
饿汉式
在系统加载类的时候就会自动提供有 Singleton 类的实例化对象。 方法示例中所实现的就是饿汉式
class Singleton {private static final Singleton INSTANCE new Singleton();private Singleton() {} // 构造方法私有化public static Singleton getInstance() {return INSTANCE;
}public void print() {System.out .println(Hello,World)}}
public class JavaDemo {public static void main(String args[]) {Singleton instance null; // 仍可声明对象instance Singleton.getInstance();instance.print();}
}
懒汉式
在第一次使用的时候才进行实例化对象处理. 这样在定义时就不能使用 final 来限制个数了而是在获取实例方法中增加判断来限制个数。
class Singleton {private static Singleton instance;private Singleton() {} // 构造方法私有化public static Singleton getInstance() {if (instance null){ // 第一次使用instance new Singleton() ; // 实例化对象}return instance;
}public void print() {System.out .println(Hello,World)}}
public class JavaDemo {public static void main(String args[]) {Singleton instance null; // 仍可声明对象instance Singleton.getInstance();instance.print();}
}面试题编写一个Singleton程序并说明其主要特点
代码如上可以把懒汉式(后面需要考虑到线程同步问题) 和饿汉式都写上特点:构造方法私有化类内部提供 static 方法获取实例化对象这样不管外部如何操作永远都只有一个实例化对象提供。
多例设计模式
多例设计指的是可以保留有多个实例化对象例如如果现在要定义一个描述性别的类那么该对象只有两个:男、 女。或者描述颜色基色的类可以使用: 红色、绿色、蓝色。这种情况下可以利用多例设计来解决。
class Color { // 定义描述颜色的类private static final Color RED new Color(红色);private static final Color GREEN new Color(绿色);private static final Color BLUE new Color(蓝色);private String title;private Color(String title) { // 构造方法私有化this.title title;}public static Color getInstance(String color){switch(color){case red: return RED;case green: return GREEN;case blue : return BLUE;default : return null;}}public String toString(){return this.title;}
}
public class JavaDemo {public static void main(String args[]) {Color c Color.getInstance(green);System.out.println(c);}
}多例设计与单例设计的本质是相同的一定都会在内部提供有 static 方法以返回实例化对象。