回头看自己写的东西,大概Android当自己控制的定义,编写代码适用性比较高。但是,看看没有什么技术含量,因此,当在学习设计模式,想想有些东西是否可以改善,例如:

自己定义Dialog是Android应用必须的,系统的控件实在是太难看了。

在构建中,全然是,new完对象之后,须要什么构建什么。这样写没有问题。可读性也还行,就是看上去不咋的。

下面是小部分代码片段:

package com.example.demo.Builder;

/**
*
* @author qubian
* @data 2015年6月10日
* @email naibbian@163.com
*
*/
public class BuilderDialog extends Dialog implements View.OnClickListener {
private Context mContext;
private View view;
private View lineView;
private TextView titleTv;
private TextView contentTv;
private Button sureBtn;
private Button cancelBtn;
private String sureTitle;
private String cancelTitle;
private String title;
private String content;
private boolean sureVisible = true;
private boolean cancelVisible = true;
private View.OnClickListener sureListener;
private View.OnClickListener cancelListener; public BuilderDialog(Context context, String title, String content) {
super(context, R.style.base_dialog_style);
this.mContext = context;
this.title = title;
this.content = content;
view = LayoutInflater.from(mContext).inflate(R.layout.dialog_normal,
null);
addContentView(view, Utils.getDialogLayoutParams(mContext));
} public void setSureTitle(String title) {
sureTitle = title;
} public void setCancelTitle(String title) {
cancelTitle = title;
} public void setCancelVisible(boolean visible) {
cancelVisible = visible;
} public void setSureListener(View.OnClickListener listener) {
if (listener != null) {
sureListener = listener;
}
} public void setCancelListener(View.OnClickListener listener) { } /**
* 能否够返回
*
* @param canBack
*/
public void setCanBack(boolean canBack) { }
//初始化
private void initView() {
lineView = view.findViewById(R.id.line_view);
titleTv = (TextView) view.findViewById(R.id.title_tv);
contentTv = (TextView) view.findViewById(R.id.content_tv);
contentTv.setText((content.replace("\\r","\r").replace("\\n", "\n")));
sureBtn = (Button) view.findViewById(R.id.sure_btn);
cancelBtn = (Button) view.findViewById(R.id.cancel_btn);
} @Override
public void show() {
initView();
titleTv.setText(title);
if (sureVisible) {
if (sureListener == null) {
sureBtn.setOnClickListener(this);
} else {
sureBtn.setOnClickListener(sureListener);
}
if (sureTitle != null) {
sureBtn.setText(sureTitle);
}
} else {
sureBtn.setVisibility(View.GONE);
}
if (cancelVisible) { } else { }
super.show();
} @Override
public void onClick(View v) {
if (v.getId() == sureBtn.getId()) {
this.cancel();
} else if (v.getId() == cancelBtn.getId()) {
this.cancel();
}
} }

使用。和适用都没问题,而且逻辑也比較简单,那么怎样优化呢?

言归正传:

建造者模式

1、定义:

将一个复杂的构建与其表示分离,使得同样的构建有了不同的表示。

2、目的:

建造者模式是讲复杂的内部构建封装在内部,对于其它外部成员来说,仅仅须要传递构建者和构建工具。便能够得到所需。不须要关心怎样构建,以及内部构建过程。

3、使用:

3.1、在构建的过程中,同意不同的构建过程。产生不同表示的构建对象;

3.2、在复杂的对象时,其复杂的构建算法应当独立于对象的组成部分,或者是独立于装配方式时;

4、一个简单的demo:

核心:抽象建造者,详细建造者,实体类

package com.example.demo.Builder;

import android.util.Log;
/**
* 建造者模式
* @author qubian
* @data 2015年6月10日
* @email naibbian@163.com
*
*/
public class Product { Builder mBuilder ;
public Builder getBuilder()
{
if (mBuilder==null) {
mBuilder = new ProductBuilder();
}
return mBuilder;
} /**
* 抽象建造者
* @author qubian
* @data 2015年6月10日
* @email naibbian@163.com
*
*/
public interface Builder
{
public Builder buildPart1(); public Builder buildPart2(); public Product getProduct();
}
/**
* 详细的建造者
* @author qubian
* @data 2015年6月10日
* @email naibbian@163.com
*
*/
public class ProductBuilder implements Builder
{
private static final String TAG= "ProductBuilder";
Product mProduct = new Product();
@Override
public Builder buildPart1() {
Log.i(TAG, "buildPart1");
return this; } @Override
public Builder buildPart2() {
Log.i(TAG, "buildPart2");
return this; } @Override
public Product getProduct() { return mProduct;
} } }

使用:

package com.example.demo.Builder;

/**
* 使用
* @author qubian
* @data 2015年6月10日
* @email naibbian@163.com
*
*/
public class UseProduct { public void use()
{
Product p = new Product().getBuilder().buildPart1().buildPart2().getProduct();
} }

