본문 바로가기

자바 프로그래밍

[JAVA] 싱글톤 패턴

* 객체들을 하나의 클래스에서 관리하여 getInstance 메소드를 통해 모든 클라이언트에 동일한 인스턴스를 반환하는 작업을 수행한다. 

* 다음은 Swing 컴포넌트를 이용한 예시다. 각각의 패널들을 하나의 클래스를 통해 관리한다. 


package Server;


import ServerUI.*;


public class ServerAppManager {

    private static ServerAppManager s_instance;

    private ServerFrame sFrame;

    private ServerBookPanel sBookPanel;

    private ServerPrimaryPanel sPrimPanel;

    private ServerReservationPanel sReservationPanel;

    private ServerRentPanel serverRentPanel;

    private ServerUserPanel sUserPanel;


    private ServerAppManager() {


    }


    public static ServerAppManager getS_instance() {

        if (s_instance == null)

            s_instance = new ServerAppManager();

        return s_instance;

    }


    public ServerReservationPanel getsReservationPanel() {

        return sReservationPanel;

    }


    public void setsReservationPanel(ServerReservationPanel sReservationPanel) {

        this.sReservationPanel = sReservationPanel;

    }


    public ServerRentPanel getServerRentPanel() {

        return serverRentPanel;

    }


    public void setServerRentPanel(ServerRentPanel serverRentPanel) {

        this.serverRentPanel = serverRentPanel;

    }


    public void setS_ServerFrame(ServerFrame sFrame) {

        this.sFrame = sFrame;

    }


    public ServerFrame getsServerFrame() {

        return sFrame;

    }


    public ServerBookPanel getsBookPanel() {

        return sBookPanel;

    }


    public void setsBookPanel(ServerBookPanel sBookPanel) {

        this.sBookPanel = sBookPanel;

    }


    public ServerPrimaryPanel getsPrimPanel() {

        return sPrimPanel;

    }


    public void setsPrimPanel(ServerPrimaryPanel sPrimPanel) {

        this.sPrimPanel = sPrimPanel;

    }


    public ServerUserPanel getsUserPanel() {

        return sUserPanel;

    }


    public void setsUserPanel(ServerUserPanel sUserPanel) {

        this.sUserPanel = sUserPanel;

    }


}