对于开发人员来讲,设计模式有时候就是一道坎,可是设计模式又很是有用,过了这道坎,它可让你水平提升一个档次。而在android开发中,必要的了解一些设计模式又是必须的,由于设计模式在Android源码中,能够说是无处不在。对于想系统的学习设计模式的同窗,这里推荐一本书,《大话设计模式》。android
面向对象的基础特征
面向对象的设计原则
单例模式
模板模式
适配器模式
工厂模式
代理模式
原型模式
策略模式
Build模式
观察者模式
装饰者模式
中介模式
门面模式设计模式
Build模式是很是常见的设计模式之一,写个笔记,记录一下个人学习过程和心得。api
首先了解一些Build模式的定义。网络
将一个复杂对象的构建与它的表示分离,使得一样的构建过程能够建立不一样的表示框架
可是看完这个定义,并无什么卵用,你依然不知道什么是Builder设计模式。在此我的的态度是学习设计模式这种东西,不要过分在乎其定义,定义每每是比较抽象的,学习它最好的例子就是经过样例代码。ide
咱们经过一个例子来引出Builder模式。假设有一个Car类,咱们经过该Car类来构建一大批汽车,这个Car类里有不少属性,最多见的好比颜色,价格,品牌,排量等等,而且咱们容许这些值不被设置,也就是容许为null,该类的定义以下。函数
public class Car { Color color; double price; String brand; String displacement; public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getDisplacement() { return displacement; } public void setDisplacement(String displacement) { this.displacement = displacement; } }
而后咱们为了方即可能会定义一个构造方法。post
public Car(Color color, double price, String brand, String displacement) { this.color = color; this.price = price; this.brand = brand; this. displacement = displacement; }
有时候,只想传部分参数,你还会定义以下相似的构造方法。学习
public Car(Color color) { this.color = color; } public Car(Color color, double price) { this.color = color; this.price = price; } public Car(Color color, double price, String brand) { this.color = color; this.price = price; this.brand = brand; }
因而你就能够这样建立各个须要的对象ui
Person p2=new Person(Color.read); Person p3=new Person(Color.blue,180000); Person p4=new Person(Color.green,21180, "小鹏"); Person p5=new Person(Color.white,17170,"法拉利","4.0L");
能够想象一下这样建立的坏处,若是四个参都是同一种数据类型,那各个参数究竟是什么意思,那得考读性了,。还有一个问题就是当有不少参数时,编写这个构造数就会显得异常麻烦,这时候若是换一个角度,试试Builder模式,你会发现代码的可读性一会儿就上去了。
咱们给Car增长一个静态内部类Builder类,并修改Car类的构造函数,代码以下。
public class Car { Color color; double price; String brand; String displacement; public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getDisplacement() { return displacement; } public void setDisplacement(String displacement) { this.displacement = displacement; } private Car(Builder builder) { this.color = builder.color; this.price = builder.price; this.brand = builder.brand; this. displacement = builder.displacement; } public static class Builder { Color color; double price; String brand; String displacement; public Builder color(Color color){ this.color=color; return this; } public Builder price(double price){ this.price=price; return this; } public Builder brand(String brand){ this.brand=brand; return this; } public Builder displacement(String displacement){ this.displacement=displacement; return this; } public Car build() { return new Car(this); } } }
这样,咱们就不会被Car类构造函数的各个入参搞得稀里糊涂了。
此外Builder类中的成员函数返回Builder对象自身,让它支持链式调用,使代码可读性大大加强。因而咱们就能够这样建立Car类。
new Car.Builder().color(Color.BLUE) .price(129800) .pailiang("1.5T") .brand("小鹏") .build();
有没有以为建立过程一会儿就变得那么清晰了。对应的值是什么属性一目了然,可读性大大加强。
综上,咱们总结一下build模式的要点:
1. 定义一个静态内部类Builder,内部的成员变量和外部类同样
2. Builder类经过一系列的方法用于成员变量的赋值,并返回当前对象自己(this)
3. Builder类提供一个外部类的建立方法(build、create……),该方法内部调用了外部类的一个私有构造函数,入参就是内部类Builder
4. 外部类提供一个私有构造函数供内部类调用,在该构造函数中完成成员变量的赋值,取值为Builder对象中对应的成变量的值
其实在Android中, Builder模式也是被大量的运用。好比常见的对话框的建立
AlertDialog.Builder builder=new AlertDialog.Builder(this); AlertDialog dialog=builder.setTitle("标题") .setIcon(android.R.drawable.ic_dialog_alert) .setView(R.layout.myview) .setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton(R.string.negative, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create(); dialog.show();
并且,各个第三方框架也大量的运用了Builder模式
如Gson中的GsonBuilder,代码太长了,就不贴了,有兴趣本身去看源码,这里只贴出其Builder的使用方法。
GsonBuilder builder=new GsonBuilder(); Gson gson=builder.setPrettyPrinting() .disableHtmlEscaping() .generateNonExecutableJson() .serializeNulls() .create();
再看看著名的网络请求框架OkHttp
Request. Builder builder=new Request.Builder(); Request request=builder.addHeader("","") .url("") .post(body) .build();
Builder模式一般做为配置类的构建器将配置的构建和表示分离开来,同时也是将配置从目标类中隔离出来,避免做为过多的setter方法,而且隐藏内部的细节。Builder模式比较常见的实现形式是经过链式调用,这样使得代码更加简洁、易懂。
内部类与外部类相互引用,可能会致使内存消耗比较大,不过鉴于如今的手机内存来说,这点几乎影响不大。