java 构建者模式

In my last post, I explained about Builder Pattern in Java. Sometime back I wrote an article about how to create immutable class in java. In this post, we will learn how easy it’s to create java immutable class using Builder pattern. Using builder pattern to create immutable class is a good approach when the number of arguments in the Constructor is more that can cause confusion in their ordering.

在上一篇文章中,我解释了Java中的Builder模式 。 有时我写了一篇关于如何在java中创建不可变类的文章。 在这篇文章中,我们将学习使用Builder模式创建Java不可变类是多么容易。 当构造函数中的参数数量更多而可能导致其顺序混乱时,使用构造器模式创建不可变类是一种好方法。

Java不可变类要点 (Java Immutable class important points)

  • The immutable class should have only getter methods.不可变类应仅具有getter方法。
  • The immutable class will have a private constructor with Builder object as parameter that will be used to create the immutable class.不可变类将具有一个私有构造函数,其中带有Builder对象作为参数,将用于创建不可变类。
  • If the immutable class attributes are not immutable, for example HashMap, we should perform deep copy or cloning to avoid modification of its attributes.如果不可更改的类属性不是不可更改的,例如HashMap,则应执行深度复制或克隆操作,以避免对其属性进行修改。
  • Using Builder pattern is easy when number of optional attributes are more in the immutable class.当不可变类中的可选属性数量更多时,使用Builder模式很容易。

Java不可变类–构建器模式 (Java Immutable Class – Builder Pattern)

Here is the implementation of an immutable class in java using Builder pattern.

这是使用Builder模式在Java中实现不可变类的实现。

package com.journaldev.design.builder;import java.util.HashMap;public class ImmutableClass {//required fieldsprivate int id;private String name;//optional fieldsprivate HashMap<String, String> properties;private String company;public int getId() {return id;}public String getName() {return name;}public HashMap<String, String> getProperties() {//return cloned object to avoid changing it by the client applicationreturn (HashMap<String, String>) properties.clone();}public String getCompany() {return company;}private ImmutableClass(ImmutableClassBuilder builder) {this.id = builder.id;this.name = builder.name;this.properties = builder.properties;this.company = builder.company;}//Builder classpublic static class ImmutableClassBuilder{//required fieldsprivate int id;private String name;//optional fieldsprivate HashMap<String, String> properties;private String company;public ImmutableClassBuilder(int i, String nm){this.id=i;this.name=nm;}public ImmutableClassBuilder setProperties(HashMap<String,String> hm){this.properties = (HashMap<String, String>) hm.clone();return this;}public ImmutableClassBuilder setCompany(String comp){this.company = comp;return this;}public ImmutableClass build(){return new ImmutableClass(this);}}
}

Here is the test program to check if the object we create is immutable or not.

这是测试程序,用于检查我们创建的对象是否不可变。

