반응형

객체를 가볍게 만들어서 메모리 사용을 줄이는 패턴이다. 플라이급의 플라이인 가벼운 것을 의미한다.

자주 변하는 속성(exstrinsit)과 변하지 않는 속성(instrinsit)을 분리하고 재사용하여 메모리
사용을 줄일 수 있다.

잘 변하지 않는 것들을 모아 놓은 것을 플라이 웨이트 패턴이라고 하는데 이 때

잘 변하지 않는 것들을 팩토리에 모아 놓고 캐싱하여 재사용하는 것이다.

🚗 편집기 예제로 패턴 적용 전 알아보기


해당 프로그램은 hello라는 글을 쓸 수 있는 편집기이다.

hello라는 글자를 white 색상, 폰트는 Nanum, 폰트 크기는 12로 정해진 글자이다.

public class Character {

  private char value;
  private String color;
  private String fontFamily;
  private int fontSize;

  public Character(char value, String color, String fontFamily, int fontSize) {
    this.value = value;
    this.color = color;
    this.fontFamily = fontFamily;
    this.fontSize = fontSize;
  }
}

public class Client {

  public static void main(String[] args) {
    Character c1 = new Character('h', "white", "Nanum", 12);
    Character c2 = new Character('e', "white", "Nanum", 12);
    Character c3 = new Character('l', "white", "Nanum", 12);
    Character c4 = new Character('l', "white", "Nanum", 12);
    Character c5 = new Character('o', "white", "Nanum", 12);
  }
}

이 프로그램은 한글자 한글자마다 객체를 생성하게 되므로 메모리를 아주 많이 사용하게 된다.

그래서 이를 개선하기 위해 intrinsit한 내용은 캐싱해놓고 사용해서 메모리 사용을 줄일 수 있도록 개선할 수

있는데 이를 플라이 웨이트 패턴으로 할 수 있다.

🚗 플라이 웨이트 패턴으로 메모리 사용량 줄이기


instrinsit 데이터 고르기

이는 도메인적으로 다 다르기 때문에 현재 예제에서는 fontFamily, fontSize라고 판단한다.

그럼 이 데이터는 변하지 않는 데이터로써 모두가 공유하는 객체가 될 것이다.

적용하기

instrinsit 데이터 관리 클래스

데이터를 관리하는 객체로 변하지 않는 값이므로 immutable로 만들어야 한다.

class final까지 있는 이유는 상속으로 생성되는 것까지 막기 위함인데

해당 패턴의 용도 자체가 메모리 사용량을 줄이기 위함이기 때문이다.

public final class Font {

  final String family;
  final int size;

  public Font(String family, int size) {
    this.family = family;
    this.size = size;
  }

  public String getFamily() {
    return family;
  }

  public int getSize() {
    return size;
  }
}

exstrinsit 데이터 관리 클래스

변하는 데이터를 관리하는 객체로써 instrinsit인 Font를 갖는다.

public class Character {

  char value;
  String color;
  Font font;

  public Character(char value, String color, Font font) {
    this.value = value;
    this.color = color;
    this.font = font;
  }
}

Fly weight factory

intrinsit을 생성하고 조회하는 팩토리 클래스로써 이미 존재하는 Font이면 그대로 반환하고

그렇지 않으면 새로 생성해서 반환한다.

public final class FontFactory {

  private static final Map<String, Font> cache = new HashMap<>();

  private FontFactory() {}

  public Font getFont(String font) {
    if (cache.containsKey(font)) {
      return cache.get(font);
    }

    String[] split = font.split(":");
    String fontFamily = split[0];
    String fontSize = split[1];

    Font newFont = new Font(fontFamily, Integer.parseInt(fontSize));
    cache.put(font, newFont);

    return newFont;
  }
}

Client 코드

public class Client {

  public static void main(String[] args) {
    Character c1 = new Character('h', "white", FontFactory.getFont("nanum:12"));
    Character c2 = new Character('e', "white", FontFactory.getFont("nanum:12"));
    Character c3 = new Character('l', "white", FontFactory.getFont("nanum:12"));
    Character c4 = new Character('l', "white", FontFactory.getFont("nanum:12"));
    Character c5 = new Character('o', "white", FontFactory.getFont("nanum:12"));
  }
}

🚗 장점과 단점


장점

메모리 사용량을 줄일 수 있다.

단점

구조가 복잡해진다.

반응형
복사했습니다!