1.Creating and Destroying Objects——Effective Java 2nd Ed学习笔记

本文探讨了静态工厂方法的三大优点,包括清晰命名、性能优化及灵活性,并讨论了其两个主要缺点。此外,还介绍了当面对众多构造参数时采用Builder模式作为解决方案的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1.One advantage of static factory methods is that, unlike constructors, they have names.

 

有时候,一个类有几个不同的构造函数,各个构造函数只能靠参数来区分,编写程序和阅读程序的时候构造子的意义都不明确。而是用静态工厂方法可以明确的知道该构造子的含义,比如BigInteger(int, int, Random),这个构造子返回的可能是一个素数,但是在代码上并没有体现出来,使用BigInteger.probablePrime()静态工厂方法的意义就明确得多了。

 

2.A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked.

 

有时候并不需要每次都创建一个新的对象实例,就可以使用静态工厂方法,避免了创建对象的性能损失。比如Boolean.valueOf(boolean),使用该方法不会重新创建一个Boolean对象,它的代码如下,很明显,这比每次都创建一个新对象在性能上要好得多。This technique is similar to the Flyweight pattern [Gamma95, p. 195].

 

public static Boolean valueOf(boolean b) {

 

return b ? Boolean.TRUE : Boolean.FALSE;

 

}

 

而且有时,静态工厂方法还可以用来保证没有两个相等的对象同时存在,所以在这种情况下可以直接使用a==b判断两个对象是否相等,比使用equals方法效率高。

 

3.A third advantage of static factory methods is that, unlike constructors,they can return an object of any subtype of their return type.

 

a) One application of this flexibility is that an API can return objects without making their classes public.

For example, the Java Collections Framework has thirty-two convenience implementations of its collection interfaces, providing unmodifiable collections,synchronized collections, and the like.Nearly all of these implementations are exported via static factory methods in one noninstantiable class (java.util.Collections).The classes of the returned objects are all nonpublic.这样使用者完全不需要了解那些类就可以使用它们,而且这也使得使用者可以 针对接口编程。

b) Not only can the class of an object returned by a public static factory method be nonpublic, but the class can vary from invocation to invocation depending on the values of the parameters to the static factory.java.util.EnumSet没有公共的构造子,只有静态工厂方法,该方法实际上返回EnumSet的两种不同的实现(这一点使用者完全不需要了解):如果EnumSet大 小小于64,就返回RegularEnumSet实例(当然它继承自EnumSet),这个EnumSet实际上至用了一个long来存储这个EnumSet。如果EnumSet大小大于等于64,则返回JumboEnumSet实例,它使用一个long[]来存储。这样做的好处很明显:大多数情况下返回的RegularEnumSet效率比JumboEnumSet高很多。

 

c) The class of the object returned by a static factory method need not even exist at the time the class containing the method is written.这是service provider frameworks的基础。

 

比如说Java Database Connectivity API 就是一个 service provider frameworks,在编写JDBC的时候,各个厂商所提供的JDBC驱动根本就不存在,但是JDBC API却能返回这些厂商的JDBC对象。service provider frameworks有3个基本的组件:1.服务接口(service interface),服务的提供者必须实现该接口,在JDBC中是Driver接口,2. 服务提供者注册(provider registration)API,JDBC中即DriverManager.registerDriver()3.服务访问(service access)API,JDBC中即DriverManager.getConnection

 

d) A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized typeinstances.(这一点已经没有优势了,因为JDK1.7对此做了简化)

 

Map<String, List<String>> m = new HashMap<String, List<String>>();

 

==> Map<String, List<String>> m = HashMap.newInstance();

 

4.The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.

 

5.A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.而且,在Javadoc中,静态工厂方法不如构造子醒目,对使用者造成一定困难。

 

对于静态工厂方法的区分度不够可以通过命名规范来改进:valueOf, of, getInstance, newInstance, getType, newType

 

Item 2: Consider a builder when faced with many constructorparameters

 

场景:编写一个商品的营养成分类,有超过20种营养元素,而实际中一个商品包含的营养元素也就几种。这样一个类的构造子将会相当大,并且其中无意义的项非常多,每一个参数仅仅靠它的位置来确定它的意义,这样意义不明确,阅读起来不易懂,编写困难,容易出现错误(比如几个参数的位置弄错),这样的错误通常隐晦而难以发现(让程序及早知道错误并提示这个错误是重要的!)。比如:

 

NutritionFacts cocaCola = new NutritionFacts(240, 8, 100, 0, 0, 0, 35, 27, 0, 0, 0);//很难知道哪个数字对应哪个营养

 

一种解决方案是使用JavaBean,如下:NutritionFacts cocaCola = new NutritionFacts();NutritionFacts cocaCola = new NutritionFacts();

cocaCola.setServingSize(240);
cocaCola.setServings(8);
cocaCola.setCalories(100);
cocaCola.setSodium(35);
cocaCola.setCarbohydrate(27);