5、在Android的源代码中。建造者模式。肯定是不可缺少的;

当中最为代表的就是AlertDialog,在其构建过程中,便是构建与表示分离。

其内部的Builder便是他的构建者。

public class AlertDialog extends Dialog implements DialogInterface {
private AlertController mAlert; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAlert.installContent();
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mAlert.onKeyDown(keyCode, event)) return true;
return super.onKeyDown(keyCode, event);
} @Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (mAlert.onKeyUp(keyCode, event)) return true;
return super.onKeyUp(keyCode, event);
} public static class Builder {
private final AlertController.AlertParams P;
private int mTheme; /**
* Constructor using a context for this builder and the {@link AlertDialog} it creates.
*/
public Builder(Context context) {
this(context, resolveDialogTheme(context, 0));
} /**
* Set the title displayed in the {@link Dialog}.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setTitle(CharSequence title) {
P.mTitle = title;
return this;
} /**
* Set the message to display using the given resource id.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setMessage(int messageId) {
P.mMessage = P.mContext.getText(messageId);
return this;
} /**
* Set the message to display.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setMessage(CharSequence message) {
P.mMessage = message;
return this;
} /**
* Set the resource id of the {@link Drawable} to be used in the title.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setIcon(int iconId) {
P.mIconId = iconId;
return this;
} /**
* Set a listener to be invoked when the positive button of the dialog is pressed.
* @param textId The resource id of the text to display in the positive button
* @param listener The {@link DialogInterface.OnClickListener} to use.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setPositiveButton(int textId, final OnClickListener listener) {
P.mPositiveButtonText = P.mContext.getText(textId);
P.mPositiveButtonListener = listener;
return this;
} /**
* Set a listener to be invoked when the positive button of the dialog is pressed.
* @param text The text to display in the positive button
* @param listener The {@link DialogInterface.OnClickListener} to use.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
P.mPositiveButtonText = text;
P.mPositiveButtonListener = listener;
return this;
} /**
* Set a listener to be invoked when the negative button of the dialog is pressed.
* @param textId The resource id of the text to display in the negative button
* @param listener The {@link DialogInterface.OnClickListener} to use.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setNegativeButton(int textId, final OnClickListener listener) {
P.mNegativeButtonText = P.mContext.getText(textId);
P.mNegativeButtonListener = listener;
return this;
} /**
* Set a listener to be invoked when the negative button of the dialog is pressed.
* @param text The text to display in the negative button
* @param listener The {@link DialogInterface.OnClickListener} to use.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
P.mNegativeButtonText = text;
P.mNegativeButtonListener = listener;
return this;
} /**
* Set a listener to be invoked when the neutral button of the dialog is pressed.
* @param textId The resource id of the text to display in the neutral button
* @param listener The {@link DialogInterface.OnClickListener} to use.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setNeutralButton(int textId, final OnClickListener listener) {
P.mNeutralButtonText = P.mContext.getText(textId);
P.mNeutralButtonListener = listener;
return this;
}
/**
* Set a custom view to be the contents of the Dialog. If the supplied view is an instance
* of a {@link ListView} the light background will be used.
*
* @param view The view to use as the contents of the Dialog.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setView(View view) {
P.mView = view;
P.mViewSpacingSpecified = false;
return this;
} /**
* Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not
* {@link Dialog#show()} the dialog. This allows the user to do any extra processing
* before displaying the dialog. Use {@link #show()} if you don't have any other processing
* to do and want this to be created and displayed.
*/
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;
} /**
* Creates a {@link AlertDialog} with the arguments supplied to this builder and
* {@link Dialog#show()}'s the dialog.
*/
public AlertDialog show() {
AlertDialog dialog = create();
dialog.show();
return dialog;
}
} }

也许它的开放性,AlterView也有自己的构建过程,这种使用AlterView建设者Builder要构建视图,他对象可以自行操作。

