建造者模式和AlertDialog源码解析

建造者模式

即按照这
我的理解是 :对同一个类型按照顺序对不同的属性进行赋值 然后完成建造,得到对象的过程.
就像建房子,建造者就是建筑工,产品就是房子.建筑工会对这个房子的属性:大门,地板,墙面等进行创建.完成后,交付房子.

不同于,工厂模式,它是针对不同的类型的对象进行生产,详情请参照我的博客–工厂模式,就像创建房子,关注于

建造者模式注重于房子的 零部件的组装的顺序,而工厂模式则注重于创建怎样的房子.

创建示例

目前,创建者模式,有很多变种.

方式1:标准模式

标准模式包括抽象建造者,具体建造者,指导者,产品

类图

《建造者模式和AlertDialog源码解析》

关键代码

public class AlertDialogBuilder extends DialogBuilder {
    
    AlertDialog alertDialog;
    
    public AlertDialogBuilder(){
        
        alertDialog=new AlertDialog();
    }
    
    
    /** @pdOid f2e872d8-2b7e-433a-a73a-b123600c1147 */
    public DialogBuilder createBackGround(String s) {
        // TODO: implement

        alertDialog.setBackground(s);
        return this;
    }
   
   /** @pdOid 132730f9-2fb8-461b-82ff-b8ad1fd6b644 */
   public DialogBuilder createShape(String s) {
       alertDialog.setShape(s);
      return this;
   }
   
   /** @pdOid 70ece84f-407e-4d0b-8f59-bf2666f8b56c */
   public DialogBuilder createTitle(String s) {
      // TODO: implement
       alertDialog.setTitle(s);
      return this;
   }

@Override
public AlertDialog build() {
    // TODO Auto-generated method stub
    return alertDialog;
}

}


public class DialogDirector {
   /** @pdOid 2526f4f0-f70e-4d84-a843-58429582f705 */
   private DialogBuilder dialogBuilder;
   
   /** @pdOid a9e2ae49-fffc-43fb-adb7-ab3582f85c31 */
   public  AlertDialog createDialog(String background,String shape,String title) {
       
       dialogBuilder= new AlertDialogBuilder();
      
      return  dialogBuilder.createBackGround(background).createShape(shape).createTitle(title).build();
   }

}

方式2:链式简版

只包括建造者类,产品参数内部类,产品类

类图

《建造者模式和AlertDialog源码解析》

参考代码

/**

 * 房子类,不能暴露自己的属性和方法
 *
 */
public class Room {
    private String window;    
    private String floor;
    private String doorl;
    
    public void apply(WorkBuilder.RoomParmas parmas)
    {
        window=parmas.window;
        floor=parmas.floor;
        doorl=parmas.door;
    }
    
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Room [window=" + window + ", floor=" + floor + ", doorl=" + doorl  + "]";
    }

    public String show()
    {
        return this.toString();
    }
    
}

public class WorkBuilder{
    
    
    private RoomParmas parmas;
    
    /**
     * 初始化建造者,初始化RoomParmas中间件
     */
    public WorkBuilder( ) { 
        this.parmas = new RoomParmas();
    }
    /**
     * 创建window,然后返回自身WorkBuilder,链式调用
     * @param window
     * @return
     */
    
    public  WorkBuilder makeWindow(String window ) {
        parmas.window=window;
        return this;
    }

    public WorkBuilder makeFloor(String floorCorlor) {
        parmas.floor=floorCorlor;
        return this;
    }
    public WorkBuilder makeDoor(String door) {
        parmas.door=door;
        return this;
    }
/**
 * 创建,创建对象,新建room对象,将中间量param传入Room,进行构建.
 * @return
 */
    public Room build() {
        Room room=new Room();
        room.apply(parmas);
        return room;
    }
    
    /**
     * 内部类来作为中间量,作为建造对象的参数传入
     * @author ccj
     *
     */
    class RoomParmas
    {
        public  String window;    
        public String floor;
        public String door;
        
    }

}

public class Test {
      
      public static void main(String[] args) {    
             Room room=new WorkBuilder()
                     .makeWindow("防弹玻璃")
                     .makeFloor("水泥地板")
                     .makeDoor("凯旋门").build();
                     ; //创建Room对象   
             System.out.println(room.show());   //工人交房子 
           }        
      
}

Android中的体现

创建者模式在android应用层代码的体现在 AlertDialog的创建过程
不同的是在源码中,在AlertDialog类中,有一个Builder类,在Builder类中有一个AlertController类,以及AlertController.AlertParams类.

Builder内部类:负责AlertDialogAlertController的交互中间件;

AlertController.AlertParams:负责初始化Alertdialog的属性值.
AlertController:负责AlertDialog的动作.

时序图调用

《建造者模式和AlertDialog源码解析》

代码流程

AlertDialog.Builder builder=new AlertDialog.Builder(this);

Dialog dialog=builder.create();

dialog.show();

详情请研究代码,这里不做多余描述,

1.AlertDialog.Builder builder = new AlertDialog.Builder(this);

初始化,Builder,AlertController.AlertParams,和AlertController.

    //创建者,构造方法负责样式theme
       public Builder(Context context, int theme) {
            P = new AlertController.AlertParams(new ContextThemeWrapper(
                    context, resolveDialogTheme(context, theme)));
            mTheme = theme;
        }

2.dialog = builder.create();

1.builder中有AlertController.AlertParams的引用
2.AlertController.AlertParams相当于创建者中的中间量,基本上Dilaog所有的属性都是在这里的 .

P.apply(dialog.mAlert);(mAlert是AlertController的引用).
AlertController.AlertParams中设置,然后将要创建的Dilaog属性,转移到AlertController.所有操作的真正的执行动作,监听等 都在AlertController中进行.


//创建者的create方法,按照流程新建AlertDialog对象
        public AlertDialog create() {
            final AlertDialog dialog = new AlertDialog(P.mContext, mTheme, 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;
        }

3.当activity执行dialog.show();

AlertDialog执行到声明周琦的oncreate时调用 mAlert.installContent();然后在此方法中setupView();展示dialog.

private void setupView() {
        final View parentPanel = mWindow.findViewById(R.id.parentPanel);
        final View defaultTopPanel = parentPanel.findViewById(R.id.topPanel);
        final View defaultContentPanel = parentPanel.findViewById(R.id.contentPanel);
        final View defaultButtonPanel = parentPanel.findViewById(R.id.buttonPanel);

        // Install custom content before setting up the title or buttons so
        // that we can handle panel overrides.
        final ViewGroup customPanel = (ViewGroup) parentPanel.findViewById(R.id.customPanel);
        setupCustomContent(customPanel);

总结

随着设计模式系列的深入,越来越能体会到设计模式的优雅.
建造者模式,顾名思义,就是对一个复杂的对象,进行一步步的构建,最终生成一个不同风格的产品的过程.

    原文作者:Chauncey_Chen
    原文地址: https://www.jianshu.com/p/3dd06ec5f94f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