本人Android实际开发工作经验3年,新加入一家互联网公司负责项目重构,用户100W+,app已经迭代开发2年之久,存在代码结构不清醒,业务逻辑混乱,命名不规范,所以进行项目重构,重构是一个不断完善的过程,这是我前期根据项目需求定的一个框架,新手很多不足,希望多多指教哈!
project端工程介绍:
1:项目业务集成基于mvp+proxy结构
2:大部分模块含有core为当前模块核心结构
2:除以下模块还有entity,constant等辅助模块
app模块:
工程初始化:分为以下base初始化类
baseApplication:工程启动初始化
baseActivity:页面初始化
baseModel:工程数据模型
Briage模块:
”桥“ 分为以下子模块
factory:功能模块工厂加工,组件连接
Manager:功能模块集成
Croes模块:
工程核心模块,mvp模式
其中v模块:
activity:分工负责页面生命周期与Android其他四大组件交互
layout:由各子proxyview类代理
Assemly模块:
“组件模块”分为三部分:service,broadcast,contentprovide
Assistant模块:
项目帮助模块:分为以下子模块
Cache:工程缓存模块
Helper:工程帮助处理模块
Net:工程网络处理模块
security:工程安全模块
Modles模块:
”模型“ 模块 分为以下子模块:
modle:数据模型,业务模型
Presenters模块:
”交互层模块“分为以下子模块
adapter:适配器
prsenter:交互类
Views模块:
”View“ 模块‘分为以下子模块:
cores:view模块结构核心
Ui:activity 页面
window:分为fragment,dialog,view
ProxyView模块:
”View代理“ 代理activity关于view一切初始化
下面Cores模块部分代码示例,基于mvp思想,根据项目重构实际修改:
/* --------------------------------SettingActivity.java------------------------------------------------*/
public class SettingActivity extends BaseActivity {
private SettingPresentersettingPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
initCore();
}
@Override
public void initCore() {
settingPresenter=new SettingPresenter();
settingPresenter.initCreate(this);
}
@Override
public void onEventMainThread(Event event) {
super.onEventMainThread(event);
}
@Override
protected void onPause() {
super.onPause();
DialogHelper.dismissDialog(settingPresenter.getSettingProxyView().mComfirmDialog);
DialogHelper.dismissDialog(settingPresenter.getSettingProxyView().mLoadingDialog);
if (settingPresenter.getSettingProxyView().activity.isFinishing()) {
settingPresenter.getSettingProxyView().mLoadingDialog =null;
settingPresenter.getSettingProxyView().mComfirmDialog =null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
settingPresenter.detachView();
}
}
/* --------------------------------SettingPresenter.java---------------------------------------------*/
public class SettingPresent erextends BasePresenter {
private SettingModelsettingModel;
private SettingProxyViewsettingProxyView;
public SettingModel getSettingModel() {
return settingModel;
}
public SettingProxyView getSettingProxyView() {
return settingProxyView;
}
@Override
public void initCreate(BaseActivity activity) {
settingProxyView =new SettingProxyView(activity,this);
settingModel=new SettingModel();
attachView(settingProxyView);
}
@Override
public void initView() {
}
@Override
public void initData() {
settingModel.addProxyView(settingProxyView);
settingModel.initRadioGroup();
}
@Override
public void initEvent() {
}
@Override
public void ViewOnClick(View view) {
switch (view.getId()){
case R.id.setting_check_version:
settingModel.checkNewVersion();
ClickAgentHelper.getInstance().addClickLog(ClickLog.S_LOG_CLICK_SETTING_CHECK_UPDATE);
break;
case R.id.setting_about:
Intent i =new Intent(settingProxyView.activity, AboutActivity.class);
settingProxyView.activity.startActivity(i);
ClickAgentHelper.getInstance().addClickLog(ClickLog.S_LOG_CLICK_SETTING_ABOUT);
break;
default:
break;
}
}
@Override
public void ViewOnLongClick(View view) {
}
}
/* ————————————————BasePresenter.java--------------------------------------------/**
* 交互层
* <基础业务类>*/
public abstract class BasePresenter implements Presenterlmp, ViewEventListenter {
protected ProxyViewproxyView;
public BasePresenter() {
}
public final void attachView(ProxyView proxyView) {
this.proxyView = proxyView;
initView();
initData();
initEvent();
}
public void detachView(){
LogHelper.d("detachView");
proxyView =null;
}
public abstract void initView();
public abstract void initData();
public abstract void initEvent();
@Override
public String getName() {
return proxyView.getClass().getSimpleName();
}
}
/* ————————————————SettingProxyView.java-------------------------------------------
public class SettingProxyView extends ProxyView {
public DialogmLoadingDialog =null;
public DialogmComfirmDialog =null;
public RadioGroupmBBSpeedRG =null;
public RadioGroupmManYiRG =null;
public CheckBoxmSpeekWcRG =null;
public RadioGroupmNaviOrientationRG =null;
public RadioGroupmNaviDayOrNightRG =null;
public RadioGroupmNaviMapDirectionRG =null;
public RadioGroupmNaviShijiaoRG =null;
public RadioGroupmFontSizeRG =null;
public CheckBoxmIntelligentBroadcastRG =null;
public CheckBoxmUMPushRG =null;
public CheckBoxmShowLimitMarkerRG =null;
public CheckBoxmTestOpenCB =null;
public TextViewmTitle=null;
public TextViewmTips=null;
public SettingProxyView(BaseActivity activity, ViewEventListenter viewEventListenter) {
super(activity,viewEventListenter);
initView();
initViewEvent();
}
@Override
public void initView() {
mLoadingDialog = DialogHelper.getLoadingDialog(activity);
mComfirmDialog = DialogHelper.getComfirmDialog(activity);
mTitle = (TextView) getView(R.id.nav_title);
mTips = (TextView) getView(R.id.title_tips);
mBBSpeedRG = (RadioGroup) getView(R.id.setting_voice);
mSpeekWcRG = (CheckBox)getView(R.id.setting_welcome_voice);
mNaviOrientationRG = (RadioGroup)getView(R.id.setting_screen);
mNaviDayOrNightRG = (RadioGroup)getView(R.id.setting_nav_daymode);
mIntelligentBroadcastRG = (CheckBox)getView(R.id.setting_speed);
//导航方向
mNaviMapDirectionRG = (RadioGroup)getView(R.id.setting_nav);
//导航模式 2D,3D
mNaviShijiaoRG = (RadioGroup)getView(R.id.setting_nav_perspective);
//字体大小
mFontSizeRG = (RadioGroup)getView(R.id.setting_fount_size);
//推送消息
mUMPushRG = (CheckBox)getView(R.id.setting_push_msg);
mShowLimitMarkerRG = (CheckBox)getView(R.id.setting_restricted);
mTestOpenCB = (CheckBox)getView(R.id.setting_test_open);
}
@Override
public void initViewEvent() {
mTitle.setText(activity.getResources().getText(R.string.setting_title));
mTips.setVisibility(View.GONE);
addOnClickListenter(getView(R.id.setting_check_version));
addOnClickListenter(getView(R.id.setting_about));
}
@Override
public void ViewOnClick(View view) {
mViewEventListenter.ViewOnClick(view);
}
@Override
public void ViewOnLongClick(View view) {
mViewEventListenter.ViewOnLongClick(view);
}
}
/* ————————————————ProxyView.java---------------------------------------------/**
*/
public abstract class ProxyView implements View.OnClickListener,View.OnLongClickListener{
private static final StringTAG =ProxyView.class.getSimpleName();
public ViewEventListentermViewEventListenter;
/**
* 返回按钮
*/
private Viewback;
/**
* 标题
*/
public TextViewtitle;
/**
* 右边字符
*/
public TextViewmTips;
private TextViewright;
public BaseActivityactivity;
public ProxyView(BaseActivity activity, ViewEventListenter viewEventListenter) {
this.mViewEventListenter=viewEventListenter;
this.activity=activity;
setHeader();
}
public abstract void initView();
public abstract void initViewEvent();
public abstract void ViewOnClick(View view);
public abstract void ViewOnLongClick(View view);
public void setHeader() {
back=getView(R.id.button_back);
mTips=(TextView)getView(R.id.title_tips);
title=(TextView)getView(R.id.nav_title);
if(back!=null)addOnClickListenter(back);
}
public void setTitle(String content){
if(title!=null)title.setText(content);
}
public void onClickBackButton()
{
activity.finish();
}
public View getView(int ViewId){
return activity.findViewById(ViewId);
}
public void addOnClickListenter(View view){
if (view==null)return;
view.setClickable(true);
view.setOnClickListener(this);
}
public void addOnLongClickListenter(View view){
view.setClickable(true);
view.setOnLongClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_back:
if (isFastClick()) {
return;
}
onClickBackButton();
break;
}
ViewOnClick(view);
}
@Override
public boolean onLongClick(View view) {
ViewOnLongClick(view);
return false;
}
private long mLastClickTime =0;
private static final int CLICK_SPACE_TIME =800;
public boolean isFastClick() {
long currentTime = System.currentTimeMillis();
boolean isFast =false;
if (currentTime -mLastClickTime >CLICK_SPACE_TIME) {
isFast =false;
}else {
isFast =true;
}
mLastClickTime = currentTime;
return isFast;
}
}
/* ————————————————SettingModel.java--------------------------------------------
public class SettingModel implements Modlelmp {
private SettingProxyView settingProxyView;
@Override
public void addProxyView(ProxyView proxyView) {
this.settingProxyView =(SettingProxyView) proxyView;
}
@Override
public void initModleData() {
}
public void checkNewVersion() {}
………………………………………………..省略大量数据模型,业务逻辑代码
………………………………………………..………………………………………………..………………………………………………..
………………………………………………..………………………………………………..………………………………………………..
}
/* ————————————————CoreFactory.java---------------------------------------------
/**
* <中间连接层>*
*/
public class CoreFactory {
private static CoreFactorymodel;
public static void init(Context context) {
model =new CoreFactory();
model.iniLocalFileStorageManager();
model.initPreferenceManager();
model.initSecurityManager();
model.initUserSession();
model.initCoreServiceManager(context);
model.initOkHttpManager();
model.initImageManager();
}
public static void destroy() {
model =null;
}
/**
* 初始化本地存储路径管理类
*/
private void iniLocalFileStorageManager() {
BridgeLifeCycleSetKeeper.getInstance().trustBridgeLifeCycle(LocalFileStorageManager.TAG,LocalFileStorageManager.getInstance());
}
/**
* 初始化SharedPreference管理类
*/
private void initPreferenceManager() {
BridgeLifeCycleSetKeeper.getInstance().trustBridgeLifeCycle(SharedPrefManager.TAG,SharedPrefManager.getInstance());
}
/**
* 网络请求管理类
*/
private void initOkHttpManager() {
BridgeLifeCycleSetKeeper.getInstance().trustBridgeLifeCycle(HttpManager.TAG, HttpManager.getInstance());
}
/**
* 图片管理类
*/
private void initImageManager() {
BridgeLifeCycleSetKeeper.getInstance().trustBridgeLifeCycle(ImageMangaer.TAG, ImageMangaer.getInstance());
}
/**
* 初始化安全模块
*/
private void initSecurityManager() {
}
/**
* 初始化用户信息模块
*/
private void initUserSession() {
}
/**
* 初始化Tcp服务
*
* @param context
*/
private void initCoreServiceManager(Context context) {
}
private void initDBManager() {
}
}
正在重构学习阶段,希望能得到多多指点,也希望能给大家提供一些帮助…….第一次写博客,哈哈!!!!