android design pattern

6,838 views

Published on

Published in: Technology, Education

android design pattern

  1. 1. Android Design Pattern Lucas Xu 1
  2. 2. In a nutshell  MVC  MVVM  Observer  Adapter  Façade  Bridge  Factory  Template  Composition  Decorator 2
  3. 3. MVC  Model: data  View: UI  Controller: Logic 3
  4. 4. MVC - View  res/layout/main.xml http://developer.android.com/resources/tutorials/views/hello-gridview.html <?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/ res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" /> 4
  5. 5. MVC - Controller http://developer.android.com/resources/tutorials/views/hello-listview.html  HelloGridView.java public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } 5
  6. 6. MVC - Model public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } // create a new ImageView for each item referenced by the Adapter public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); 6
  7. 7. MVC: all in one  ListActiviy Ref: http://www.ideasandroid.com/archives/295 http://developer.android.com/reference/android/app/ListActivity.html 7  Exercise  Write your custom GridActivity
  8. 8. Variant – MVVM Model View ViewModel 8 Ref: http://en.wikipedia.org/wiki/Model_View_ViewModel http://www.codeproject.com/KB/android/androidbinding.aspx Android Binding
  9. 9. MVVM (cont’d)  public static interfaceSimpleCursorAdapter  bindView(View view, Context context, Cursor cursor)  ViewBinder  setViewValue(View view, Object data, String textRepresentation) 9 Ref: http://developer.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html
  10. 10. Observer  BroadCastReciever  Sensor Listener  OnClickListener  onClick  OnCreateMenuListener  onCreateContextMenu  AsyncTask  onProgressUpdate, onPostExecute  Activity lifecycle callbacks (onStart, onResume, etc.) although with only one dedicated subscriber.  ?Many observerables support only one observer, how to extend?  View.setOnClickListener(new OnClickListener())… 10
  11. 11. Observer (cont’d)  Handler  handleMessage()  sendMessage()  Single thread model v.s. Multiple thread model  Update UI in UI thread  Looper, Handler , MessageQueue  Kill Thread 11
  12. 12. Observer (cont’d)  UI Sync:  ES 文件浏览器  ListView, GridView, etc,  Delete, add, rename file .  App alliance  App downloading  Downloading cancelled/ failed  App installed 12
  13. 13. Adapter  View  AdapterView  ExpandableListView  Gallery  GridView  ListView  Spinner 13 Data Adapter View ListView GridView CustomView Data Source Content Provider SharedPreference Resources SQLite File System
  14. 14. public interfaceAdapter abstract int getCount() abstract Object getItem(int position) abstract long getItemId(int position) abstract int getItemViewType(int position) abstract View getView(int position, View convertView, ViewGroup parent) abstract int getViewTypeCount() abstract boolean hasStableIds() abstract boolean isEmpty() abstract void registerDataSetObserver(DataSetObserver observer) abstract void unregisterDataSetObserver(DataSetObserver observer) 14 Adapter (cont’)  ArrayAdapter<T>  BaseAdapter  CursorAdapter  HeaderViewListAdapter  ListAdapter  ResourceCursorAdapter  SimpleAdapter  SimpleCursorAdapter  SpinnerAdapter  WrapperListAdapter
  15. 15. Façade  A facade is an object that provides a simplified interface to a larger body of code, such as a class library.  wrap a poorly-designed collection of APIs with a single well-designed API (as per task needs).  Hide Complexity  App network  ExchangeViewManager 15
  16. 16. Façade (cont’d) Android Media Framework 16
  17. 17. Bridge  "decouple an abstraction from its implementation so that the two can vary independently“ 17 Adapter CursorAdapter SimpleCursorAdapter ArrayAdapter AdapterView AdapterViewFlipper ExpandableListView Gallery
  18. 18. Factory Method  Defer instantiation to subclasses 18 View Activity ListView OnCreate() ListActivity OnCreate() ... View= FactoryMethod() ... return new ListView
  19. 19. Factory Method (cont’d)  View.  protected Dialog onCreateDialog(int id) Builder  AlertDialog.Builder builder = new AlertDialog.Builder(this); 19
  20. 20. Template  Activity  onCreate()  AsyncTask  doInBackground() 20
  21. 21. Composition  Group sub objects, treated as a single instance  View, View Group, etc. 21
  22. 22. Composition (cont’d)  View  ViewGroup  ? Why does ViewGroup subclass View, what pattern is used?  ?交换的小把手有很多样式,用哪种设计模式? 22 public abstract class ViewGroup extends View implements ViewParent{ private View[] mChildren; … } Reference: GOF: Design Pattern, chap 2
  23. 23. 23 http://www.adakoda.com/adakoda/2010/01/20/ASPH_AW_REV1.pdf
  24. 24. Decorator  java.io.BufferedInputStream(InputStream)  java.io.DataInputStream(InputStream)  java.io.BufferedOutputStream(OutputStre am)  java.util.zip.ZipOutputStream(OutputStrea m) 24
  25. 25. 25

×