단일 디자인 모델 계속 - 상한 선 이 있 는 여러 가지 디자인

3504 단어 디자인 모드
package Singleton_Pattern;

import java.util.ArrayList;
import java.util.Random;

//               ,         
public class demo3 {
    public static void main(String[] args) {
        int ministerNum = 5;
        for(int i=0;i<ministerNum;i++){
            Emperor2 emp = Emperor2.getInstance();
            System.out.println(" "+(i+1)+"       ");
            emp.say();
        }
    }
}

class Emperor2{
    //             
    private static int maxNumOfEmperor = 2;
    //        ,    ArrayList   ,         
    private static ArrayList<String> nameList = new ArrayList<String>();
    //      ,         
    private static ArrayList<Emperor2> emperorList =new ArrayList<Emperor2>();
    //        
    private static int countNumOfEmperor =0;
    //      
    static{
        for(int i=0;i<maxNumOfEmperor;i++){
            emperorList.add(new Emperor2(" "+(i+1)+" "));
        }
    }

    private Emperor2(){

    }

    private Emperor2(String name){
        nameList.add(name);
    }

    //          
    public static Emperor2 getInstance(){
        Random random = new Random();
        countNumOfEmperor = random.nextInt(maxNumOfEmperor);
        return emperorList.get(countNumOfEmperor);
    }
    //    
    public static void say(){
        System.out.println("  "+nameList.get(countNumOfEmperor));
    }
}

좋은 웹페이지 즐겨찾기