8種常用的android設計模式
一般來說,常用的android設計模式有以下8種:單例、工廠、觀察者、代理、命令、介面卡、合成、訪問者。
單例模式:目的是為了讓系統中只有一個呼叫物件,缺點是單例使其他程式過分依賴它,而且不同單例執行在不同程序中,使得維護困難;
工廠模式:生產固定的一些東西,如抽象類,缺點是產品修改麻煩;如喜歡動作片和愛情片的人分別向伺服器發出同一個請求,就可以得到他們想看的影片集,相當於不同物件進行同一請求,需求均得到滿足。
AOSP中使用到工廠模式的地方:
-
Java中的工廠方法:
-
java.net.DatagramSocketImplFactory
-
java.net.SocketImplFactory
-
java.net.URLStreamHandlerFactory
-
java.security.cert.CertificateFactory
-
java.util.prefs.PreferencesFactory
-
javax.xml.parsers.SAXParserFactory
-
SSLSocketFactory
-
LogFactory
-
AuthSchemeFactory
-
SocketFactory
-
MultihomePlainSocketFactory
-
LayeredSocketFactory
-
PlainSocketFactory
-
CookieSpecFactory
-
HttpRequestFactory
-
HttpResponseFactory
-
ClientConnectionManagerFactory
-
XMLStringFactory
-
lookUpFactoryClass
-
DocumentBuilderFactory
-
IntentFactory
-
SQLiteDatabase.CursorFactory
-
RemoteViewsFactory
-
EffectFactory
-
CodecFactory
-
EGLContextFactory
-
SAXParserFactory
-
Spannable.Factory
-
MockEditableFactory
-
TransformerFactory
-
IApiLoader
-
TypePool
-
ITypeFactory
-
org.xmlpull.v1.XmlPullParserFactory
-
ContextFactory
-
LayoutInflater
-
CardViewFragment
-
NFAFactory
-
ExecutorFactory
-
C/C++中的工廠方法:
-
AudioSignalFactory
-
GenericFactory
-
EmulatedCameraFactory
-
ANTLR3_TOKEN_FACTORY
-
JniDependencyFactory
-
WeakPtrFactory
-
scheme_classifier_factory
-
content_activity_factory
-
activity_factory
-
pref_service_factory
-
MessagePumpFactory
-
surface_factory
-
LayerTreeHostFactory
-
SurfaceFactory
-
DriveIntegrationServiceFactory
-
ServiceFactory
-
devtools_network_transaction_factory
-
MockDistillerFactory
-
MockDistillerPageFactory
-
BrowserContextKeyedAPIFactory
-
PickerFactory
-
URLFetcherImplFactory
-
BrowserContextKeyedAPIFactory
-
chrome/browser/extensions/api/dial/dial_api_factory.h
-
chrome/browser/extensions/api/idle/idle_manager_factory.h
-
chrome/browser/extensions/api/networking_private/networking_private_event_router_factory.h
-
chrome/browser/extensions/extension_garbage_collector_factory.h
-
chrome/browser/extensions/extension_storage_monitor_factory.h
-
chrome/browser/extensions/extension_system_factory.h
-
chrome/browser/extensions/extension_toolbar_model_factory.h
-
chrome/browser/extensions/install_tracker_factory.h
-
chrome/browser/extensions/menu_manager_factory.h
-
chrome/browser/extensions/token_cache/token_cache_service_factory.h
-
chrome/browser/chromeos/extensions/file_manager/event_router_factory.h
-
chrome/browser/signin/chrome_signin_client_factory.h
-
chrome/browser/signin/chrome_signin_client_factory.h
-
chrome/browser/signin/signin_manager_factory.h
-
chrome/browser/invalidation/profile_invalidation_provider_factory.h
-
chrome/browser/services/gcm/gcm_profile_service_factory.h
-
chrome/browser/net/proxy_service_factory.h
-
chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h
-
content/public/browser/cookie_store_factory.h
-
net/http/http_auth_handler_factory.h
-
net/url_request/url_request_job_factory_impl.h
-
PrivetHTTPAsynchronousFactory
-
chrome/browser/search/hotword_service_factory.h
-
chrome/browser/search_engines/template_url_service_factory.h
-
chrome/browser/search_engines/template_url_fetcher_factory.h
-
components/gcm_driver/gcm_client_factory.h
-
chrome/browser/supervised_user/supervised_user_service_factory.h
-
chrome/browser/signin/profile_oauth2_token_service_factory.h
-
chrome/browser/supervised_user/custodian_profile_downloader_service_factory.h
-
chrome/browser/supervised_user/supervised_user_pref_mapping_service_factory.h
-
chrome/browser/supervised_user/supervised_user_settings_service_factory.h
-
chrome/browser/supervised_user/supervised_user_shared_settings_service_factory.h
-
chrome/browser/supervised_user/supervised_user_sync_service_factory.h
-
chrome/browser/sync/profile_sync_service_factory.h
-
chrome/browser/themes/theme_service_factory.h
-
chrome/browser/bookmarks/bookmark_model_factory.h
-
chrome/browser/dom_distiller/dom_distiller_service_factory.h
-
chrome/browser/history/history_service_factory.h
-
chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h
-
chrome/browser/password_manager/password_store_factory.h
-
chrome/browser/search_engines/template_url_service_factory.h
-
chrome/browser/signin/signin_manager_factory.h
-
chrome/browser/sync/profile_sync_components_factory_impl.h
-
chrome/browser/webdata/web_data_service_factory.h
-
chrome/browser/ui/app_list/app_list_syncable_service_factory.h
-
chrome/browser/spellchecker/spellcheck_factory.h
-
content/common/gpu/gpu_memory_buffer_factory.h
-
content/renderer/android/synchronous_compositor_factory.cc
-
mojo/services/public/cpp/view_manager/view_manager_client_factory.h
太多了,比想辦法羅列完,有一個更急迫的問題:如何消滅程式設計師?
觀察者模式:就是多個物件對一個物件進行監控,如快取;
代理模式:自己的事交給別人去做,分別返回結果即可,如非同步執行緒;
命令模式:呼叫物件與作用物件之間分離,由中介軟體來協調兩者之間的工作,如控制器;
介面卡模式:將一個介面變成使用者所需要的介面,如baseadapter可以適配listview和spinner,因為它們有相同的介面
合成模式:將一對多的關係轉換成一對整體的關係,如listview與介面卡;
建造者模式:AlertDialog.Builder