service provider framework (2)

本文探讨了Java反射机制的使用场景及成本,强调了在可能的情况下优先选择接口而非反射来访问对象的方法。反射虽强大但牺牲了类型安全性、可读性和性能。

Effective JAVA

Item 53 : Prefer interfaces to reflection

The core reflection facility, java.lang.reflect, offers programmatic access to information about loaded classes. Given a Class object, you can obtain Contructor, Method, and Field instances representing the constructors, methods, and fields of the class represented by the Class instance. These objects provide programmatic access to the class's member names, field types, method signatures, and so on.

JAVA反射的核心包java.lang.reflect提供了以编程方式访问Class对象中的构造函数,方法,字段相应的信息,可以查看标注等信息。

Moreover, Constructor, Method, and Field instances let you manipulate their underlying counterparts reflectively: you can construct instances, invoke methods, and access fields of the underlying class by invoking methods on the Constructor, Method, and Field instances. For example, Method.invoke lets you invoke any method on any object of any class (subject to the usual security constraints). Reflection allows one class to use another, even if the latter class did not exist when the former was compiled. This power, however, comes at a price:

最终要的是反射可以使:在没有被调用代码的时候,可以先编写调用代码。这种技术可以良好的支持“声明与实现分离”的代码实现。例如SPF,调用代码会调用service的实现者,但是调用代码的编写者并不知道实现者是否存在,这就所谓的动态可插拔的实现基石。只要声明与实现成功的剥离,那么之后的结合就会有多样性的特征。

  • You lose all the benefits of compile-time type checking, including exception checking. If a program attempts to invoke a nonexistent or inaccessible method reflectively, it will fail at runtime unless you've taken special precautions.
  • The code required to perform reflective access is clumsy and verbose. It is tedious to write and difficult to read.
  • Performance suffers. Relective method invocation is much slower than normal method invocation. Exactly how much slower is hard to say, because there are so many factors at work. On my machine, the speed difference can be as samll as a factor of two or as large as a factor of fifty.
这种技术存在缺点:1. 编译时无法检查,这是“声明与实现分离”的side-effect. 2. 代码量多,这是JAVA的反射的特点。3. 效率,影响因子很多,这个待研究啊...

The core reflection facility was originally designed for component-based application builder tools. Such tools generally load classes on demand and use reflection to find out what methods and constructors they support. The tools let their users interactively construct applications that access these classes, but the generated applications access the classes normally, not reflectively. Reflection is used only at design time. As a result, objects should not be accessed reflectively in normal applications at runtime.
反射技术最初是为了基于组件形式的编译器设计的,需求驱动设计啊。编译器根据命令装载类,然后通过反射查找到所认识的构造函数或者方法。用户可以编写直接访问这些类的应用,当然不是通过反射喽。

There are a few sophisticated applications that require reflection. Examples include class browers, object inspectors, code analysis tools, and interpretive embedded systems. Reflection is also appropriate for use in remote precedure call (RPC) system to eliminate the need for stub compilers. If you have any doubts as to whether your application falls into one of these categories, it probably doesn't .
其它的应用场景包括:类浏览工具,对象审视器,代码分析工具等。反射也可以用在RPC上,减少对 stub compiler的依赖。

You can obtain many of the benefits of reflection while incurring few of its costs by using it only in a very limited form. For many programs that must use a class that is unavailable at compile time, there exists at compile time an appropriate interface or superclass by which to refer to the class. If this is the case, you can create instances reflectively and access them normally via their interface or superclass. If the appropriate constructor has no parameters, then you don't even need to use java.lang.reflect; the Class.newInstance method provides the required functionality.
只要忍受一点痛,你就可得到使用反射的好处了。呵呵,就像医院里医生给小孩打针:"不怕不怕,痛一下,病就好了"。
通过反射实例化,通过接口调用。

翻译了这么多,其实还没说到主题,但是主题很简单啊:
 In fact, the technique is sufficiently powerful to implements a full-blown service provider framework.

标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值