Java设计模式之建造者模式

一、什么是建造者模式


Builder模式也叫建造者模式或者生成器模式,是由GoF提出的23种设计模式中的一种。Builder模式是一种对象创建型模式之一,用来隐藏复合对象的创建过程,它把复合对象的创建过程加以抽象,通过子类继承和重载的方式,动
态地创建具有复合属性的对象。

二、建造者模式的结构

这里写图片描述


三、建造者模式应用场景

  • 对象的创建:Builder模式是为对象的创建而设计的模式
  • 创建的是一个复合对象:被创建的对象为一个具有复合属性的复合对象
  • 关注对象创建的各部分的创建过程:不同的工厂(这里指builder生成器)对产品属性有不同的创建方法

四、实例


//具体的构建对象
/*
 * 房子
 */
public class House {
    // 地板
    private String floor;
    // 墙
    private String wall;
    // 屋顶
    private String housetop;

    public String getFloor() {
        return floor;
    }

    public void setFloor(String floor) {
        this.floor = floor;
    }

    public String getWall() {
        return wall;
    }

    public void setWall(String wall) {
        this.wall = wall;
    }

    public String getHousetop() {
        return housetop;
    }

    public void setHousetop(String housetop) {
        this.housetop = housetop;
    }

}


/*
 * 工程队
 */
public interface HouseBuilder {
    //修地板
    public void makeFloor();
    //修墙
    public void makeWall();
    //修屋顶
    public void makeHousetop();
    public House getHouse();
}

public class HouseDirector {    

    public void makeHouse(HouseBuilder builder) {
        builder.makeFloor();
        builder.makeWall();
        builder.makeHousetop();
    }

}


public class GongyuBuilder implements HouseBuilder{
    House house = new House();

    public House getHouse() {
        return house;
    }

    public void makeFloor() {
        house.setFloor("公寓-->地板");
    }

    public void makeHousetop() {
        house.setHousetop("公寓-->房顶");
    }

    public void makeWall() {
        house.setWall("公寓-->墙");
    }

}

/*
 * 平房工程队
 */

public class PingFangBuilder implements HouseBuilder {
    House house = new House();

    public void makeFloor() {
        house.setFloor("平房-->地板");
    }

    public void makeHousetop() {
        house.setHousetop("平房-->房顶");
    }

    public void makeWall() {
        house.setWall("平房-->墙");
    }

    public House getHouse() {
        return house;
    }

}


public class MainClass {

    public static void main(String[] args) {
//      //客户直接造房子
//      House house = new House();
//      house.setFloor("地板");
//      house.setWall("墙");
//      house.setHousetop("屋顶");


        //由工程队来修
        HouseBuilder builder = new GongyuBuilder();
        //设计者来做
        HouseDirector director = new HouseDirector();
        director.makeHouse(builder);

        House house = builder.getHouse();
        System.out.println(house.getFloor());
        System.out.println(house.getWall());
        System.out.println(house.getHousetop());
    }

}

五、建造者模式在 Android中的运用(自定义PopupWindow)

/**
 * 
 * created by zero on 2016-08-19
 * 
 * 通过建造者模式构建一个popupwindow
 *
 */
public class CommonPopup extends PopupWindow
{
    private final Context context;
    private final String title;
    private final String content;
    private final IListenLeftCallBack iListenLeft;
    private final IListenRightCallBack iListenRight;
    private final String leftText;
    private final String rightText;

    public static class Builder{

        private final Context context;
        private String title;
        private String content;
        private IListenLeftCallBack iListenLeft;
        private IListenRightCallBack iListenRight;
        private String leftText;
        private String rightText;

        public Builder(Context context)
        {
            this.context = context;
        }

        public Builder setTitle(String title){
            this.title = title;
            return this;
        }

        public Builder setContent(String content){
            this.content = content;
            return this;
        }

        public Builder setListenLeft(IListenLeftCallBack iListenLeft){
            this.iListenLeft = iListenLeft;
            return this;
        }

        public Builder setLeftText(String leftText){
            this.leftText = leftText;
            return this;
        }

        public Builder setListenRight(IListenRightCallBack iListenRight){
            this.iListenRight = iListenRight;
            return this;
        }

        public Builder setRightText(String rightText){
            this.rightText = rightText;
            return this;
        }

        public CommonPopup build(){
            return new CommonPopup(this);
        }

    }

