- 重叠构造器(telescoping constructor)模式 —— 代码复杂,阅读困难
示例代码:
// Telescoping constructor pattern - does not scale well! public class NutritionFacts { private final int servingSize; // (ml) required private final int servings; // (per container) required private final int calories; // optional private final int fat; // (g) optional private final int sodium; // (mg) optional private final int carbohydrate; // (g) optional public NutritionFacts(int servingSize, int servings) { this(servingSize, servings, 0); } public NutritionFacts(int servingSize, int servings, int calories) { this(servingSize, servings, calories, 0); } public NutritionFacts(int servingSize, int servings, int calories, int fat) { this(servingSize, servings, calories, fat, 0); } public NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium) { this(servingSize, servings, calories, fat, sodium, 0); } public NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) { this.servingSize = servingSize; this.servings = servings; this.calories = calories; this.fat = fat; this.sodium = sodium; this.carbohydrate = carbohydrate; } }
public class Client { public static void main(String[] args) { NutritionFacts cocaCola = new NutritionFacts(240, 8, 100, 0, 35, 27); } }
- JavaBean模式 —— 线程不安全,类可变
示例代码:
// JavaBean Pattern - allows inconsistency, mandates mutability public class NutritionFacts { // Parameters initialized to default values (if any) private int servingSize = -1; // Required; no default value private int servings = -1; private int calories = 0; private int fat = 0; private int sodium = 0; private int carbohydrate = 0; public NutritionFacts() { } // Setters public void setServingSize(int servingSize) { this.servingSize = servingSize; } public void setServings(int servings) { this.servings = servings; } public void setCalories(int calories) { this.calories = calories; } public void setFat(int fat) { this.fat = fat; } public void setSodium(int sodium) { this.sodium = sodium; } public void setCarbohydrate(int carbohydrate) { this.carbohydrate = carbohydrate; } }
public class Client { public static void main(String[] args) { NutritionFacts cocaCola = new NutritionFacts(); cocaCola.setServingSize(240); cocaCola.setServings(8); cocaCola.setCalories(100); cocaCola.setSodium(35); cocaCola.setCarbohydrate(27); } }
- 构建器模式 —— 适应多参数,安全,方便编写,可维护性高,开销大
示例代码:
public class NutritionFacts { private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate; public static class Builder { // Required parameters private final int servingSize; private final int servings; // Optional parameters - initialized to default values private int calories = 0; private int fat = 0; private int carbohydrate = 0; private int sodium = 0; public Builder(int servingSize,int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val) { calories = val; return this; } public Builder fat(int val) { fat = val; return this; } public Builder carbohydrate(int val) { carbohydrate = val; return this; } public Builder sodium(int val) { sodium = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } private NutritionFacts(Builder builder) { servingSize = builder.servingSize; servings = builder.servings; calories = builder.calories; fat = builder.fat; sodium = builder.sodium; carbohydrate = builder.carbohydrate; //对象域抛出异常 if(this.fat>this.sodium){ throw new IllegalArgumentException("The fat must be little than sodium!!"); } } }
public class Client { public static void main(String[] args) { NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8) .calories(100).sodium(35).carbohydrate(27).build(); } }
相关推荐
TheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zipTheThridJE~EJ.zip
Eijiro V86 JE~EJ.zip.001Eijiro V86 JE~EJ.zip.001Eijiro V86 JE~EJ.zip.001Eijiro V86 JE~EJ.zip.001Eijiro V86 JE~EJ.zip.001
Eijiro V86 JE~EJ.zip.006Eijiro V86 JE~EJ.zip.006Eijiro V86 JE~EJ.zip.006
Eijiro V86 JE~EJ.zip.005Eijiro V86 JE~EJ.zip.005Eijiro V86 JE~EJ.zip.005Eijiro V86 JE~EJ.zip.005
Eijiro V86 JE~EJ.zip.002Eijiro V86 JE~EJ.zip.002Eijiro V86 JE~EJ.zip.002
Eijiro V86 JE~EJ.zip.004Eijiro V86 JE~EJ.zip.004
Eijiro V86 JE~EJ.zipEijiro V86 JE~EJ.zip
标题中的"ej.rar_c/C++"表明这是一个与C++编程语言相关的压缩文件,而"ej.c"可能是包含C语言代码的源文件。描述提到的是一个能在DOS环境下操作光驱的C程序,这意味着我们将探讨如何在DOS操作系统下通过C语言控制硬件...
Aspnet-ej2-aspmvc-ej1compatibility.zip,此演示旨在...,asp.net是一个开源的web框架,用于使用.net构建现代web应用和服务。asp.net创建基于html5、css和javascript的网站,这些网站简单、快速,可以扩展到数百万用户。
通过Java开始命令行的方式修改VM参数,用户可使用JProfiler GUI的连接监听Java应用程序。该设置程序不但可以在本地计算机上运行,还可以在网络中作为某个设置程序的附加程序使用。JProfiler的帮助文件,可以帮助...
【ej4B.rar_Time】是一个关于使用时间中断的示波器程序的压缩包,其中包含三个文件:ej4B.c、ej4B.h和ej4B.uir。这个项目主要探讨了在嵌入式系统或微控制器编程中如何利用时间中断来实现示波器功能,这对于电子...
3. **低功耗设计**:为了适应移动设备和便携式应用,EJ511可能采用了低功耗技术,减少运行时的能源消耗。 4. **集成性**:EJ511可能集成了必要的模拟和数字信号处理功能,包括色彩空间转换、图像缩放、去噪等,简化...
ABB的EJ1820系列是DeviceNet远程IO模块,它支持DeviceNet通讯协议,可以完美兼容多个厂商的DeviceNet网络,并能对ABB数字IO信号进行扩展。EJ1820系列远程IO模块支持多种功能,包括数字量输入输出、模拟量输入输出...
标题中的"ej_manager.zip"似乎是一个包含前端项目资源的压缩文件,主要针对一个后台管理系统的空白模板页面。这个模板采用了左右分布的布局设计,其中左侧是功能模块的选择区域,而右侧则动态展示与所选功能相对应的...
EJB(Enterprise JavaBeans)是Java企业级应用开发的核心组件之一,主要用于构建可复用的、分布式的、安全的服务器端应用程序。EJB超时调用是指在EJB方法执行过程中,如果超过了预设的时间限制(即超时时间),系统...
1. 产品信息:标题中提到了EJ101IA-01G_Final_Ver02_***_***.pdf,这表明有一款产品在2014年1月24日至7月19日之间发布了最终版本。由于标题中的日期格式,我们可以推断出产品发布的时间段以及版本号。 2. 屏幕规格...
BP5711EJ是一款专为市电输入的调光光源和灯具设计的驱动芯片,它采用了线性恒流技术,无需磁性元件,有助于减小驱动器体积,降低成本,同时满足电磁兼容(EMI)标准。 该芯片的主要特点包括: 1. 外围电路简单,...