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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值