**单例设计模式**
全局的 静态一实例,私有构造器。
分为:饥汉式 、饱汉式
package com.gg.demo;
public class TestDemo7 {
public static TestDemo7 testDemo = new TestDemo7();
private final String name = "天安门";
private String addr = "北京";
public static TestDemo7 getTestDemo() {
return testDemo;
}
public static void setTestDemo(TestDemo7 testDemo) {
TestDemo7.testDemo = testDemo;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getName() {
return name;
}
private TestDemo7() {
}
}
class Demo {
public static void main(String[] args) {
TestDemo7 t1=TestDemo7.testDemo;
}
}
单例设计模式列子
最新推荐文章于 2022-10-27 17:22:13 发布