디자인패턴 2. Singleton Pattern

min seung moon·2021년 6월 25일
0

Spring

목록 보기
7/50

1. Singleton Pattern

  • Singleton Pattern은 어떠한 클래스(객체)가 유일하게 1개만 존재 할 때 사용
  • 이를 주로 사용하는 곳은 서로 자원을 공유할 때 사용하는데, 실물 세계에서는 프린터가 해당되며, 실제 프로그래밍에서는 TCP Socket 통신에서 서버와 연결된 connect 객체에 주로 사용

2. 프로젝트 테스트

01. 프로젝트 생성

  • Java Project로 생성, design
  • singleton Package SocketClient.class 생성
  • SocketClient.class
package com.company.design.singleton;

public class SocketClient {

    // static으로 하여 생성자 없이 객체를 생성할 준비를 함
    private static SocketClient socketClient = null;

    // default 생성자를 막아서 외부에서 SocketClient 객체 생성을 막음
    private SocketClient() {

    }

    // getInstance를 static으로 생성하여 생성자 없이 객체에 접근하도록 만듬
    public static SocketClient getInstance() {

        if (socketClient == null) {
            socketClient = new SocketClient();
        }

        return socketClient;
    }

    public void connect() {
        System.out.println("connect");
    }

}

  • AClazz, BClazz 생성(Class는 예약어이기에 ss대신 zz를 사용)

    • AClazz
    package com.company.design.singleton;
    
    public class AClazz {
    
        private SocketClient socketClient;
    
        public AClazz() {
            this.socketClient = SocketClient.getInstance();
        }
    
        public SocketClient getSocketClient() {
            return this.socketClient;
        }
    }
    • BClazz
    package com.company.design.singleton;
    
    public class BClazz {
    
        private SocketClient socketClient;
    
        public BClazz() {
            this.socketClient = SocketClient.getInstance();
        }
    
        public SocketClient getSocketClient() {
            return this.socketClient;
        }
    }
  • Main에서 Test

package com.company.design;

import com.company.design.singleton.AClazz;
import com.company.design.singleton.BClazz;
import com.company.design.singleton.SocketClient;

public class Main {

    public static void main(String[] args) {

        AClazz aClazz = new AClazz();
        BClazz bClazz = new BClazz();

        SocketClient aClient = aClazz.getSocketClient();
        SocketClient bClient = bClazz.getSocketClient();

        System.out.println("두개의 객체가 동일한가?");
        System.out.println(aClient.equals(bClient));
    }
}


02. Singleton Pattern이 수정

  • 만약에 Singleton Pattern이 아니여도 객체가 동일한지 테스트
  • SocketClient.java
package com.company.design.singleton;

public class SocketClient {

    // static으로 하여 생성자 없이 객체를 생성할 준비를 함
    private static SocketClient socketClient = null;

    // default 생성자를 막아서 외부에서 SocketClient 객체 생성을 막음
    public SocketClient() {

    }

    // getInstance를 static으로 생성하여 생성자 없이 객체에 접근하도록 만듬
    public static SocketClient getInstance() {

        if (socketClient == null) {
            socketClient = new SocketClient();
        }

        return socketClient;
    }

    public void connect() {
        System.out.println("connect");
    }

}
  • AClazz.java
package com.company.design.singleton;

public class AClazz {

    private SocketClient socketClient;

    public AClazz() {
        //this.socketClient = SocketClient.getInstance();
        this.socketClient = new SocketClient();
    }

    public SocketClient getSocketClient() {
        return this.socketClient;
    }
}
  • BClazz.java
package com.company.design.singleton;

public class BClazz {

    private SocketClient socketClient;

    public BClazz() {
        //this.socketClient = SocketClient.getInstance();
        this.socketClient = new SocketClient();
    }

    public SocketClient getSocketClient() {
        return this.socketClient;
    }
}
  • Main Test
profile
아직까지는 코린이!

0개의 댓글