[DesignPattern] flyweight pattern

2014. 1. 6. 17:37Architecture/DesignPattern

반응형


Flyweight Pattern


 

 개요

 클래스 다이어그램

 예제(Java)

 같이보기

 참고 사항

 

 

 

<< 개요 >>

Flyweight - 동일한 것을 공유해서 낭비를 없앤다.

이 디자인패턴은 객체를 '가볍게' 하기 위함 이다. 여기에서의 가볍다라는 것은 메모리의 사용량을 말한다.

한마디로 인스턴스를 가능한 공유시켜서 쓸데없이 new를 하지 않는 것이다.

 

 

<< 클래스 다이어그램 >> 


<< 예제(Java) >>

 

결과

 객체 생성!?
aceHunter님이1레벨이 되어 arrowShotting skill을 사용 할 수 있습니다.
aceHunter님이1레벨이 되어 doubleArrowShotting skill을 사용 할 수 있습니다.
aceHunter님이30레벨이 되어 arrowShotting 10발 skill을 사용 할 수 있습니다.
객체 생성!?
highHunter님이120레벨이 되어 arrowShotting 10발 skill을 사용 할 수 있습니다.
highHunter님이120레벨이 되어 슈퍼!! doubleArrowShotting skill을 사용 할 수 있습니다.

 

 

테스트 코드 

package kr.pe.acet.flyweight;

import static org.junit.Assert.*;
import kr.pe.acet.flyweight.HunterKind;
import kr.pe.acet.flyweight.HunderVilage;

import org.junit.Test;

public class FlyweightTest {

 @Test
 public void flyweightHunterLevleUpTest() {
  HunderVilage hunter = null;
  HunterKind acet = null;
  
  hunter= HunderVilage.getInstance();
  
  acet = hunter.getFlyweightHunter("aceHunter",1);
  acet.arrowShotting();
  acet.doubleArrowShotting();
  
  acet = hunter.getFlyweightHunter("aceHunter",30);
  acet.arrowShotting();
  acet.doubleArrowShotting();
  
  acet = hunter.getFlyweightHunter("highHunter",120);
  acet.arrowShotting();
  acet.doubleArrowShotting();
 }

}

 

Flyweight 소스 - interface

 package kr.pe.acet.flyweight;

public interface Hunter {
 
 // Lv 1
 public void arrowShotting();
 
 // lv 2
 public void doubleArrowShotting();
}

 

Flyweight 구현부 

package kr.pe.acet.flyweight;

public class HunterKind implements Hunter{

 private int lv;
 private String shareObj;

 public HunterKind(String shareObj) {
  this.shareObj = shareObj;
 }
 
 public void setLv(int lv) {
  this.lv = lv;
 }

 @Override
 public void arrowShotting() {
  // TODO Auto-generated method stub
  if(lv == 1){
   System.out.println(shareObj+"님이"+lv+"레벨이 되어 arrowShotting skill을 사용 할 수 있습니다.");
  }else if(lv > 20){
   System.out.println(shareObj+"님이"+lv+"레벨이 되어 arrowShotting 10발 skill을 사용 할 수 있습니다.");
  }
 }

 @Override
 public void doubleArrowShotting() {
  // TODO Auto-generated method stub
  if(lv == 1){
   System.out.println(shareObj+"님이"+lv+"레벨이 되어 doubleArrowShotting skill을 사용 할 수 있습니다.");
  }else if(lv > 100){
   System.out.println(shareObj+"님이"+lv+"레벨이 되어 슈퍼!! doubleArrowShotting skill을 사용 할 수 있습니다.");
  }
  
 }

}

 

 

 

Flyweight Factory 소스 

package kr.pe.acet.flyweight;

import java.util.HashMap;

public class HunderVilage {

 public HashMap<String, HunterKind> hunterSchool = new HashMap<String, HunterKind>();
 
 public HunderVilage(){
  
 }
 
 
 private static HunderVilage singletonHunter;
 public static HunderVilage getInstance(){
        if(singletonHunter == null){
            synchronized(HunderVilage.class){
                if(singletonHunter == null)
                 singletonHunter = new HunderVilage();
            }
            
        }
        return singletonHunter;
    }
 
 //핵심 코드.
 public synchronized HunterKind getFlyweightHunter(String shareObj, int level)
 {
  HunterKind th = (HunterKind) hunterSchool.get(shareObj);
  if (th == null)
  {
   System.out.println("객체 생성!?");
   th = new HunterKind(shareObj);
   th.setLv(level);
   hunterSchool.put(shareObj, th);
  }else{
   th.setLv(level);
  }
  return th;
 }


}

 

 

hunterSchool에 인스턴스를 종류별로 만들고, 같은 값을 사용하는 인스턴스를 Key 값으로 분류해 사용하면, 매번 new 하지 않아도 되고(new 를 할때 시간소요), 그만큼 메모리도 덜 소비 해서 퍼포먼스를 높일수 있다.

 

추가적으로

Intrinsic 상태 : Flyweight 객체의 내부에 저장 관리 되는 정보

Extrinsic 상태 : Flyweight 객체의 외부에 저장 관리 되는 정보

 


 

<< 같이 보기 >>

2013/07/21 - [Architecture/DesignPattern] - [첫번째 스터디] singleton 패턴


<< 참고 사항 >>

1. http://kimsunzun.tistory.com/entry/Flyweight-%ED%8C%A8%ED%84%B4-1

2. http://jmnote.com/wiki/Flyweight_%ED%8C%A8%ED%84%B4

3. http://www.luciole.kr/133





반응형

'Architecture > DesignPattern' 카테고리의 다른 글

[DesignPattern] Visitor pattern  (0) 2014.01.11
[DesignPattern] Iterator pattern  (0) 2014.01.06
[DesignPattern] state pattern  (0) 2013.12.31
[DesignPattern] memento pattern  (0) 2013.12.16
[DesignPattern] mediator pattern  (0) 2013.12.09