Java设计模式-建造者模式

本文探讨了Java设计模式中的建造者模式,适合于创建复杂对象且构造过程需要多种不同方式的情况。通过实例,解释了建造者模式的使用场景、UML图,并提供了代码示例,还展示了在Android开发中的具体应用。
摘要由CSDN通过智能技术生成

1:使用场景

1)相同的方法,不同的执行顺序,产生不同的事件结果

2)多个部件或零件,都可以装配到一个对象中,但是产生的运行结果又不同

3)产品类非常复杂,或者产品类中的调用顺序不同产生了不同的作用

4)当初始化一个对象特别复杂,如参数多。


2:UML图


3:代码举例

public abstract class Computer {
    protected String mBoard;
    protected String mDisplay;
    protected String mOS;
    
    public void setBoard(String board){
    	mBoard=board;
    }
    
    public void setDisplay(String display){
    	mDisplay=display;
    }
    
    public abstract void setOS();
    
    
}

public  class Macbook extends Computer{
    public Macbook(){
    	
    }
    
    public  void setOS(){
    	mOS= "Mac OS X 10.10";
    }
    
    
}

public abstract class Builder {
	
    public abstract void buildBoard(String board);
    
    public abstract void buildDisplay(String display);
    
    public abstract void buildOS();
    
    public abstract Computer createComputer();
}

public  class MacbookBuilder extends Builder{
	Computer mComputer=new Macbook();
	

	@Override
	public void buildBoard(String board) {
		mComputer.setBoard(board);
	}

	@Override
	public void buildDisplay(String display) {
		mComputer.setDisplay(display);
	}

	@Override
	public void buildOS() {
		mComputer.setOS();
	}

	@Override
	public Computer createComputer() {
		return mComputer;
	}
	
    
}

public class Director {
	Builder mBuilder=null;
	
	public Director(Builder b){
		b=mBuilder;
	}

	public void construct(String board,String display){
		mBuilder.buildBoard(board);
		mBuilder.buildDisplay(display);
		mBuilder.buildOS();
	}
}


public  class Client {
	public  static void main(String[] args){
		
		Builder mBuilder=new MacbookBuilder();
		
		Director mDirector=new Director(mBuilder);
		
		mDirector.construct("Intel主板", "Retina显示器");
	}

}



4: Android中的建造者模式

private void showDialog(Context context) {  
    AlertDialog.Builder builder = new AlertDialog.Builder(context);  
    builder.setIcon(R.drawable.icon);  
    builder.setTitle("Title");  
    builder.setMessage("Message");  
    builder.setPositiveButton("Button1",  
            new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int whichButton) {  
                    setTitle("点击了对话框上的Button1");  
                }  
            });  
    builder.setNeutralButton("Button2",  
            new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int whichButton) {  
                    setTitle("点击了对话框上的Button2");  
                }  
            });  
    builder.setNegativeButton("Button3",  
            new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int whichButton) {  
                    setTitle("点击了对话框上的Button3");  
                }  
            });  
    builder.create().show();  // 构建AlertDialog, 并且显示
} 

public static class Builder {
    private final AlertController.AlertParams P;
    private int mTheme;

    public Builder(Context context) {
        this(context, resolveDialogTheme(context, 0));
    }

    public Builder(Context context, int theme) {
        P = new AlertController.AlertParams(new ContextThemeWrapper(
                context, resolveDialogTheme(context, theme)));
        mTheme = theme;
    }

    public Context getContext() {
        return P.mContext;
    }

    public Builder setTitle(int titleId) {
        P.mTitle = P.mContext.getText(titleId);
        return this;
    }

    public Builder setTitle(CharSequence title) {
        P.mTitle = title;
        return this;
    }

    public Builder setCustomTitle(View customTitleView) {
        P.mCustomTitleView = customTitleView;
        return this;
    }

    public Builder setMessage(int messageId) {
        P.mMessage = P.mContext.getText(messageId);
        return this;
    }

    public Builder setMessage(CharSequence message) {
        P.mMessage = message;
        return this;
    }

    public Builder setIcon(int iconId) {
        P.mIconId = iconId;
        return this;
    }

    public Builder setIcon(Drawable icon) {
        P.mIcon = icon;
        return this;
    }

    public Builder setIconAttribute(int attrId) {
        TypedValue out = new TypedValue();
        P.mContext.getTheme().resolveAttribute(attrId, out, true);
        P.mIconId = out.resourceId;
        return this;
    }

