오늘은 재택근무를 해서 블로그 공부를 할 시간이 됐다!
디자인 패턴을 집중적으로 공부해보자~
Flyweight Pattern?
- 어떤 클래스의 인스턴스 한 개만 가지고 여러 개의 "가상 인스턴스"를 제공하고 싶을 때 사용하는 패턴이다!
- 비용이 큰 자원을 공통으로 사용할 수 있도록 만드는 패턴이다!
- 궁극적으로 객체를 가볍게 하기 위한 것이라구~!
[핵심]
- 객체를 가능한 대로 공유시켜서 쓸떼없이 new하지 않도록 하는 것이 목표!
- 객체가 필요할 때 맨날 new 하는게 아니라, 이미 만들어져 있는 인스턴스(객체)를 이용할 수 있으면 그걸 공유하기
[구조]
- Flyweight의 역할
- 공유에 사용할 클래스들의 인터페이스(API)
- 다시 말해서 프로그램을 가볍게 하기 위한 역할을 함!
- ConcreteFlyweight(구체적인 플라이급)의 역할
- Flyweight의 내용을 정의! 실제 공유될 객체!
- FlyweightFactory(플라이급의 공장)의 역할
- 해당 공장을 사용해서 Flyweight의 인스턴스를 생성 또는 공유해주는 역할!
- 객체가 요청될 때마다 factory는 객체가 이미 생성되었는지 확인하는 역할!
- Client(클라이언트)의 역할
- 해당 패턴의 사용자!
[구현 하면서 익히기]
// 패턴을 적용할(공유로 사용될) 클래스들의 인터페이스(API)를 작성합니다.
public interface Shape {
void draw();
}
// getCircle() 메소드를 통해 객체의 생성 또는 공유의 역할을 담당하며 클라이언트에게 응답해줍니다.
public class ShapeFactory {
private static final HashMap<String, Circle> circleMap = new HashMap<>();
public static Shape getCircle (String color) {
Circle circle = circleMap.get(color);
if (circle == null) {
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("==== 새로운 객체 생성 : " + color + "색 원 ====");
}
return circle;
}
}
// ConcreteFlyweight에 해당하며 인터페이스(API)의 내용을 정의하고, 필요한 속성을 가질 수 있습니다.
public class Circle implements Shape {
private String color;
private int x;
private int y;
private int radius;
public Circle(String color) {
this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Circle{" +
"color='" + color + '\'' +
", x=" + x +
", y=" + y +
", radius=" + radius +
'}');
}
}
public class ShapeApplication {
public static void main(String[] args) {
String[] colors = {"Red", "Green", "Blue", "Yellow"};
for (int i = 0; i < 10; i++) {
Circle circle = (Circle) ShapeFactory.getCircle(colors[(int)(Math.random()*4)]);
circle.setX((int)(Math.random()*100));
circle.setY((int)(Math.random()*4));
circle.setRadius((int)(Math.random()*10));
circle.draw();
}
}
}
결과
오늘도 공부 시켜주신 멘토분들 너무 감사합니다! 여러분들의 지식 나눔 덕분에 제가 오늘도 공부했네요!
-11132021-
내가썼는데 뭐라는지 모르겠네.. 다시 써야할듯...
https://beomseok95.tistory.com/259
[Design Pattern] Flyweight pattern
[Design Pattern] Flyweight pattern Flyweight pattern이란? Flyweight pattern은 비용이 큰 자원을 공통으로 사용할 수 있도록 만드는 패턴입니다. 비용이 크다는 것은 두 가지로 나누어 생각할 수 있습니다. 1...
beomseok95.tistory.com
https://lee1535.tistory.com/106
[디자인패턴/Design Pattern] Flyweight Pattern / 플라이웨이트 패턴
관련 내용은 [자바 언어로 배우는 디자인 패턴 입문] , [Head First Design Pattern] 의 내용을 참고해서 정리한 글입니다. 잘못된 부분은 댓글로 피드백 부탁드립니다. 1. Flyweight 패턴이란? 어떤 클래스
lee1535.tistory.com
** 그냥 하루하루 개인 공부한 것을 끄적 거리는 공간입니다.
이곳 저곳에서 구글링한 것과 강의 들은
내용이 정리가 되었습니다.
그림들은 그림밑에 출처표시를 해놓았습니다.
문제가 될시 말씀해주시면 해당 부분은 삭제 하도록하겠습니다. **
'public void static main > Etc' 카테고리의 다른 글
[Design Pattern] Adapter Pattern (0) | 2021.11.10 |
---|---|
[Design Pattern] Builder Pattern (0) | 2021.11.09 |
[Design Pattern] Observer Pattern (0) | 2021.11.07 |
[Design Pattern] Singleton Pattern (0) | 2021.11.02 |
[npm] npm이란 (0) | 2021.05.05 |
댓글