728x90
반응형
싱글톤 패턴 실무에서 어떻게 쓰이나?
- 스프링에서 빈의 스코프 중에 하나로 싱클톤 스코프
- 자바 java.lang.Runtime
- 다른 디자인 패턴(빌더, 퍼사드, 추상 팩토리 등) 구현체의 일부로 쓰이기도 함
Runtime
public class RuntimeExample{
public static void main(String[] args){
Runtime runtime = Runtime.getRuntime();
System.out.println(runtime.maxMemory());
System.out.println(runtime.freeMemory());
}
}
- 이 경우 new로 선언이 안됨
- 이 런타임은 자바 어플리케이션 실행하는 환경에 대한것
- 실행환경의 메모리 정보등 출력 가능
스프링의 경우
public clas SpringsExample{
public static void main(String[] args){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
string hello = applicationContext.applicationContext.getBean("hello", String.class);
string hello1 =applicationContext.applicationContext.getBean("hello", String.class);
System.out.println(hello == hello1)
}
}
// 다른 클래스
public class SpringConfig{
@Bean
public String hello(){
return hello;
}
}
- 두개의 같은 인스턴스가 나옴
- 싱글톤 패턴이라고 부르기 보다는 싱글톤 스코프라고 함
- 이것은 유일한 인스턴스로 관리하는것 , 싱글톤은 아니다.
- 다른것이지만 유일한 객체가 필요한 경우 저런식으로 등록해서 사용 한다.
728x90
반응형
'CS Study > 디자인패턴' 카테고리의 다른 글
2021.11.15_팩토리메소드패턴02.패턴적용하기 (0) | 2021.11.15 |
---|---|
2021.11.15_팩토리메소드패턴01.패턴소개 (0) | 2021.11.15 |
2021.11.14_04.안전하고단순하게구현하는방법 (0) | 2021.11.14 |
2021.11.14_03.싱글톤패턴구현방법을깨트리는방법 (0) | 2021.11.14 |
2021.11.14_02.멀티쓰레드환경에서안전하게구현하는방법 (0) | 2021.11.14 |
댓글