Android设计模式(十)--生成器模式的更多相关文章

  1. 设计模式十: 生成器模式(Builder Pattern)

    简介 生成器模式属于创建型模式的一种, 又叫建造者模式. 生成器模式涉及4个关键角色:产品(Product),抽象生成器(builder),具体生成器(ConcreteBuilder),指挥者(Dir ...

  2. 每天一个设计模式-7 生成器模式(Builder)

    每天一个设计模式-7 生成器模式(Builder) 一.实际问题 在讨论工厂方法模式的时候,提到了一个导出数据的应用框架,但是并没有涉及到导出数据的具体实现,这次通过生成器模式来简单实现导出成文本,X ...

  3. Android 设计模式之MVC模式

    说到Android设计模式的MVC模式,估计很多人都是比较熟悉了,这里深入了解一下MVC到底是怎么回事,以ListView为例子讲解. 一.深入理解MVC概念 MVC即Model-View-Contr ...

  4. Java设计模式:生成器模式

    问题的提出: 有些类很容易创建对象,直接调用其构造方法,例如Student student = new Student("1001","zhang",21); ...

  5. 【设计模式】- 生成器模式(Builder)

    生成器模式 建造者模式.Builder 生成器模式 也叫建造者模式,可以理解成可以分步骤创建一个复杂的对象.在该模式中允许你使用相同的创建代码生成不同类型和形式的对象. 生成器的结构模式 生成器(Bu ...

  6. Java设计模式-Builder生成器模式

    概念: 生成器模式也称之为建造者模式.生成器模式的意图在于将一个复杂的构建与其表示相分离,构建与产品分离. UML: Ibuild接口清晰地反映了创建产品Product的流程. 生成器模式涉及4个关键 ...

  7. Android设计模式系列-组合模式

    Android中对组合模式的应用,可谓是泛滥成粥,随处可见,那就是View和ViewGroup类的使用.在android UI设计,几乎所有的widget和布局类都依靠这两个类.组合模式,Compos ...

  8. 面向对象设计模式_生成器模式详解(Builder Pattern)

    首先提出一个很容易想到应用场景: 手机的生产过程:手机有非常多的子件(部件),成千上万,不同品牌的手机的生产过程都是复杂而有所区别的,相同品牌的手机在设计上也因客户需求多样化,大到型号,小到颜色,是否 ...

  9. 面向对象设计模式_生成器模式解读(Builder Pattern)

    首先提出一个很容易想到应用场景: 手机的生产过程:手机有非常多的子件(部件),成千上万,不同品牌的手机的生产过程都是复杂而有所区别的,相同品牌的手机在设计上也因客户需求多样化,大到型号,小到颜色,是否 ...

  10. 设计模式--Builder生成器模式

    如果文章中哪里有问题,希望各位大哥大姐指出,小弟十分感激. 正文 什么是生成器模式? 生成器模式就是把生产对象的过程进一步抽取.细化.独立.以往我们生产对象,可能就是在一个小作坊里面从头做到尾.现在用 ...

随机推荐

  1. poj3984(经典dfs)

    题目链接:http://poj.org/problem?id=3984 分析:直接深搜从起点到终点,如何取最短路线,其实只要优先向下或向右走即可. #include <cstdio> #i ...

  2. _00021 尼娜抹微笑伊拉克_谁的的最离奇的异常第二阶段 Jedis pool.returnResource(jedis)

    笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 博文标题:_00021 妳那伊抹微笑_谁的异常最诡异第二期之 Jedis pool.returnRes ...

  3. Python-方法重载的问题

    定义一个父类,在写一个子类继承他,重载他的foo方法: class Father: def foo(self): print"I am father" class Son(Fath ...

  4. GString及IntelliJIdea中调试Groovy的操作步骤

    今天是学习Groovy的第一天,首先我觉得学习任何一种语言都要先弄清楚这种语言的特性,因为只有了解了特性之后学习才能达到好的效果,那么groovy的特点是什么的.我觉得groovy是一种动态语言,动态 ...

  5. 程序猿进化 - 在拉钩子1024对APE节讲座计划

    注意:下面这篇文章来自于我在网上拉勾1024对APE节现场演示程序. 我是蒋宇捷,信天创投的合伙人.之前是百度魔图的联合创始人. 我先做个自我介绍,事实上每次介绍自己事实上是非常痛苦的事情,由于我前不 ...

  6. 设计模式六大原则(4):接口隔离原则(Interface Segregation Principle)

    接口隔离原则: 使用多个专门的接口比使用单一的总接口要好. 一个类对另外一个类的依赖性应当是建立在最小的接口上的. 一个接口代表一个角色,不应当将不同的角色都交给一个接口.没有关系的接口合并在一起,形 ...

  7. 【LeetCode with Python】 Rotate Image

    博客域名:http://www.xnerv.wang 原标题页:https://oj.leetcode.com/problems/rotate-image/ 题目类型:下标计算 难度评价:★★★ 本文 ...

  8. ASP.NET Core环境并运行 继续跨平台

    ASP.NET Core环境并运行 继续跨平台 无需安装mono,在Linux(Ubuntu)下搭建ASP.NET Core环境 继续.NET跨平台 上一篇:使用VS Code开发ASP.NET Co ...

  9. 原创教程“ActionScript3.0游戏中的图像编程”開始连载啦!

            经过近两年的不懈努力,笔者的原创教程"ActionScript3游戏中的图像编程"最终在今日划上了完美的句号!这其中记录着笔者多年来在游戏制作,尤其是其中图像处理方 ...

  10. ListView IllegalStateException

    贴出源代码: android.widget.ListView ... if(mItemCount == 0){ resetList(); invokeOnItemScrollListener(); r ...