原标题:老司机带你轻松入门架构师----如何重构Android的v4包的部分源码
前言
过年回家忙着干活,忙着给亲戚的孩子发红包,好累,忙里偷闲打开studio看了一下v4包,之前看过几个类,这次是每个类都看了一下,原来Android的v4包的源码也有一些是写的不是那么友好的,还有很多改善空间。
下面就拿其中的android.support.v4.text这个包里面的 TextUtilsCompat和 TextUtilsCompatJellybeanMr1这两个类来做一个具体讲解。
一、首先看一下Androidv4包下面的 TextUtilsCompat 和 TextUtilsCompatJellybeanMr1 源码:(一)TextUtilsCompat 源码:
public
final
classTextUtilsCompat{
privatestaticclassTextUtilsCompatImpl{ TextUtilsCompatImpl() { }
@NonNullpublicString htmlEncode(@NonNull String s){ StringBuilder sb = newStringBuilder();
charc;
for( inti = 0; i < s.length(); i++) { c = s.charAt(i);
switch(c) {
case'
case'>': sb.append( ">"); //$NON-NLS-1$break;
case'&': sb.append( "&"); //$NON-NLS-1$break;
case''':
//http://www.w3.org/TR/xhtml1// The named character reference ' (the apostrophe, U+0027) was// introduced in XML 1.0 but does not appear in HTML. Authors should// therefore use ' instead of ' to work as expected in HTML 4// user agents.sb.append( "'"); //$NON-NLS-1$break;
case'"': sb.append( """); //$NON-NLS-1$break;
default: sb.append(c); } }
returnsb.toString(); }
publicintgetLayoutDirectionFromLocale(@Nullable Locale locale){
if(locale != null&& !locale.equals(ROOT)) {
finalString Subtag = ICUCompat.maximizeAndGet(locale);
if(Subtag == null) returngetLayoutDirectionFromFirstChar(locale);
// This is intentionally limited to Arabic and Hebrew s, since olde
// versions of Android platform only considered those s to be right-to-left.if(Subtag.equalsIgnoreCase(ARAB__SUBTAG) || Subtag.equalsIgnoreCase(HEBR__SUBTAG)) {
returnViewCompat.LAYOUT_DIRECTION_RTL; } }
returnViewCompat.LAYOUT_DIRECTION_LTR; }
/** * Fallback algorithm to detect the locale direction. Rely on the first char of the * localized locale name. This will not work if the localized locale name is in English * (this is the case for ICU 4.4 and "Urdu" ) * *@paramlocale *@returnthe layout direction. This may be one of: * {@link ViewCompat#LAYOUT_DIRECTION_LTR} or * {@link ViewCompat#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical s will be supported */privatestaticintgetLayoutDirectionFromFirstChar(@NonNull Locale locale){
switch(Character.getDirectionality(locale.getDisplayName(locale).charAt( 0))) {
caseCharacter.DIRECTIONALITY_RIGHT_TO_LEFT:
caseCharacter.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
returnViewCompat.LAYOUT_DIRECTION_RTL;
caseCharacter.DIRECTIONALITY_LEFT_TO_RIGHT:
default:
returnViewCompat.LAYOUT_DIRECTION_LTR; } } }
privatestaticclassTextUtilsCompatJellybeanMr1ImplextendsTextUtilsCompatImpl{ TextUtilsCompatJellybeanMr1Impl() { }
@Override@NonNullpublicString htmlEncode(@NonNull String s){
returnTextUtilsCompatJellybeanMr1.htmlEncode(s); }
@OverridepublicintgetLayoutDirectionFromLocale(@Nullable Locale locale){
returnTextUtilsCompatJellybeanMr1.getLayoutDirectionFromLocale(locale); } }
privatestaticfinalTextUtilsCompatImpl IMPL;
static{
finalintversion = Build.VERSION.SDK_INT;
if(version >= 17) { // JellyBean MR1IMPL = newTextUtilsCompatJellybeanMr1Impl(); } else{ IMPL = newTextUtilsCompatImpl(); } }
/** * Html-encode the string. *@params the string to be encoded *@returnthe encoded string */@NonNullpublicstaticString htmlEncode(@NonNull String s){
returnIMPL.htmlEncode(s); }
/** * Return the layout direction for a given Locale * *@paramlocale the Locale for which we want the layout direction. Can be null. *@returnthe layout direction. This may be one of: * {@link ViewCompat#LAYOUT_DIRECTION_LTR} or * {@link ViewCompat#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical s will be supported */publicstaticintgetLayoutDirectionFromLocale(@Nullable Locale locale){
returnIMPL.getLayoutDirectionFromLocale(locale); }
publicstaticfinalLocale ROOT = newLocale( "", "");
staticString ARAB__SUBTAG = "Arab";
staticString HEBR__SUBTAG = "Hebr";
privateTextUtilsCompat(){} } (二)TextUtilsCompatJellybeanMr1 源码: /** * Jellybean MR1 - specific TextUtils API access. */@RequiresApi( 17)
@TargetApi( 17)
classTextUtilsCompatJellybeanMr1{
@NonNullpublicstaticString htmlEncode(@NonNull String s){
returnTextUtils.htmlEncode(s); }
publicstaticintgetLayoutDirectionFromLocale(@Nullable Locale locale){
returnTextUtils.getLayoutDirectionFromLocale(locale); } }
二、分析一下以上源码的一些需要改进的问题(仅个人理解,如有不同意见,欢迎提出):
通过以上源码来看,看起来确实有点不是很舒服。
(一)排列顺序有点乱,我格式化了一下,如下,看的稍微清楚了一些: /** * 格式化之后的TextUtilsCompat类 */publicclassTextUtilsCompat{
privatestaticfinalTextUtilsCompatImpl IMPL;
publicstaticfinalLocale ROOT = newLocale( "", "");
staticString ARAB__SUBTAG = "Arab";
staticString HEBR__SUBTAG = "Hebr";
privateTextUtilsCompat(){}
static{
finalintversion = Build.VERSION.SDK_INT;
if(version >= 17) { // JellyBean MR1IMPL = newTextUtilsCompatJellybeanMr1Impl(); } else{ IMPL = newTextUtilsCompatImpl(); } }
/** * Html-encode the string. *@params the string to be encoded *@returnthe encoded string */@NonNullpublicstaticString htmlEncode(@NonNull String s){
returnIMPL.htmlEncode(s); }
/** * Return the layout direction for a given Locale * *@paramlocale the Locale for which we want the layout direction. Can be null. *@returnthe layout direction. This may be one of: * {@link ViewCompat#LAYOUT_DIRECTION_LTR} or * {@link ViewCompat#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical s will be supported */publicstaticintgetLayoutDirectionFromLocale(@Nullable Locale locale){
returnIMPL.getLayoutDirectionFromLocale(locale); }
privatestaticclassTextUtilsCompatImpl{ TextUtilsCompatImpl() {}
@NonNullpublicString htmlEncode(@NonNull String s){ StringBuilder sb = newStringBuilder();
charc;
for( inti = 0; i < s.length(); i++) { c = s.charAt(i);
switch(c) {
case'
case'>': sb.append( ">"); //$NON-NLS-1$break;
case'&': sb.append( "&"); //$NON-NLS-1$break;
case''':
//http://www.w3.org/TR/xhtml1// The named character reference ' (the apostrophe, U+0027) was// introduced in XML 1.0 but does not appear in HTML. Authors should// therefore use ' instead of ' to work as expected in HTML 4// user agents.sb.append( "'"); //$NON-NLS-1$break;
case'"': sb.append( """); //$NON-NLS-1$break;
default: sb.append(c); } }
returnsb.toString(); }
publicintgetLayoutDirectionFromLocale(@Nullable Locale locale){
if(locale != null&& !locale.equals(ROOT)) {
finalString Subtag = ICUCompat.maximizeAndGet(locale);
if(Subtag == null) {
returngetLayoutDirectionFromFirstChar(locale); }
// This is intentionally limited to Arabic and Hebrew s, since older// versions of Android platform only considered those s to be right-to-left.if(Subtag.equalsIgnoreCase(ARAB__SUBTAG) || Subtag.equalsIgnoreCase(HEBR__SUBTAG)) {
returnViewCompat.LAYOUT_DIRECTION_RTL; } }
returnViewCompat.LAYOUT_DIRECTION_LTR; }
/** * Fallback algorithm to detect the locale direction. Rely on the first char of the * localized locale name. This will not work if the localized locale name is in English * (this is the case for ICU 4.4 and "Urdu" ) * *@paramlocale *@returnthe layout direction. This may be one of: * {@link ViewCompat#LAYOUT_DIRECTION_LTR} or * {@link ViewCompat#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical s will be supported */privatestaticintgetLayoutDirectionFromFirstChar(@NonNull Locale locale){
switch(Character.getDirectionality(locale.getDisplayName(locale).charAt( 0))) {
caseCharacter.DIRECTIONALITY_RIGHT_TO_LEFT:
caseCharacter.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
returnViewCompat.LAYOUT_DIRECTION_RTL;
caseCharacter.DIRECTIONALITY_LEFT_TO_RIGHT:
default:
returnViewCompat.LAYOUT_DIRECTION_LTR; } } }
privatestaticclassTextUtilsCompatJellybeanMr1ImplextendsTextUtilsCompatImpl{ TextUtilsCompatJellybeanMr1Impl() { }
@Override@NonNullpublicString htmlEncode(@NonNull String s){
returnTextUtilsCompatJellybeanMr1.htmlEncode(s); }
@OverridepublicintgetLayoutDirectionFromLocale(@Nullable Locale locale){
returnTextUtilsCompatJellybeanMr1.getLayoutDirectionFromLocale(locale); } } } (二)TextUtilsCompat 这个类里面有两个内部类,一个是TextUtilsCompatImpl,一个是TextUtilsCompatJellybeanMr1Impl,TextUtilsCompatJellybeanMr1Impl是继承自TextUtilsCompatImpl的。 (三)从静态代码块看出,api 大于17 使用 new TextUtilsCompatJellybeanMr1Impl(); api小于17 使用TextUtilsCompatImpl。TextUtilsCompat,TextUtilsCompatImpl和TextUtilsCompatJellybeanMr1Impl里面都有 htmlEncode 方法和 getLayoutDirectionFromLocale 方法。
静态代码块里面通过TextUtilsCompatImpl IMPL这个常量来判断,当api大于17用TextUtilsCompatJellybeanMr1Impl,否则用TextUtilsCompatImpl,然后htmlEncode方法调用了对应内部类里面的htmlEncode方法,getLayoutDirectionFromLocale调用了对应内部类里面的getLayoutDirectionFromLocale方法。
(四)TextUtilsCompatImpl和TextUtilsCompatJellybeanMr1Impl里面都有 htmlEncode 方法和 getLayoutDirectionFromLocale 方法,看看它们的区别。
(1)TextUtilsCompatJellybeanMr1Impl这个内部类的方法解析:
htmlEncode(@NonNull String s) 方法 返回的是: TextUtilsCompatJellybeanMr1.htmlEncode(s); ⇒ 调用了TextUtils.htmlEncode(s);
getLayoutDirectionFromLocale(@Nullable Locale locale) 方法返回的是: TextUtilsCompatJellybeanMr1.getLayoutDirectionFromLocale(locale); ⇒ 调用了TextUtils.getLayoutDirectionFromLocale(locale);
(2)TextUtilsCompatImpl这个内部类的方法解析:
htmlEncode(@NonNull String s) 方法 返回的是: 在这个方法内部写了一遍,跟TextUtils.htmlEncode(s);方法里面的一模一样。
getLayoutDirectionFromLocale(@Nullable Locale locale) 方法返回的是: 重新写了一遍,这个方法是真正有所区别的地方。
三、根据我做过项目用到的MVP的开发模式,我把共同的htmlEncode方法和getLayoutDirectionFromLocale方法抽取出一个接口,然后分别用两个实现类去实现接口,然后用TextUtilsCompat这个类去判断调用哪个实现类的方法,这样看起来更直观一些。具体步骤如下:(一)抽取公共接口ITextUtilsCompat
/** * 抽取公用的接口 */
public
interfaceITextUtilsCompat{
/** * Html-encode the string. *@params the string to be encoded *@returnthe encoded string */publicString htmlEncode(@NonNull String s);
/** * Return the layout direction for a given Locale * *@paramlocale the Locale for which we want the layout direction. Can be null. *@returnthe layout direction. This may be one of: * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} or * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical s will be supported */publicintgetLayoutDirectionFromLocale(@Nullable Locale locale); } (二)写一个TextUtilsCompatJellybeanMr1实现ITextUtilsCompat 接口,然后把之前TextUtilsCompatJellybeanMr1类里面的复制过来,具体如下: /** * 兼容Android 17+ 版本 * Jellybean MR1 - specific TextUtils API access. */@RequiresApi( 17)
@TargetApi( 17)
classTextUtilsCompatJellybeanMr1implementsITextUtilsCompat{
@Override@NonNullpublicString htmlEncode(@NonNull String s){
returnTextUtils.htmlEncode(s); }
@OverridepublicintgetLayoutDirectionFromLocale(@Nullable Locale locale){
returnTextUtils.getLayoutDirectionFromLocale(locale); } } (三)写一个类TextUtilsCompatImpl实现ITextUtilsCompat 接口,然后把之前TextUtilsCompat类里面的有关代码复制过来,具体如下: /** * Android 17以下版本使用这个类 */classTextUtilsCompatImplimplementsITextUtilsCompat{
publicstaticfinalLocale ROOT = newLocale( "", "");
staticString ARAB__SUBTAG = "Arab";
staticString HEBR__SUBTAG = "Hebr";
@Override@NonNullpublicString htmlEncode(@NonNull String s){ StringBuilder sb = newStringBuilder();
charc;
for( inti = 0; i < s.length(); i++) { c = s.charAt(i);
switch(c) {
case'
case'>': sb.append( ">"); //$NON-NLS-1$break;
case'&': sb.append( "&"); //$NON-NLS-1$break;
case''':
//http://www.w3.org/TR/xhtml1// The named character reference ' (the apostrophe, U+0027) was// introduced in XML 1.0 but does not appear in HTML. Authors should// therefore use ' instead of ' to work as expected in HTML 4// user agents.sb.append( "'"); //$NON-NLS-1$break;
case'"': sb.append( """); //$NON-NLS-1$break;
default: sb.append(c); } }
returnsb.toString(); }
@OverridepublicintgetLayoutDirectionFromLocale(@Nullable Locale locale){
if(locale != null&& !locale.equals(ROOT)) {
finalString Subtag = ICUCompat.maximizeAndGet(locale);
if(Subtag == null) {
returngetLayoutDirectionFromFirstChar(locale); }
// This is intentionally limited to Arabic and Hebrew s, since older// versions of Android platform only considered those s to be right-to-left.if(Subtag.equalsIgnoreCase(ARAB__SUBTAG) || Subtag.equalsIgnoreCase(HEBR__SUBTAG)) {
returnViewCompat.LAYOUT_DIRECTION_RTL; } }
returnViewCompat.LAYOUT_DIRECTION_LTR; }
/** * Fallback algorithm to detect the locale direction. Rely on the first char of the * localized locale name. This will not work if the localized locale name is in English * (this is the case for ICU 4.4 and "Urdu" ) * *@paramlocale *@returnthe layout direction. This may be one of: * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} or * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical s will be supported */privatestaticintgetLayoutDirectionFromFirstChar(@NonNull Locale locale){
switch(Character.getDirectionality(locale.getDisplayName(locale).charAt( 0))) {
caseCharacter.DIRECTIONALITY_RIGHT_TO_LEFT:
caseCharacter.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
returnViewCompat.LAYOUT_DIRECTION_RTL;
caseCharacter.DIRECTIONALITY_LEFT_TO_RIGHT:
default:
returnViewCompat.LAYOUT_DIRECTION_LTR; } } } (四)封装TextUtilsCompat给调用者使用,具体如下: /** * v4包下面的TextUtilsCompat的简单优化 * 这里使用的是策略模式,根据不同api版本调用不同的接口实现类 * 这样写更好维护。 */publicfinalclassTextUtilsCompat{
privatestaticfinalITextUtilsCompat IMPL;
privateTextUtilsCompat(){}
static{
finalintversion = Build.VERSION.SDK_INT;
// JellyBean MR1 大于等于17if(version >= Build.VERSION_CODES.JELLY_BEAN_MR1) { IMPL = newTextUtilsCompatJellybeanMr1(); } else{ IMPL = newTextUtilsCompatImpl(); } }
/** * Html-encode the string. *@params the string to be encoded *@returnthe encoded string */@NonNullpublicstaticString htmlEncode(@NonNull String s){
returnIMPL.htmlEncode(s); }
/** * Return the layout direction for a given Locale * *@paramlocale the Locale for which we want the layout direction. Can be null. *@returnthe layout direction. This may be one of: * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} or * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical s will be supported */publicstaticintgetLayoutDirectionFromLocale(@Nullable Locale locale){
returnIMPL.getLayoutDirectionFromLocale(locale); } } 到此,TextUtilsCompat 这个类就封装完了。看完之后是不是很清爽?其实还有很多类似的类都可以根据类似的方式做一下改进的。源码不是完美的,只要掌握以上示例代码的思想还是很容易的让代码更好理解,更简洁清晰的。
责任编辑: