Effective Java notes-Creating and Destroying Objects(1)

本文探讨了静态工厂方法相较于构造函数的优势与劣势,并强调了在某些情况下使用静态工厂方法的重要性,特别是在避免直接创建对象时。

Item 1 Consider static factory method instead of constructors

 

Advantages:

  1. One advantage of static factory methods is that, unlike constructors, they have names. (Mostly used in Java.utils.Collections)
  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.
  3. A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type. (See service provider framework , eg : The JDBC Driver Manager)
  4. A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances.(可以通过为具有泛型参数的类提供static factory method来消除实例化该类时的泛型参数)

 

Disadvantages:

  1. The main disadvantage of providing only static factory method is that classes without public or protected constructors cannot be subcalssed. (See Collections Framework, encourages progammers to use composition instead of inheritance)
  2. A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods. (In my point, this issue could be ignored, just seeking for an appropriate name for the method is ok)

 Key points of item1:

  • The static factory methods descriped in item1 has no direct equivalent in Design Patterns.
  • Avoid the reflex to provide public constructors without first considering static factories.
下载方式:https://pan.quark.cn/s/c9b9b647468b ### 初级JSP程序设计教程核心内容解析#### 一、JSP基础概述JSP(JavaServer Pages)是由Sun Microsystems公司创建的一种动态网页技术规范,主要应用于构建动态网站及Web应用。JSP技术使得开发者能够将动态数据与静态HTML文档整合,从而实现网页内容的灵活性和可变性。##### JSP的显著特性:1. **动态与静态内容的分离**:JSP技术支持将动态数据(例如数据库查询结果、实时时间等)嵌入到静态HTML文档中。这种设计方法增强了网页的适应性和可维护性。2. **易用性**:开发者可以利用常规的HTML编辑工具来编写静态部分,并通过简化的标签技术将动态内容集成到页面中。3. **跨平台兼容性**:基于Java平台的JSP具有优良的跨操作系统运行能力,能够在多种不同的系统环境中稳定工作。4. **强大的后台支持**:JSP能够通过JavaBean组件访问后端数据库及其他资源,以实现复杂的数据处理逻辑。5. **执行效率高**:JSP页面在初次被请求时会被转换为Servlet,随后的请求可以直接执行编译后的Servlet代码,从而提升了服务响应的效率。#### 二、JSP指令的运用JSP指令用于设定整个JSP页面的行为规范。这些指令通常放置在页面的顶部,向JSP容器提供处理页面的相关指导信息。##### 主要的指令类型:1. **Page指令**: - **语法结构**:`<%@ page attribute="value" %>` - **功能**:定义整个JSP页面的运行特性,如设定页面编码格式、错误处理机制等。 - **实例**: ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值