1. 程式人生 > >8種常用的android設計模式

8種常用的android設計模式

一般來說,常用的android設計模式有以下8種:單例、工廠、觀察者、代理、命令、介面卡、合成、訪問者。

 

  單例模式:目的是為了讓系統中只有一個呼叫物件,缺點是單例使其他程式過分依賴它,而且不同單例執行在不同程序中,使得維護困難;

 

  工廠模式:生產固定的一些東西,如抽象類,缺點是產品修改麻煩;如喜歡動作片和愛情片的人分別向伺服器發出同一個請求,就可以得到他們想看的影片集,相當於不同物件進行同一請求,需求均得到滿足。

      AOSP中使用到工廠模式的地方:

 
  1. Java中的工廠方法:

  2. java.net.DatagramSocketImplFactory

  3. java.net.SocketImplFactory

  4. java.net.URLStreamHandlerFactory

  5. java.security.cert.CertificateFactory

  6. java.util.prefs.PreferencesFactory

  7. javax.xml.parsers.SAXParserFactory

  8. SSLSocketFactory

  9. LogFactory

  10. AuthSchemeFactory

  11. SocketFactory

  12. MultihomePlainSocketFactory

  13. LayeredSocketFactory

  14. PlainSocketFactory

  15. CookieSpecFactory

  16. HttpRequestFactory

  17. HttpResponseFactory

  18. ClientConnectionManagerFactory

  19. XMLStringFactory

  20. lookUpFactoryClass

  21. DocumentBuilderFactory

  22. IntentFactory

  23. SQLiteDatabase.CursorFactory

  24. RemoteViewsFactory

  25. EffectFactory

  26. CodecFactory

  27. EGLContextFactory

  28. SAXParserFactory

  29. Spannable.Factory

  30. MockEditableFactory

  31. TransformerFactory

  32. IApiLoader

  33. TypePool

  34. ITypeFactory

  35. org.xmlpull.v1.XmlPullParserFactory

  36. ContextFactory

  37. LayoutInflater

  38. CardViewFragment

  39. NFAFactory

  40. ExecutorFactory

  41.  
  42. C/C++中的工廠方法:

  43.  
  44. AudioSignalFactory

  45. GenericFactory

  46. EmulatedCameraFactory

  47. ANTLR3_TOKEN_FACTORY

  48. JniDependencyFactory

  49. WeakPtrFactory

  50. scheme_classifier_factory

  51. content_activity_factory

  52. activity_factory

  53. pref_service_factory

  54. MessagePumpFactory

  55. surface_factory

  56. LayerTreeHostFactory

  57. SurfaceFactory

  58. DriveIntegrationServiceFactory

  59. ServiceFactory

  60. devtools_network_transaction_factory

  61. MockDistillerFactory

  62. MockDistillerPageFactory

  63. BrowserContextKeyedAPIFactory

  64. PickerFactory

  65. URLFetcherImplFactory

  66. BrowserContextKeyedAPIFactory

  67. chrome/browser/extensions/api/dial/dial_api_factory.h

  68. chrome/browser/extensions/api/idle/idle_manager_factory.h

  69. chrome/browser/extensions/api/networking_private/networking_private_event_router_factory.h

  70. chrome/browser/extensions/extension_garbage_collector_factory.h

  71. chrome/browser/extensions/extension_storage_monitor_factory.h

  72. chrome/browser/extensions/extension_system_factory.h

  73. chrome/browser/extensions/extension_toolbar_model_factory.h

  74. chrome/browser/extensions/install_tracker_factory.h

  75. chrome/browser/extensions/menu_manager_factory.h

  76. chrome/browser/extensions/token_cache/token_cache_service_factory.h

  77. chrome/browser/chromeos/extensions/file_manager/event_router_factory.h

  78. chrome/browser/signin/chrome_signin_client_factory.h

  79. chrome/browser/signin/chrome_signin_client_factory.h

  80. chrome/browser/signin/signin_manager_factory.h

  81. chrome/browser/invalidation/profile_invalidation_provider_factory.h

  82. chrome/browser/services/gcm/gcm_profile_service_factory.h

  83. chrome/browser/net/proxy_service_factory.h

  84. chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h

  85. content/public/browser/cookie_store_factory.h

  86. net/http/http_auth_handler_factory.h

  87. net/url_request/url_request_job_factory_impl.h

  88. PrivetHTTPAsynchronousFactory

  89. chrome/browser/search/hotword_service_factory.h

  90. chrome/browser/search_engines/template_url_service_factory.h

  91. chrome/browser/search_engines/template_url_fetcher_factory.h

  92. components/gcm_driver/gcm_client_factory.h

  93. chrome/browser/supervised_user/supervised_user_service_factory.h

  94. chrome/browser/signin/profile_oauth2_token_service_factory.h

  95. chrome/browser/supervised_user/custodian_profile_downloader_service_factory.h

  96. chrome/browser/supervised_user/supervised_user_pref_mapping_service_factory.h

  97. chrome/browser/supervised_user/supervised_user_settings_service_factory.h

  98. chrome/browser/supervised_user/supervised_user_shared_settings_service_factory.h

  99. chrome/browser/supervised_user/supervised_user_sync_service_factory.h

  100. chrome/browser/sync/profile_sync_service_factory.h

  101. chrome/browser/themes/theme_service_factory.h

  102. chrome/browser/bookmarks/bookmark_model_factory.h

  103. chrome/browser/dom_distiller/dom_distiller_service_factory.h

  104. chrome/browser/history/history_service_factory.h

  105. chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h

  106. chrome/browser/password_manager/password_store_factory.h

  107. chrome/browser/search_engines/template_url_service_factory.h

  108. chrome/browser/signin/signin_manager_factory.h

  109. chrome/browser/sync/profile_sync_components_factory_impl.h

  110. chrome/browser/webdata/web_data_service_factory.h

  111. chrome/browser/ui/app_list/app_list_syncable_service_factory.h

  112. chrome/browser/spellchecker/spellcheck_factory.h

  113. content/common/gpu/gpu_memory_buffer_factory.h

  114. content/renderer/android/synchronous_compositor_factory.cc

  115. mojo/services/public/cpp/view_manager/view_manager_client_factory.h

                   太多了,比想辦法羅列完,有一個更急迫的問題:如何消滅程式設計師?

 

  觀察者模式:就是多個物件對一個物件進行監控,如快取;

 

  代理模式:自己的事交給別人去做,分別返回結果即可,如非同步執行緒;

 

  命令模式:呼叫物件與作用物件之間分離,由中介軟體來協調兩者之間的工作,如控制器;

 

  介面卡模式:將一個介面變成使用者所需要的介面,如baseadapter可以適配listview和spinner,因為它們有相同的介面

 

  合成模式:將一對多的關係轉換成一對整體的關係,如listview與介面卡;

 

      建造者模式:AlertDialog.Builder