Singleton【java】

Person.java
public interface Person {
  public void say();
}

PersonSingleton.java

public class PersonSingleton implements Person {

public void say() {
System.out.println("Welcome to w3c");
}

    private static Person personsingleton=null;
   
private PersonSingleton(){   
}

public static Person getInstance(){
if(personsingleton==null)
{
personsingleton=new PersonSingleton();
}
return personsingleton;
}
}


Test.java
public class Test {

public static void main(String args[]){

PersonSingleton.getInstance().say();
}

}

你可能感兴趣的:(Singleton)