对于开发人员来说,设计模式是提高编程水平的一个非常重要的门槛. 以前写过一些设计模式的文章,有兴趣可以参考一下. 这一次来写一下android中设计模式的应用,相信你会对设计模式有一个更深入的理解. 什么是设计模式? 设计模式大总结-创建型模式 设计模式大总结2-结构型模式
调用方式
Person.Builder person = new Person.Builder(); person.age(1).name("11");Builder模式是不是也很面熟?是的,我们的AlertDialog就是这样子写的
AlertDialog.Builder builder=new AlertDialog.Builder(this); AlertDialog dialog=builder.setTitle("标题") .setIcon(android.R.drawable.xx) .setView(R.layout.xx) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create(); dialog.show();还有StringBuilder
StringBuilder builder = new StringBuilder(); builder.append("aa").append("bb");研究过浅拷贝和深拷贝的人对这样的例子一定不陌生
package com.example.king.designpatternsdemoforandroid; /** * Created by king on 2017/3/9. */ public class Person2 implements Cloneable{ private String name; private int age; private double height; private double weight; public Person2(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", height=" + height + ", weight=" + weight + '}'; } @Override public Object clone(){ Person2 person=null; try { person=(Person2)super.clone(); person.name=this.name; person.weight=this.weight; person.height=this.height; person.age=this.age; } catch (CloneNotSupportedException e) { e.printStackTrace(); } return person; } }调用
Person2 p=new Person2(); p.setAge(18); p.setName("张三"); p.setHeight(178); p.setWeight(65); System.out.println(p); Person2 p1= (Person2) p.clone(); System.out.println(p1); p1.setName("李四"); System.out.println(p); System.out.println(p1);结果
System.out: Person{name='张三', age=18, height=178.0, weight=65.0} System.out: Person{name='张三', age=18, height=178.0, weight=65.0} System.out: Person{name='张三', age=18, height=178.0, weight=65.0} System.out: Person{name='李四', age=18, height=178.0, weight=65.0}其实这就是一个原型模式
我们经常用的Intent就是一个原型模式的使用者
@Override public Object clone() { return new Intent(this); } /** * Copy constructor. */ public Intent(Intent o) { this.mAction = o.mAction; this.mData = o.mData; this.mType = o.mType; this.mPackage = o.mPackage; this.mComponent = o.mComponent; this.mFlags = o.mFlags; if (o.mCategories != null) { this.mCategories = new ArraySet<String>(o.mCategories); } if (o.mExtras != null) { this.mExtras = new Bundle(o.mExtras); } if (o.mSourceBounds != null) { this.mSourceBounds = new Rect(o.mSourceBounds); } if (o.mSelector != null) { this.mSelector = new Intent(o.mSelector); } if (o.mClipData != null) { this.mClipData = new ClipData(o.mClipData); } }