728x90
반응형
By 백기선.코딩으로 학습하는 GoF의 디자인 패턴
Singleton
1. 개념 및 구현 방법
2. 패턴 구현 깨트리는 방법
3. 안전하고 단순하게 구현하는 방법
4. 자바와 스프링에서 찾아보는 패턴
실무에서는 어떻게 쓰이나?
- 스프링에서 빈의 스코프 중에 하나로 싱글톤 스코프.
- 자바 java.lang.Runtime
- 다른 디자인 패턴(빌더, 퍼사드, 추상 팩토리 등) 구현체의 일부로 쓰이기도 한다.
public class RunTimeExample {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println("runtime = " + runtime.maxMemory());
System.out.println("runtime.freeMemory() = " + runtime.freeMemory());
}
}
@Configuration
public class SpringConfig {
@Bean
public String hello(){
return "Hello";
}
}
public class SpringExample {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
String hello = applicationContext.getBean("hello", String.class);
String hello2 = applicationContext.getBean("hello", String.class);
System.out.println(hello == hello2);
}
}
728x90
반응형
'Design Patterns > 생성(Creational)' 카테고리의 다른 글
[Abstract factory]추상팩토리 (0) | 2022.05.25 |
---|---|
[FactoryMethod]장점과 단점 (0) | 2022.05.20 |
[Singleton]3. 안전하고 단순하게 구현하는 방법 (0) | 2022.04.07 |
[Singleton]2. 패턴 구현 깨트리는 방법 (0) | 2022.04.05 |
[Singleton] 1. 개념 및 구현 방법 (0) | 2022.04.04 |