    public Builder setPositiveButton(int textId, final OnClickListener listener) {
        P.mPositiveButtonText = P.mContext.getText(textId);
        P.mPositiveButtonListener = listener;
        return this;
    }

    public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
        P.mPositiveButtonText = text;
        P.mPositiveButtonListener = listener;
        return this;
    }

    public Builder setNegativeButton(int textId, final OnClickListener listener) {
        P.mNegativeButtonText = P.mContext.getText(textId);
        P.mNegativeButtonListener = listener;
        return this;
    }

    public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
        P.mNegativeButtonText = text;
        P.mNegativeButtonListener = listener;
        return this;
    }

    public Builder setNeutralButton(int textId, final OnClickListener listener) {
        P.mNeutralButtonText = P.mContext.getText(textId);
        P.mNeutralButtonListener = listener;
        return this;
    }

    public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {
        P.mNeutralButtonText = text;
        P.mNeutralButtonListener = listener;
        return this;
    }

    public Builder setCancelable(boolean cancelable) {
        P.mCancelable = cancelable;
        return this;
    }

    public Builder setOnCancelListener(OnCancelListener onCancelListener) {
        P.mOnCancelListener = onCancelListener;
        return this;
    }

    public Builder setOnDismissListener(OnDismissListener onDismissListener) {
        P.mOnDismissListener = onDismissListener;
        return this;
    }

    public Builder setOnKeyListener(OnKeyListener onKeyListener) {
        P.mOnKeyListener = onKeyListener;
        return this;
    }

    public Builder setItems(int itemsId, final OnClickListener listener) {
        P.mItems = P.mContext.getResources().getTextArray(itemsId);
        P.mOnClickListener = listener;
        return this;
    }

    public Builder setItems(CharSequence[] items, final OnClickListener listener) {
        P.mItems = items;
        P.mOnClickListener = listener;
        return this;
    }

    public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
        P.mAdapter = adapter;
        P.mOnClickListener = listener;
        return this;
    }

    public Builder setCursor(final Cursor cursor, final OnClickListener listener,
            String labelColumn) {
        P.mCursor = cursor;
        P.mLabelColumn = labelColumn;
        P.mOnClickListener = listener;
        return this;
    }

    public Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems, 
            final OnMultiChoiceClickListener listener) {
        P.mItems = P.mContext.getResources().getTextArray(itemsId);
        P.mOnCheckboxClickListener = listener;
        P.mCheckedItems = checkedItems;
        P.mIsMultiChoice = true;
        return this;
    }

    public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, 
            final OnMultiChoiceClickListener listener) {
        P.mItems = items;
        P.mOnCheckboxClickListener = listener;
        P.mCheckedItems = checkedItems;
        P.mIsMultiChoice = true;
        return this;
    }

    public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, 
            final OnMultiChoiceClickListener listener) {
        P.mCursor = cursor;
        P.mOnCheckboxClickListener = listener;
        P.mIsCheckedColumn = isCheckedColumn;
        P.mLabelColumn = labelColumn;
        P.mIsMultiChoice = true;
        return this;
    }

    public Builder setSingleChoiceItems(int itemsId, int checkedItem, 
            final OnClickListener listener) {
        P.mItems = P.mContext.getResources().getTextArray(itemsId);
        P.mOnClickListener = listener;
        P.mCheckedItem = checkedItem;
        P.mIsSingleChoice = true;
        return this;
    }

    public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, 
            final OnClickListener listener) {
        P.mCursor = cursor;
        P.mOnClickListener = listener;
        P.mCheckedItem = checkedItem;
        P.mLabelColumn = labelColumn;
        P.mIsSingleChoice = true;
        return this;
    }

    public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
        P.mItems = items;
        P.mOnClickListener = listener;
        P.mCheckedItem = checkedItem;
        P.mIsSingleChoice = true;
        return this;
    } 

    public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {
        P.mAdapter = adapter;
        P.mOnClickListener = listener;
        P.mCheckedItem = checkedItem;
        P.mIsSingleChoice = true;
        return this;
    }

    public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
        P.mOnItemSelectedListener = listener;
        return this;
    }

    public Builder setView(View view) {
        P.mView = view;
        P.mViewSpacingSpecified = false;
        return this;
    }

    public Builder setInverseBackgroundForced(boolean useInverseBackground) {
        P.mForceInverseBackground = useInverseBackground;
        return this;
    }

    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;
    }

    public AlertDialog show() {
        AlertDialog dialog = create();
        dialog.show();
        return dialog;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值