2013. 12. 2. 22:10
Android DI란
정의
안드로이드 개발시 DI를 적용하는 방법. 관련 프레임워크에는 Roboguice, Android Annotations, Transfuse, DroidParts 등이 있다.
적용예
@EActivity(R.layout.translate) // Sets content view to R.layout.translate public class TranslateActivity extends Activity { @ViewById // Injects R.id.textInput EditText textInput; @ViewById(R.id.myTextView) // Injects R.id.myTextView TextView result; @AnimationRes // Injects android.R.anim.fade_in Animation fadeIn; @Click // When R.id.doTranslate button is clicked void doTranslate() { translateInBackground(textInput.getText().toString()); } @Background // Executed in a background thread void translateInBackground(String textToTranslate) { String translatedText = callGoogleTranslate(textToTranslate); showResult(translatedText); } @UiThread // Executed in the ui thread void showResult(String translatedText) { result.setText(translatedText); result.startAnimation(fadeIn); } // [...] }
DI 프레임워크별 장단점
- Roboguice : JSR-330을 지원하며 facebook, groupon, google docs 등 여러 앱들에서 사용. annotation processing을 사용하지 않아 부담이 있다.
- Android Annotations : @Click @Rest @Get 등의 안드로이드 전용 annotaion을 제공한다. annotation processing을 사용하므로 초기 설정이 번거롭다.
- Transfuse : JSR-330 및 annotation processing을 사용한다. AndroidManifest.xml 설정에 들어가는 내용도 annotaion을 활용하는 등 기존 어플에는 적용하기 까다로울수 있으나. 가장 annotaion을 파격적으로 사용한다.
- DroidParts : 안드로이드 전용 annotaion 및 RestClient, JSON 해석, ORM, Async 등의 다양한 기능을 제공하나 문서가 부족하고, 런타임에서 처리하는 일이 많다.
annotation processing을 지원하며 다양한 구성 요소에 injection을 지원하는 Android Annotations과
유명한 레퍼런스 어플이 많은 Roboguice을 권장할만하다.
안드로이드에서 DI 사용시 유의점
- DI를 사용하기위한 라이브러리 추가로 용량이 늘어나게 된다.
- 호출스택이 늘어나 성능에 부담이 된다.
- 어플리케이션 규모가 작아 얻는 이득이 크지 않다.
그러나 빠르게 대응해야 되는 모바일 어플리케이션에서
코드가 짧아지고 가독성이 좋아져 수정 및 유지보수하기가 쉽다는 점은 의미가 있다.
참고 http://helloworld.naver.com/helloworld/342818
'프로그래밍' 카테고리의 다른 글
PostgreSQL (0) | 2014.06.30 |
---|---|
Android 성능향상 (0) | 2014.02.12 |
Thread Dump (0) | 2014.02.10 |
Eclipse Error (0) | 2013.12.10 |
GCM (0) | 2013.12.02 |