package com.journaldev.design.test;import java.util.HashMap;import com.journaldev.design.builder.ImmutableClass;public class ImmutableBuilderTest {public static void main(String[] args) {// Getting immutable class only with required parametersImmutableClass immutableClass = new ImmutableClass.ImmutableClassBuilder(1, "Pankaj").build();HashMap<String,String> hm = new HashMap<String, String>();hm.put("Name", "Pankaj");hm.put("City", "San Jose");// Getting immutable class with optional parametersImmutableClass immutableClass1 = new ImmutableClass.ImmutableClassBuilder(1, "Pankaj").setCompany("Apple").setProperties(hm).build();//Testing immutabilityHashMap<String,String> hm1 = immutableClass1.getProperties();//lets modify the Object passed as argument or get from the Objecthm1.put("test", "test");hm.put("test", "test");//check that immutable class properties are not changedSystem.out.println(immutableClass1.getProperties());}}

That’s all for creating immutable class in java using builder pattern.

这就是使用生成器模式在Java中创建不可变类的全部内容。

Reference: Oracle Documentation

参考: Oracle文档

翻译自: https://www.journaldev.com/1432/java-immutable-class-builder

java 构建者模式

java 构建者模式_Java不可变类–构建器模式相关推荐

  1. java中装饰器_Java设计模式12:装饰器模式

    装饰器模式 装饰器模式又称为包装(Wrapper)模式.装饰器模式以多客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式的结构 通常给对象添加功能,要么直接修改对象添加相应的功能, ...

  2. 仿京东APP分类页面(mvp模式+OkHttp封装工具类+拦截器+弱引用回收)

    仿京东APP分类页面: 添加依赖 compile 'com.android.support:recyclerview-v7:24.0.0' compile 'com.google.code.gson: ...

  3. java 不可变性_JAVA不可变类(immutable)机制与String的不可变性(推荐)

    一.不可变类简介 不可变类:所谓的不可变类是指这个类的实例一旦创建完成后,就不能改变其成员变量值.如JDK内部自带的很多不可变类:Interger.Long和String等. 可变类:相对于不可变类, ...

  4. java.net.url 兼容_java.net.Url类的应用(网络编程)

    一.认识URL 类 URL 代表一个统一资源定位符,它是指向互联网"资源"的指针.资源可以是简单的文件或目录,也可以是更为复杂的对象的引用,例如对数据库或搜索引擎的查询. 简单的可 ...

  5. java 恶汉和懒汉_java里的 懒汉和恶汉模式-----讲解

    ------------java中的恶汉模式 public void Test{ private static Test inte = new Test(); // 内部自己创建好实例,私有属性(不建 ...

  6. java载入器材_JAVA之了解类载入器Classloader

    1.类的载入.连接和初始化 类初始化通常包含载入.连接.初始化三个步骤. (1)进程的结束 每当执行一个java程序时,将会启动一个java虚拟机进程,无论程序多么复杂.有多少线程.都在这个java虚 ...

  7. java中的事件派发机制_事件派发器模式

    在项目开发中,会遇到如下情形:我们自己的服务订阅.接收来自消息队列或者客户端的事件和请求,基于不同的事件采取对应的行动,这种情况下适合应用派发器模式. 主要模块 XXXEventDispatcher类 ...

  8. 装饰者模式 php,PHP设计模式之装饰器模式

    装饰器设计模式 什么是装饰器模式 装饰器模式就是对一个已有的结构增加装饰.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象 ...

  9. java 反编译 类名_java javassist创建类和反编译类

    public class Byte { public static void main(String[] args) throws Exception { //获得类池 ClassPool pool= ...

最新文章

  1. Java基础篇:面向对象
  2. jQuery学习总结之基础知识----持续更新中
  3. vue插槽样式_Vue为什么要有插槽
  4. iReport使用方法
  5. arm linux中添加开机启动
  6. 这次不忽悠:3个成功案例告诉你,开一家AI公司其实不难
  7. 【Flutter】Flutter 拍照示例 ( 浮动按钮及点击事件 | 底部显示按钮组件 | 手势检测器组件 | 拍照并获取当前拍摄照片 | 从相册中选择图片 )
  8. IBM为世博会服务支持建立快速反应通道
  9. python xml解析dom_如何解析python中表示xml.dom.minidom节点的字符串?
  10. Windows服务安装、卸载方法,卸载后在服务列表中仍显示问题,指定的服务已经标记为删除
  11. 二维数组求子数组中最大的和
  12. anaconda的执行路径
  13. js获取当前时区GMT
  14. 5001 boost之bind库函数
  15. 没有测量就没有管理,怀念DNW和复习盖洛普Q12
  16. pip 使用豆瓣镜像
  17. Cesium:3dtile制作工具
  18. 阿里云配置安全组规则完整教程汇总
  19. caffe调用之前的权重和接着断点继续训练
  20. 蛋白质组学研究的经典方法:双向电泳技术解读

热门文章

  1. PHP魔术方法小结.md
  2. SQL Server 2000企业管理器中MMC无法创建管理单元的解决方法
  3. 2005这一年不堪回首的日子
  4. [转载] Python使用QRCode生成二维码
  5. [转载] Java中的strictfp关键字
  6. 别人的Linux私房菜(19)认识与分析日志文件
  7. Kali下的内网劫持(一)
  8. 冷知识 —— 文学(名与字)
  9. 20169212《Linux内核原理及分析》第十二周作业
  10. linux grep (转)