《Android源码 设计模式》读后心得笔记
学习是一种习惯,应该养成这种习惯。
读书计划时间:10天;开始于:9.8 号;
备注:中间使用一天用来调整(大概11天阅读)
开始… 9.8号:
01号学习:
1:关闭各种流(如:FileOutputStream 等)的公用类:Closeable
public final class CloseUtils {
private CloseUtils(){}
/**
*关闭Closeable 对象
*@time:2017/9/8
*/
public static void closeQuietly(Closeable closeable){
if(null!= closeable){
try{
closeable.close() ;
}catch(IOException e){
e.printStackTrace() ;
}
}
}
}
2: 通过上面的方式这样就可以保证代码的重用性了;下面说下具体的使用方式:
public void TestClose(String url){
FileOutputStream fileouts = null ;
try{
fileouts = new FileOutputStream(url);
//xxxxxxx具体的逻辑处理
}catch(FileNotFountException e){
e.printStackTrace();
}finally{
//普通的写法:1
/*
if(fileouts != null){
try{
fileouts.close();
}catch(IOException e){
e.printStackTrace();
}
}
*/
//优化后的写法:2
CloseUtils.closeQuietly(fileouts);
}
}
3:在21世纪早期 : 单一职责,开闭原则,里氏替换,接口隔离 以及 依赖倒置(也叫依赖反转)5个原则定义为 SOLID原则,作为面向对象的基本原则。
4: 要控制代码的耦合性:这样使程序 耦合性更低,稳定性更好。
5:单例模式。
02号学习…
1:原型模式:
例如:Intent 克隆
Uri uri = Uri.parse("smsto:080123456789");
Intent ist1 = new Intent(Intent.ACTION_SENDTO,uri);
ist1.putExtra("sms_body","The sms text");
//克隆副本 这样就具备了 ist1 的属性了。
Intent istClone = (Intent) ist1.clone();
startActivity(istClone);
03号学习…
1:工厂模式。
2:时势造英雄:策略模式。
3:状态模式:代码已经测试。
4: 目前看中的模式有:单例模式/工厂模式/策略模式(状态模式在特定情况下用也是很不错的选择)。
5: 责任链模式 。
6:观察者模式。
04号学习…
1:判断gone visible invisible 可以不用通过 0 、 4 、8 这些数字判断;
新的判断方式是:如:
if(view1.getVisibility() == GONE){//判断:view
//逻辑处理
}
2:通过try{}catch{} 来解决 可能出现的异常;是一个很不错的并且很必要的方式。
看到:P468页(书籍455页) 基本都是一些模式的讲解和使用方式 ,不做过多说明了。