至少这样做参数的意义明确得多,但是这样做有严重的缺点:a JavaBean may be in an inconsistent state partway through its construction,一个构造过程被分成若干步,如果其中某一步出错,程序将会使用一个不确定的对象,这会导致很多隐晦而难以发觉的错误。还有一个缺点是:the JavaBeans pattern precludes the possibility of making a class immutable。

使用Builder模式的解决方案代码如下: // Builder Pattern

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

 在使用时,这样写:NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();

The Builder pattern simulates named optional parameters as found in Ada and Python.
The Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters

Item 3: Enforce the singleton property with a private constructor or an enum type
单例模式的实现方式:
1.  // Singleton with public final field
public class Elvis
{
	public static final Elvis INSTANCE = new Elvis();
	private Elvis() { ... }
	public void leaveTheBuilding() { ... }
}

这样做可以明确的知道这个对象是单例的,但是Joshua Bloch说:a privileged client can invoke the private constructor reflectively(Item 53) with the aid of the AccessibleObject.setAccessible method. If you need to defend against this attack, modify the constructor to make it throw an exception if it’s asked to create a second instanc  2. 

资源下载链接为: https://pan.quark.cn/s/72147cbc453d 在当今信息化时代,高校社团管理的高效性与便捷性至关重要。基于 Spring Boot 开发的社团管理系统,致力于打造一个功能全面、操作便捷且安全可靠的平台,以满足高校社团的日常运营需求。本文将深入剖析该系统的架构设计、核心功能以及实现原理。 Spring Boot 以其轻量级和快速开发的特性,成为众多企业级应用的首选框架。本社团管理系统采用 Spring Boot 搭建,并遵循 RESTful API 设计原则,构建出一个松耦合、模块化的架构。借助 Spring Boot 的自动配置功能,项目初始化工作得以大幅简化,使开发者能够更加专注于业务逻辑的开发。 权限管理是系统安全的关键环节。本系统引入多级权限控制机制,确保不同角色(如管理员、普通成员等)能够访问其对应的系统功能。通常会借助 Spring Security 或 Apache Shiro 等安全框架,通过角色、权限与资源的映射关系,实现对用户操作的精细化管理。 为了提升用户体验和提高信息传递效率,系统集成了短信接口。在用户注册、密码找回、活动报名等关键操作环节,通过短信验证码进行验证。这需要与第三方短信服务提供商(如阿里云、腾讯云等)进行对接,利用其 SDK 实现短信的发送与接收功能。 会员管理:涵盖会员注册、登录、信息修改及权限分配等功能,方便社团成员进行自我管理。 活动管理:支持活动的创建、审批、报名以及评价等全流程管理,便于社团组织各类活动。 场地管理:实现场地的预定、审批和使用记录管理,确保资源的有效分配。 会议管理:提供会议安排、通知以及签到等功能,提升会议组织效率。 社团管理:包括社团的创建、修改、解散以及社团成员管理等功能。 消息通知:能够实时推送系统消息,保障信息的及时传达。 文件下发:支持文件的上传与下载,方便
资源下载链接为: https://pan.quark.cn/s/79a048d3db20 格陵兰多媒体教学系统V7.0(专业版)-7.0.016是一款专为局域网有线网络环境设计的电子教室机房教学软件,致力于提升教学效率与互动性,助力教师高效管理与掌控课堂。该专业版系统具备丰富功能,满足现代教育需求。 其核心功能之一是广播教学。教师可将自身电脑屏幕内容实时同步至所有学生电脑,全班同学能同步查看相同教学内容,无论是演示课件、播放视频还是操作软件,都能实现统一教学节奏,从而提升教学效率。 个性化小组教学功能则允许教师针对不同学生或小组开展针对性教学。教师可选择部分学生进行单独讲解或组织分组讨论,既能兼顾每个学生的学习进度,又能激发学生间的合作与竞争,增强学习的趣味性和深度。 此外,教学测验功能便于教师进行课堂评估。教师可设计并发布在线测验,实时收集学生答题情况,快速掌握学生对课程内容的理解程度,及时调整教学策略。这种即时反馈机制有助于优化教学过程,保障学生学习效果。 在远程集控管理方面,该系统为教师提供了强大工具。教师可远程操控学生电脑,进行屏幕监控,防止学生课堂分心或进行无关活动,还能统一管理学生电脑设置,如禁用特定程序或网站,维护课堂秩序。 系统中还包含Searcher.exe,这可能是一款搜索工具,方便教师和学生快速查找课堂所需教学资源。而Readme.txt通常记录了软件的安装指南、更新日志或使用注意事项,是初次使用者获取软件信息的重要途径。 格陵兰多媒体教学系统V7.0(专业版)融合了现代信息技术与教育实践,提供了一站式教学解决方案。它使教师能够更灵活、高效地开展教学活动,为学生创造更优质的学习体验。凭借其多元化功能,该系统不仅提高了教学效率,还促进了师生互动交流,契合信息化时代教育需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值