    public CommonPopup(Builder builder)
    {
        this.context = builder.context;
        this.title = builder.title;
        this.content = builder.content;
        this.iListenLeft = builder.iListenLeft;
        this.iListenRight = builder.iListenRight;
        this.leftText = builder.leftText;
        this.rightText = builder.rightText;
        initPopup();
    }

    private PopupWindow popupWindow;
    private View popView;
    private TextView txt_title;
    private TextView txt_info;

    private void initPopup(){
        popView = LayoutInflater.from(context).inflate(R.layout.popup_common_view, null);
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        popupWindow = new PopupWindow(popView, dm.widthPixels, dm.heightPixels);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        txt_title = (TextView) popView.findViewById(R.id.txt_title);
        if(TextUtils.isEmpty(title)){
            txt_title.setVisibility(View.GONE);
        }else{
            txt_title.setText(title);
        }
        txt_info = (TextView) popView.findViewById(R.id.txt_info);
        if(TextUtils.isEmpty(content)){
            txt_info.setVisibility(View.GONE);
        }else{
            txt_info.setText(content);
        }
        Button btn_right = (Button) popView.findViewById(R.id.btn_right);
        if(!TextUtils.isEmpty(rightText)){
            btn_right.setText(rightText);
            btn_right.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    popupWindow.dismiss();
                    if(iListenRight!=null){
                        iListenRight.rightListen();
                    }
                }
            });
        }else{
            btn_right.setVisibility(View.GONE);
        }
        Button btn_left = (Button) popView.findViewById(R.id.btn_left);
        if(!TextUtils.isEmpty(leftText)){
            btn_left.setText(leftText);
            btn_left.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    popupWindow.dismiss();
                    if(iListenLeft!=null){
                        iListenLeft.leftListen();
                    }
                }
            });
        }else{
            btn_left.setVisibility(View.GONE);
        }
    }

    public void showPopup(){
        if(popupWindow==null){
            initPopup();
        }
        popupWindow.showAtLocation(popView, 0, 0, 0);
    }
}

//接口类

public interface IListenLeftCallBack
{
    void leftListen();
}
public interface IListenRightCallBack
{
    void rightListen();
}

activity

/**
 * created by zero on 2016-08-19
 */
public class BuilderActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(drawView());
    }

    // 不想写xml,就直接动态编码了
    private View drawView() {
        // TODO Auto-generated method stub
        LayoutParams layout_params = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        LinearLayout ll_layout = new LinearLayout(this);
        ll_layout.setOrientation(LinearLayout.VERTICAL);
        ll_layout.setLayoutParams(layout_params);

        LayoutParams btn_params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        btn_params.gravity = Gravity.CENTER_HORIZONTAL;
        Button btn1 = new Button(this);
        btn1.setLayoutParams(btn_params);
        btn1.setText("click1");
        btn1.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new CommonPopup.Builder(BuilderActivity.this)
                        .setContent("真的要删除历史记录吗?").setRightText("确定")
                        .setListenRight(new IListenRightCallBack()
                        {

                            @Override
                            public void rightListen() {
                                // TODO Auto-generated method stub
                                Toast.makeText(BuilderActivity.this, "right1",
                                        Toast.LENGTH_LONG).show();
                            }
                        }).build().showPopup();
            }
        });

        Button btn2 = new Button(this);
        btn2.setLayoutParams(btn_params);
        btn2.setText("click2");
        btn2.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new CommonPopup.Builder(BuilderActivity.this)
                        .setTitle("提示")
                        .setContent("真的要删除历史记录吗?")
                        .setLeftText("取消")
                        .setListenLeft(new IListenLeftCallBack()
                        {

                            @Override
                            public void leftListen() {
                                // TODO Auto-generated method stub
                                Toast.makeText(BuilderActivity.this, "left2",
                                        Toast.LENGTH_LONG).show();
                            }
                        }).setRightText("确定")
                        .setListenRight(new IListenRightCallBack()
                        {

                            @Override
                            public void rightListen() {
                                // TODO Auto-generated method stub
                                Toast.makeText(BuilderActivity.this, "right2",
                                        Toast.LENGTH_LONG).show();
                            }
                        }).build().showPopup();
            }
        });

        ll_layout.addView(btn1);
        ll_layout.addView(btn2);
        return ll_layout;
    }
}

运行 效果图:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值