- 浏览: 925479 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (322)
- Hibernate研究&源码 (27)
- Server (10)
- Coder碎语 (64)
- EnglishMulling (11)
- About XML (1)
- persistence (12)
- Core Java & OO (23)
- Java EE (6)
- JavaScript/JSON/Ajax/ext... (22)
- 我的读书笔记 (16)
- Source Codes Study (29)
- workFlow/jBPM (22)
- OFBiz: Open For Business (1)
- 项目积累 (21)
- srcStudy_acegi (1)
- Cache/Ehcache... (9)
- Java Test/JUnit.. (7)
- maven/ant (2)
- 设计模式 (1)
- SOA/cxf/ws-security (2)
- Android (4)
- 云计算/Hadoop (2)
- 加密/签名 (1)
- 正则表达式 (1)
- htmlparser (1)
- 操作系统 (5)
- DB (1)
最新评论
-
天使建站:
这里这篇文章更详细 还有完整的实例演示:js跳出循环 ...
jQuery中each的break和continue -
heshifk:
刚刚我也遇到同样的问题,然后就在纠结为什么不能直接使用brea ...
jQuery中each的break和continue -
masuweng:
不错写的.
集万千宠爱于一身的SessionImpl:get研究(四): Hibernate源码研究碎得(8) -
muzi131313:
这个老是忘,做一下笔记还是挺好的
jQuery中each的break和continue -
lg068:
data = data.replace("\n&qu ...
项目小经验: eval与回车符
Q 11: What is design by contract? Explain the assertion construct? DC(基于什么考虑,Java 1.4中引入Assert???)
A 11: Design by contract specifies the obligations of a calling-method and called-method to each other. Design by
contract is a valuable technique, which should be used to build well-defined interfaces. The strength of this
programming methodology is that it gets the programmer to think clearly about what a function does, what pre
and post conditions it must adhere to and also it provides documentation for the caller. Java uses the assert
statement to implement pre- and post-conditions. Java’s exceptions handling also support design by contract
especially checked exceptions (Refer Q39 in Java section for checked exceptions). In design by contract in
addition to specifying programming code to carrying out intended operations of a method the programmer also
specifies:
1. Preconditions – This is the part of the contract the calling-method must agree to. Preconditions specify the
conditions that must be true before a called method can execute. Preconditions involve the system state and the
arguments passed into the method at the time of its invocation. If a precondition fails then there is a bug in the
calling-method or calling software component.
On public methods:
Preconditions on public methods are enforced by explicit checks that throw particular, specified exceptions. You should not use assertion to check the parameters of the public methods but can use for the non-public methods. Assert is inappropriate because the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled. Further, assert construct does not throw an exception of a specified type. It can throw only an AssertionError.
public void setRate(int rate) {
if(rate <= 0 || rate > MAX_RATE){
throw new IllegalArgumentException(“Invalid rate --> ” + rate);
}
setCalculatedRate(rate);
}
On non-public methods
You can use assertion to check the parameters of the non-public methods.
private void setCalculatedRate(int rate) {
assert (rate > 0 && rate < MAX_RATE) : rate;
//calculate the rate and set it.
}
Assertions can be disabled, so programs must not assume that assert construct will be always executed:
//Wrong:
//if assertion is disabled, “pilotJob” never gets removed
assert jobsAd.remove(pilotJob);
//Correct:
boolean pilotJobRemoved = jobsAd.remove(pilotJob);
assert pilotJobRemoved;
2. Postconditions – This is the part of the contract the called-method agrees to. What must be true after a
method completes successfully. Postconditions can be used with assertions in both public and non-public
methods. The postconditions involve the old system state, the new system state, the method arguments and the
method’s return value. If a postcondition fails then there is a bug in the called-method or called software
component.
public double calcRate(int rate) {
if(rate <= 0 || rate > MAX_RATE){
throw new IllegalArgumentException(“Invalid rate !!! ”);
}
//logic to calculate the rate and set it goes here
assert this.evaluate(result) < 0 : this; //message sent to AssertionError on failure
return result;
}
3. Class invariants - what must be true about each instance of a class? A class invariant as an internal invariant
that can specify the relationships among multiple attributes, and should be true before and after any method
completes. If an invariant fails then there could be a bug in either calling-method or called-method. There is
no particular mechanism for checking invariants but it is convenient to combine all the expressions required for
checking invariants into a single internal method that can be called by assertions. For example if you have a class,
which deals with negative integers then you define the isNegative() convenient internal method:
class NegativeInteger {
Integer value = new Integer (-1); //invariant
//constructor
public NegativeInteger(Integer int) {
//constructor logic goes here
assert isNegative();
}
// rest of the public and non-public methods goes here. public methods should call
// assert isNegative(); prior to its return
// convenient internal method for checking invariants.
// Returns true if the integer value is negative
private boolean isNegative(){
return value.intValue() < 0 ;
}
}
The isNegative() method should be true before and after any method completes, each public method and
constructor should contain the following assert statement immediately prior to its return.
assert isNegative();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Explain the assertion construct? The assertion statements have two forms as shown below:
assert Expression1;
assert Expression1 : Expression2;
Where:
. Expression1 --> is a boolean expression. If the Expression1 evaluates to false, it throws an AssertionError without any
detailed message.
. Expression2 --> if the Expression1 evaluates to false throws an AssertionError with using the value of the Expression2 as
the error’s detailed message.
Note: If you are using assertions (available from JDK1.4 onwards), you should supply the JVM argument to
enable it by package name or class name.
java -ea[:packagename...|:classname] or java -enableassertions[:packagename...|:classname]
java –ea:Account
A 11: Design by contract specifies the obligations of a calling-method and called-method to each other. Design by
contract is a valuable technique, which should be used to build well-defined interfaces. The strength of this
programming methodology is that it gets the programmer to think clearly about what a function does, what pre
and post conditions it must adhere to and also it provides documentation for the caller. Java uses the assert
statement to implement pre- and post-conditions. Java’s exceptions handling also support design by contract
especially checked exceptions (Refer Q39 in Java section for checked exceptions). In design by contract in
addition to specifying programming code to carrying out intended operations of a method the programmer also
specifies:
1. Preconditions – This is the part of the contract the calling-method must agree to. Preconditions specify the
conditions that must be true before a called method can execute. Preconditions involve the system state and the
arguments passed into the method at the time of its invocation. If a precondition fails then there is a bug in the
calling-method or calling software component.
On public methods:
Preconditions on public methods are enforced by explicit checks that throw particular, specified exceptions. You should not use assertion to check the parameters of the public methods but can use for the non-public methods. Assert is inappropriate because the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled. Further, assert construct does not throw an exception of a specified type. It can throw only an AssertionError.
public void setRate(int rate) {
if(rate <= 0 || rate > MAX_RATE){
throw new IllegalArgumentException(“Invalid rate --> ” + rate);
}
setCalculatedRate(rate);
}
On non-public methods
You can use assertion to check the parameters of the non-public methods.
private void setCalculatedRate(int rate) {
assert (rate > 0 && rate < MAX_RATE) : rate;
//calculate the rate and set it.
}
Assertions can be disabled, so programs must not assume that assert construct will be always executed:
//Wrong:
//if assertion is disabled, “pilotJob” never gets removed
assert jobsAd.remove(pilotJob);
//Correct:
boolean pilotJobRemoved = jobsAd.remove(pilotJob);
assert pilotJobRemoved;
2. Postconditions – This is the part of the contract the called-method agrees to. What must be true after a
method completes successfully. Postconditions can be used with assertions in both public and non-public
methods. The postconditions involve the old system state, the new system state, the method arguments and the
method’s return value. If a postcondition fails then there is a bug in the called-method or called software
component.
public double calcRate(int rate) {
if(rate <= 0 || rate > MAX_RATE){
throw new IllegalArgumentException(“Invalid rate !!! ”);
}
//logic to calculate the rate and set it goes here
assert this.evaluate(result) < 0 : this; //message sent to AssertionError on failure
return result;
}
3. Class invariants - what must be true about each instance of a class? A class invariant as an internal invariant
that can specify the relationships among multiple attributes, and should be true before and after any method
completes. If an invariant fails then there could be a bug in either calling-method or called-method. There is
no particular mechanism for checking invariants but it is convenient to combine all the expressions required for
checking invariants into a single internal method that can be called by assertions. For example if you have a class,
which deals with negative integers then you define the isNegative() convenient internal method:
class NegativeInteger {
Integer value = new Integer (-1); //invariant
//constructor
public NegativeInteger(Integer int) {
//constructor logic goes here
assert isNegative();
}
// rest of the public and non-public methods goes here. public methods should call
// assert isNegative(); prior to its return
// convenient internal method for checking invariants.
// Returns true if the integer value is negative
private boolean isNegative(){
return value.intValue() < 0 ;
}
}
The isNegative() method should be true before and after any method completes, each public method and
constructor should contain the following assert statement immediately prior to its return.
assert isNegative();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Explain the assertion construct? The assertion statements have two forms as shown below:
assert Expression1;
assert Expression1 : Expression2;
Where:
. Expression1 --> is a boolean expression. If the Expression1 evaluates to false, it throws an AssertionError without any
detailed message.
. Expression2 --> if the Expression1 evaluates to false throws an AssertionError with using the value of the Expression2 as
the error’s detailed message.
Note: If you are using assertions (available from JDK1.4 onwards), you should supply the JVM argument to
enable it by package name or class name.
java -ea[:packagename...|:classname] or java -enableassertions[:packagename...|:classname]
java –ea:Account
发表评论
-
JavaPersistenceWithHibernate读书笔记(6)--持久层与另外可用替代方案
2008-04-06 17:21 16711.3 Persistence layers and alte ... -
<Java.JavaEE面试整理>(12) 对Collections FrameWork的理解(二)
2008-04-06 15:27 2873<Java.JavaEE面试整理>(12) 对Co ... -
<Java.JavaEE面试整理>(11) 对Collections FrameWork的理解(一)
2008-04-06 13:55 2415Q 16:谈谈你对Java Collectio ... -
写博一月后的收获与反思(1)
2008-04-05 17:26 1658写博一月后的收获与反思开始安下心来写博到现在,细想一下也有一个 ... -
<Java.JavaEE面试整理>(9)--抽象类与接口有什么区别?以及如何选择?
2008-04-03 15:17 1911"abstract class" or & ... -
JavaPersistenceWithHibernate读书笔记(4)
2008-04-03 10:41 21391.2.4 Problems relating to asso ... -
JavaPersistenceWithHibernate读书笔记(3)
2008-04-02 17:25 17571.2.2 The problem of subtypes ... -
<Java.JavaEE面试整理>(7) --Polymorphism之深入理解(二)
2008-04-02 14:31 1505Q. Why would you prefer code re ... -
<Java.JavaEE面试整理>(6) --Polymorphism之深入理解(一)
2008-04-02 12:14 2066Q 10:请谈谈你对多态,继承,封装和动态绑定(dynamic ... -
<Java.JavaEE面试整理>(5)
2008-04-01 08:49 1901Q 09: 请谈谈你对'is a'和'has a'的理解?也就 ... -
JavaPersistenceWithHibernate读书笔记(2)
2008-03-31 19:24 1185JavaPersistenceWithHibernate读书笔 ... -
<Java.JavaEE面试整理>(4)
2008-03-31 18:54 1439Q 06:Java中构造方法与其它常规方法有什么区别?要是你没 ... -
<Java.JavaEE面试整理>(3)
2008-03-30 16:31 1867Q 04: 怎么用Java里的Pack ... -
JavaPersistenceWithHibernate读书笔记(1)
2008-03-30 14:14 1785Part 1: Getting started with Hi ... -
<Java.JavaEE面试整理>(2)
2008-03-29 15:14 1823Java.J2EE.Job.Interview.Compani ...
相关推荐
2.2.1.4 keytool -export -alias tomcatsso -file tomcatsso.crt -keystore cacerts -storepass changeit 2.2.1.5 keytool -import -alias tomcatsso -file tomcatsso.crt -keystore cacerts -storepass changeit ...
<display-name>网站名称</display-name> <description>网站描述</description> <!-- icon元素包含small-icon和large-icon两个子元素.用来指定web站台中小图标和大图标的路径. --> <icon> <!--small-icon...
<param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 代码不完善,敬请...
<column name="ID" type="java.lang.Integer"/> </table> -<table schema="SCOTT" name="BIZ_CLAIM_VOUCHER"> <column name="ID" type="java.lang.Integer"/> </table> -<table schema="SCOTT" name="BIZ_...
<display-name>RegisterSy</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>...
<servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>velocity</servlet-name> ...
Spring MVC 是 Spring Framework 的一个重要模块,主要用于构建基于 Java 的 Web 应用程序。它提供了一个清晰的模型-视图-控制器(MVC)实现,帮助开发者分离业务逻辑与用户界面,简化 Web 应用开发流程。 #### 二...
这四个JAR文件组合在一起,通常用于构建一个基于Java EE的Web应用程序,其中JSF负责用户界面的展示和交互,JSTL提供方便的页面逻辑处理,而javaee.jar提供了整个Java EE平台的基础服务。开发者在使用这些库时,需要...
JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. ...
├<阶段1 java语言基础> │ ├<1-1-Java基础语法> │ ├<1-2 -面向对象和封装> │ └<1-3-Java语言高级> ├<阶段2 JavaWeb·> │ ├<01 HTML和CSS> │ ├<02 JavaScript> │ ├<03 BootStrap> │ ├<04 XML> │ ├...
<!!-- web.xml文件 --> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...
Spring MVC 框架搭建是 Java Web 开发中的一种常见架构模式,它基于 Model-View-Controller(MVC)模式,使用注解方式来处理请求和响应。下面将详细介绍 Spring MVC 框架的搭建过程和与 Hibernate 的整合实例。 一...
<servlet-name>JSON</servlet-name> <servlet-class>struts2.json.demo.JSON</servlet-class> </servlet> <servlet-mapping> <servlet-name>JSON</servlet-name> <url-pattern>/</url-pattern> </servlet-...
<param-value>UTF-8</param-value> </context-param> ``` ##### 4. `<filter>` 定义一个过滤器,它可以拦截请求,并对请求数据进行预处理或后处理。 ```xml <filter> <filter-name>MyFilter</filter-name> ...
以上内容是基于给定文件标题、描述、标签以及部分内容的综合分析所得出的关键知识点。这些知识点涵盖了JavaEE网络应用开发所需的基本环境搭建流程、数据库配置、项目导入与配置、工程功能需求说明以及技术支持等方面...
#### 二、Java中的Web Service技术 - **Java支持的Web Service技术**: - **官方技术**:如JAX-WS(Java API for XML Web Services),它是Sun Microsystems(现已被Oracle收购)提供的官方标准。 - **中间件...
<param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>...
Java和JavaEE是软件开发领域中的重要技术,广泛应用于企业级应用系统开发。这份压缩包包含的24个文件很可能是各种面试题目的集合,旨在帮助求职者准备Java和JavaEE相关的技术面试。以下是根据这些文件可能涵盖的知识...
### 基于全注解方式的SSH基础框架解析 #### 概述 本文将详细介绍一个基于Struts2、Spring、Hibernate以及Hibernate Generic DAO构建的基础框架。该框架使用全注解的方式进行配置,并且提供了清晰的包结构和文档,...
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> ...