码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式--模版设计模式

时间:2015-05-28 22:59:40      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

模版设计模式的概念或者定义:

定义一个操作中算法的框架,而将一些步骤延迟到子类中,使得子类可以不改变算法的结构即可重定义该算法中的某些特定步骤。
 
以前虽然经常在项目中使用模版设计模式,特别在BaseActivity,BaseFragement,BaseFragementActivity,BaseAdapter这种基类定义一些延伸到子类中实现的方法,但是不知道这个就是模版设计模式,我就毙了狗了。
 
比如在Base类里面为了规范代码的风格,在oncreate()里面调用空的函数,比如initView(),比如onCLickListener(),比如,初始化title的initTitleBar(),然后通过抽象方法供子类去实现这些模块,这样在项目编码过程中能够将每个人的代码规范到指定的位置,方便查看和调试代码。还有就是对于一些显示和判断的逻辑封装,以及都自定义的toast,对话框的统一,都可以通过在Base类中通过模版设计模式来实现,在子类中只是改变显示的内容,和逻辑以及业务的调整。
 
模版设计模式对编码的好处:
规范代码的流程,统一代码风格。
可扩展。
将子类的公共业务进行提取,减少冗余代码的编写,实现代码的复用。
 
以BaseActivity举例子来说:
getView(int id)
isNull(String str)
isStr(String str)
gotoActivity(Class<?> cls)
toast(String message)
都是将本来在子类中冗余的代码统一封装到base类进行调用,简化代码编写,减少冗余。
  1 package com.yougou.android.ui;
  2 import android.app.Activity;
  3 import android.content.Intent;
  4 import android.content.res.Resources;
  5 import android.os.Bundle;
  6 import android.text.TextUtils;
  7 import android.view.KeyEvent;
  8 import android.view.View;
  9 import android.view.Window;
 10 import android.view.WindowManager;
 11 import android.widget.EditText;
 12 import android.widget.Toast;
 13 import com.yougou.android.util.Logger;
 14 /**
 15  * @类名: BaseActivity
 16  * @描述: TODO(Activity基类,所有的Activity都要继承自该Activity)
 17  */
 18 public class BaseActivity extends Activity {
 19     Resources res; // 通用资源缩写
 20     // 为0 短时间
 21     // 为1 长时间
 22     public static final int LENGTH_TIME = 0;
 23     @Override
 24     protected void onCreate(Bundle savedInstanceState) {
 25         requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示标题
 26         super.onCreate(savedInstanceState);
 27         res = getResources(); // 通用资源缩写
 28         // 优化输入法模式
 29         int inputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
 30         getWindow().setSoftInputMode(inputMode);
 31     }
 32     /**
 33      * 通过泛型来简化findViewById
 34      * 
 35      * @param id
 36      * @param <E>
 37      * @return
 38      */
 39     @SuppressWarnings("unchecked")
 40     protected final <E extends View> E getView(int id) {
 41         try {
 42             return (E) findViewById(id);
 43         } catch (ClassCastException ex) {
 44             Logger.i("Could not cast View to concrete class.");
 45             throw ex;
 46         }
 47     }
 48     /**
 49      * 检查字符串是否是空对象或空字符串
 50      * 
 51      * @param str
 52      * @return 为空返回true,不为空返回false
 53      */
 54     public boolean isNull(String str) {
 55         if (TextUtils.isEmpty(str)) {
 56             return true;
 57         } else {
 58             return false;
 59         }
 60     }
 61     /**
 62      * 检查字符串是否是字符串
 63      * 
 64      * @param str
 65      * @return 为空返回true,不为空返回false
 66      */
 67     public boolean isStr(String str) {
 68         return !isNull(str);
 69     }
 70     /**
 71      * 从当前activity跳转到目标activity,<br>
 72      * 如果目标activity曾经打开过,就重新展现,<br>
 73      * 如果从来没打开过,就新建一个打开
 74      * 
 75      * @param cls
 76      */
 77     public void gotoExistActivity(Class<?> cls) {
 78         Intent intent;
 79         intent = new Intent(this.getApplicationContext(), cls);
 80         intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
 81         startActivity(intent);
 82     }
 83     /**
 84      * 新建一个activity打开
 85      * 
 86      * @param cls
 87      */
 88     public void gotoActivity(Class<?> cls) {
 89         Intent intent;
 90         intent = new Intent(this.getApplicationContext(), cls);
 91         startActivity(intent);
 92     }
 93     /**
 94      * @Title: toast
 95      * @Description: TODO(土司操作)
 96      * @param @param context
 97      * @param @param message 要显示的内容
 98      * @return void 返回类型
 99      * @throws
100      */
101     public void toast(String message) {
102         Toast.makeText(this.getApplicationContext(), message, LENGTH_TIME)
103         .show();
104     }
105     /**
106      * @Title: toast
107      * @Description: TODO(土司操作)
108      * @param @param context
109      * @param @param message 设定文件
110      * @return void 返回类型
111      * @throws
112      */
113     public void toast(int message) {
114         Toast.makeText(this.getApplicationContext(), message, LENGTH_TIME)
115         .show();
116     }
117     /**
118      * 从资源获取字符串
119      * 
120      * @param resId
121      * @return
122      */
123     public String getStr(int resId) {
124         return res.getString(resId);
125     }
126     /**
127      * @方法名: getDemon
128      * @描述: TODO(获取资源文件中的尺寸内容)
129      * @设定: @param mRid
130      * @设定: @return 设定文件
131      * @返回: int 返回类型
132      * @日期: 2014-6-30 下午2:14:11
133      * @throws
134      */
135     protected int getDemon(int mRid) {
136         return (int)res.getDimension(mRid);
137     }
138     /**
139      * 从EditText 获取字符串
140      * 
141      * @param editText
142      * @return
143      */
144     public String getStr(EditText editText) {
145         return editText.getText().toString();
146     }
147     @Override
148     public boolean onKeyDown(int keyCode, KeyEvent event) {
149         switch (keyCode) {
150         case KeyEvent.KEYCODE_BACK:
151             this.finish();
152         }
153         return super.onKeyDown(keyCode, event);
154     }
155 }

 

 
 

设计模式--模版设计模式

标签:

原文地址:http://www.cnblogs.com/androidsuperman/p/4537116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!