MVP在android上是常见的一种设计模式,在Launhcer,手机浏览器里面经常会看到,观摩了下其他大神写的,现在总结下,操刀起来写个demo
有什么问题的地方请提出来。
大家一起研究讨论。
demo 下载地址;http://download.csdn.net/detail/xufeifandj/8267619
demo主要是 主页是一个webview,实现了 历史记录的添加,与显示。例子比较简单。
整个项目的结构如下:
HistoryPresenter.java 主要是对view与model的控制相当于MVC中的 控制层吧。
具体实现添加历史记录移除历史记录等业务逻辑,都在HistoryModel里面去实现了。而UI.java则是一个接口,通过业务逻辑分析,把数据交予UI去通知MainAcitivy更新View.
package com.ferris.ferrismvp.presenter; import java.util.List; import com.ferris.ferrismvp.UI; import com.ferris.ferrismvp.beam.History; import com.ferris.ferrismvp.model.HistoryModel; import com.ferris.ferrismvp.model.IHistoryModel; public class HistoryPresenter { private UI ui; private IHistoryModel iHistoryModel; public HistoryPresenter(UI ui) { // TODO Auto-generated constructor stub this.ui=ui; this.iHistoryModel=new HistoryModel(); } /** * 添加历史记录 * @param hisString */ public void AddHistory(History hisString){ iHistoryModel.addHistory(hisString); } /** * 一掉历史记录 * @param hisString */ public void removeHistory(History hisString){ iHistoryModel.removeHistory(hisString); } /** * 显示所有历史记录 */ public void showHistorys(){ List<History> histories=iHistoryModel.showAllHistory(); ui.showAllHistory(histories); } }
历史记录Model业务逻辑接口,添加删除,查询3个方法。
package com.ferris.ferrismvp.model; import java.util.List; import com.ferris.ferrismvp.beam.History; public interface IHistoryModel { public void addHistory(History history); public void removeHistory(History history); public List<History> showAllHistory(); }
具体实现逻辑
package com.ferris.ferrismvp.model; import java.util.ArrayList; import java.util.List; import com.ferris.ferrismvp.beam.History; public class HistoryModel implements IHistoryModel{ private List<History> historys=new ArrayList<History>(); public HistoryModel() { // TODO Auto-generated constructor stub } @Override public void addHistory(History history) { // TODO Auto-generated method stub historys.add(history); } @Override public void removeHistory(History history) { // TODO Auto-generated method stub int removeId=-1; for(int i=0;i<historys.size();i++){ if(historys.get(i).getUrl().equals(history.getUrl())){ removeId=i; } } if(removeId!=-1){ historys.remove(removeId); } } @Override public List<History> showAllHistory() { // TODO Auto-generated method stub return historys; } }
History Bean对象
package com.ferris.ferrismvp.beam; public class History { private String title; private String url; public History() { // TODO Auto-generated constructor stub } public History(String title,String url) { // TODO Auto-generated constructor stub this.title=title; this.url=url; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
UI接口类
package com.ferris.ferrismvp; import java.util.List; import com.ferris.ferrismvp.beam.History; public interface UI { public void showAllHistory(List<History> historys); }
package com.ferris.ferrismvp; import java.util.List; import com.ferris.ferrismvp.beam.History; import com.ferris.ferrismvp.presenter.HistoryPresenter; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.widget.Button; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener,UI{ private WebView webView1 ; private Button addHistory,showHistory; private HistoryPresenter historyPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); initWebView(); historyPresenter=new HistoryPresenter(this); } private void initWebView() { // TODO Auto-generated method stub webView1.getSettings().setJavaScriptEnabled(true); webView1.loadUrl("http://www.baidu.com"); } private void findView() { // TODO Auto-generated method stub webView1=(WebView)findViewById(R.id.webView1); addHistory=(Button)findViewById(R.id.button1); showHistory=(Button)findViewById(R.id.button2); addHistory.setOnClickListener(this); showHistory.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1://添加历史记录 historyPresenter.AddHistory(new History(webView1.getTitle(),webView1.getUrl())); break; case R.id.button2://显示历史记录 historyPresenter.showHistorys(); break; default: break; } } @Override public void showAllHistory(List<History> historys) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, historys.toString(), Toast.LENGTH_SHORT).show(); } }
时间: 2024-12-26 16:17:37