当前位置: 移动技术网 > IT编程>移动开发>Android > 深入理解Android中的建造者模式

深入理解Android中的建造者模式

2019年07月24日  | 移动技术网IT编程  | 我要评论

ca1226,bubibu音译,沟女四大天王粤语

前言

在android开发过程中,我发现很多安卓源代码里应用了设计模式,比较常用的有适配器模式(各种adapter),建造者模式(alert dialog的构建)等等。虽然我们对大多数设计模式都有所了解,但是在应用设计模式的这个方面,感觉很多人在这方面有所不足。所以这篇文章我们一起深入的理解android中的建造者模式。

建造者模式(builder pattern)也叫生成器模式,其定义如下:

separate the construction of a complex object from its representation so that the same construction process can create different representations.将一个复杂对象的构建与它的标示分离,这样的话就可以使同样的构建过程可以创建不同的表示。

我的理解:就是把一个产品(对象)表示(展示)和构建(创建)过程分离开来,这样产品的构建流程相同却可以有不同的产品表示。

应用场景

这里举出android中常见的例子:

android中的alertdialog对话框的构建过程就是建造者模式的典型应用。


看一下builder源码

 public static class builder {
  private final alertcontroller.alertparams p;

  public builder(context context) {
    this(context, resolvedialogtheme(context, 0));
  }


  public builder(context context, int themeresid) {
    p = new alertcontroller.alertparams(new contextthemewrapper(
        context, resolvedialogtheme(context, themeresid)));
  }

  //各种set参数方法
  settitle()
  ...
  ...
  ...

  /**
   * creates an {@link alertdialog} with the arguments supplied to this
   * builder.
   * <p>
   * calling this method does not display the dialog. if no additional
   * processing is needed, {@link #show()} may be called instead to both
   * create and display the dialog.
   */
  public alertdialog create() {
    // context has already been wrapped with the appropriate theme.
    final alertdialog dialog = new alertdialog(p.mcontext, 0, false);
    p.apply(dialog.malert);
    dialog.setcancelable(p.mcancelable);
    if (p.mcancelable) {
      dialog.setcanceledontouchoutside(true);
    }
    dialog.setoncancellistener(p.moncancellistener);
    dialog.setondismisslistener(p.mondismisslistener);
    if (p.monkeylistener != null) {
      dialog.setonkeylistener(p.monkeylistener);
    }
    return dialog;
  }


  // 这个show方法是builder对象的,里面封装了create()和show()可以直接调取创建并显示对话框
  public alertdialog show() {
    final alertdialog dialog = create();
    dialog.show();
    return dialog;
  }
}

分析源码可以看到内部类builder是用来构建这个对话框的:

    1、创建builder的时候,会把这个alertcontroller.alertparams p;对象p创建new出来

    2、在对bilder设置各种参数的时,这些参数都存在了对象p中

    3、然后builder参数设置完毕后在调用create方法。

final alertdialog dialog = new alertdialog(p.mcontext, 0, false);
p.apply(dialog.malert); // malert是外部类中的

这个方法中会首先调用外部类alertdialog的构造方法,new出一个外部类对象,然后p.apply()方法会将p这个对象作为内部类的属性赋值给alertcontroller的对象malert。这样就完成了一次的构建。

下面是alertdialog的部分源码

public class alertdialog extends dialog implements dialoginterface {
  // 这个对象用来承接builder内部所设置的参数
  private alertcontroller malert;

  //以下几个构造方法决定只能通过内部类builder来构建
  protected alertdialog(context context) {
    this(context, 0);
  }

  protected alertdialog(context context, boolean cancelable,   oncancellistener cancellistener) {
    this(context, 0);
    setcancelable(cancelable);
    setoncancellistener(cancellistener);
  }


  protected alertdialog(context context, @styleres int themeresid) {
    this(context, themeresid, true);
  }

  alertdialog(context context, @styleres int themeresid, boolean createcontextthemewrapper) {
    super(context, createcontextthemewrapper ? resolvedialogtheme(context, themeresid) : 0,
      createcontextthemewrapper);

    mwindow.alwaysreadcloseontouchattr();
    malert = new alertcontroller(getcontext(), this, getwindow());
    }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位android开发者们能有所帮助,如果有疑问大家可以留言交流。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网