java设计模式--Builder设计模式

本文通过一个具体实例详细介绍了Builder设计模式的实现方式,并对比了它与工厂模式的区别。Builder模式能够有效减少复杂对象内部部件间的耦合度。

在学习Builder设计模式的时候 ,发现 这个Builder 和 工厂设计模式十分相似 ,我甚至说不出来他们之间有着什么样的区别,但可以肯定的是 ,如果我们需要产生一个复杂的对象,这个对象中包含多个其他的对象,以及属性等 。

那么我们应该使用Builder 模式 ,Builder模式减少了 对象部件之间的耦合度 ,所谓的耦合度 也就是对象部件之间的联系 。

其实说的再多也不如自己认真的分析一段代码 并且自己着手实现一个Builder原型 。

下面一段代码实现的功能 是产生一个 MyProduct 对象 这MyProduct对象中包含了 PartA PartB PartC三个对象 ,我们不是像以往的一样,将所有操作在 MyProduct 完成 。

看下面操作吧 可能比我说的更好理解: 下面是结构

class MyProduct //我们的产品类 包含三个部件 。 PartA PartB PartC
{
private PartA a ;
private PartB b ;
private PartC c ;
MyProduct(PartA a,PartB b ,PartC c)
{
this.a=a;
this.b=b;
this.c=c;
}
public void showPart()
{
System.out.println(a.getClass().getName());
System.out.println(b.getClass().getName());
System.out.println(c.getClass().getName());
}
}
class PartA{}
class PartB{}
class PartC{}
interface Builder //Builder接口
{
public void buildPartA() ;
public void buildPartB() ;
public void buildPartC() ;
public MyProduct getResult(); //返回产品
}

class MyBuilderTest implements Builder
{
private PartA a =null;
private PartB b =null;
private PartC c =null;
@Override
public void buildPartA() {
if(a==null)
this.a= new PartA() ;
else
return ;
}
@Override
public void buildPartB() {
if(b==null)
this.b= new PartB() ;
else
return ;
}
@Override
public void buildPartC() {
if(c==null)
this.c= new PartC() ;
else
return ;
}
@Override
public MyProduct getResult() {
return new MyProduct(a,b,c);
}
}
class Director //一个代理
{

private MyBuilderTest t ;
Director(MyBuilderTest t)
{
this.t=t ;
}
public void build()
{
t.buildPartA() ;
t.buildPartB() ;
t.buildPartC() ;
}
}
class MyBuilder
{
public static void main(String []agrs)
{
MyBuilderTest test =new MyBuilderTest(); // 我们的构建器
Director d=new Director(test) ; //Director类
d.build() ; //通过Director构建
MyProduct product=(MyProduct)test.getResult() ; //调用Builer的 getResult返回结果
product.showPart() ; //产品的方法
}
}

<think>我们正在讨论Android开发中的Builder设计模式。根据引用内容,Builder模式主要用于构建复杂对象,通过链式调用设置参数,最后通过build方法创建对象实例。实现细节包括:1.私有化目标类的构造方法,使其只能通过内部Builder类构建。2.静态内部类Builder,它拥有与目标类相同的字段,并提供设置这些字段的方法(每个方法返回Builder自身以实现链式调用)。3.Builder类提供一个build方法,在该方法中调用目标类的私有构造方法(将Builder自身作为参数传入),在目标类的构造方法中完成字段的复制。4.目标类一旦创建,其参数不可更改(即不可变)。在Android中,AlertDialog就是使用Builder模式的经典例子。现在,我们按照用户要求,详细说明Builder模式的使用与实现。</think>### Android Builder设计模式的使用与实现 Builder设计模式是一种创建型模式,用于**分步构建复杂对象**,特别适用于参数众多或配置灵活的场景。在Android开发中,Builder模式被广泛应用于对话框创建、网络请求配置等场景。 #### 一、核心实现原理 1. **私有化构造方法** 目标类的构造方法设为私有,禁止直接实例化。 ```java public class User { private final String name; // final确保不可变 private final int age; private User(Builder builder) { // 私有构造器 this.name = builder.name; this.age = builder.age; } } ``` 2. **静态内部Builder类** 作为构建桥梁,持有与目标类相同的字段: ```java public static class Builder { private String name; private int age; public Builder setName(String name) { this.name = name; return this; // 返回自身实现链式调用 } public Builder setAge(int age) { this.age = age; return this; } public User build() { return new User(this); // 调用私有构造器 } } ``` #### 二、Android经典应用:AlertDialog ```java // 使用示例 AlertDialog dialog = new AlertDialog.Builder(context) .setTitle("警告") .setMessage("确认删除?") .setPositiveButton("确定", (d, which) -> { /* 逻辑 */ }) .setNegativeButton("取消", null) .create(); // 最终构建对象 ``` **实现解析**(参考引用[2][4]): 1. `AlertDialog`构造器私有化,只能通过`Builder`构建 2. `Builder`的每个set方法返回自身,支持链式调用 3. `create()`方法调用`AlertDialog`私有构造器并传入配置参数 4. 最终通过`show()`将Dialog添加到WindowManager[^4] #### 三、自定义Builder模式实现 以构建用户对象为例: ```java public class User { private final String name; private final int age; private final String email; private User(Builder builder) { this.name = builder.name; this.age = builder.age; this.email = builder.email; } public static class Builder { private String name; private int age; private String email = ""; // 默认值 public Builder setName(String name) { this.name = name; return this; } public Builder setAge(int age) { if (age < 0) throw new IllegalArgumentException("年龄无效"); this.age = age; return this; } public Builder setEmail(String email) { this.email = email; return this; } public User build() { if (name == null) throw new IllegalStateException("名称不能为空"); return new User(this); } } } // 使用示例 User user = new User.Builder() .setName("张三") .setAge(30) .setEmail("zhang@example.com") .build(); ``` #### 四、设计优势 1. **参数解耦**:分离对象构建与参数设置(引用[1]) 2. **链式调用**:提升代码可读性(引用[6]) 3. **对象不可变**:build()后字段final确保线程安全 4. **参数校验**:可在build()方法中集中校验 5. **灵活扩展**:支持可选参数和默认值 #### 五、适用场景 1. 需创建含多个可选参数的对象 2. 要求对象创建后不可修改(不可变对象) 3. 需要分步构建复杂配置(如Retrofit.Builder) 4. 替代冗长的多参数构造方法 > **关键点**:Builder模式通过内部类封装构建逻辑,既保证对象创建的灵活性,又维持了目标类的不可变性。在Android中,除了对话框外,Glide图片加载框架、Retrofit网络库等都广泛采用此模式[^2][^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值