EJB Design Patterns4(0分)

  • 主题发起人 主题发起人 VRGL
  • 开始时间 开始时间
V

VRGL

Unregistered / Unconfirmed
GUEST, unregistred user!
Design Pattern IV
In Design Pattern III, we saw that the size of the Entity Bean was greatly reduced by
having the Entity Bean inherit from the Struct and thereby making all the fields
(columns in the database) available for CMP. Here we further reduce the size of the
Entity Bean in the implementation of the setData() and getData() methods
by introducing an additional method in the Struct.
Value Object code snippet for Company
public class CompanyStruct implements
java.io.Serializable {
public Integer comId;
public String comName;
public String comDescription;
public Timestamp mutationDate;
public void copyFrom(CompanyStruct struct) {
comId = struct.comId;
comName = struct.comName;
comDescription = struct.comDescription;
mutationDate = struct.mutationDate;
}
}
Since Entity Bean inherits from the Struct defined above, the copyFrom()
method is available in the implementation class. However it should be noted that the
copyFrom() method is not a business method i.e. it is not exposed in the remote
interface of the entity bean. Now the getData() and setData() methods in
the Entity Bean’s implementation class in Design Patterns II and III can be reduced
to:
Code snippet for an Entity Bean’s getData()
public CompanyStruct getData() {
CompanyStruct result = new CompanyStruct();
result.copyFrom(this);
return result;
}
The “this” passed as a parameter to the copyFrom() method refers to the
Entity Bean implementation class. Since it inherits from the Struct, the entity bean
implementation class passed as a parameter to the copyFrom() method is actually
cast to the Struct class. EJB? vendors frown upon passing “this” as a parameter
because two threads of control could have a reference to the same bean and a head-on
collision could occur with transaction contexts when a method is invoked on it.
What we havedo
ne above is not in violation of the EJB? vendor’s suggestion as
the reference is not passed between beans and no methods are called on the
reference that could result in a transaction collision.
Code snippet for an Entity Bean’s setData()
public void setData(CompanyStruct struct) {
this.copyFrom(struct);
}
Similarly, the above method copies the values from the Struct to the Entity Bean.
With an Entity Bean that maps to a table with a large number of columns, the
advantages are extremely high with respect to the size of code in the Entity Bean
implementation class. The above design pattern makes the code extremely compact,
easy to read, understand and maintain.
Also, any change to the Database table requires only a change in the Struct. Hardly
(if any) code needs to be changed in the Entity Bean implementation class. Apart
from this change to the Struct, the Deployment Descriptor needs to be changed
when CMP fields are added or removed thus improving your team’s response to
normal development as well as changes in specifications etc.
At this stage, the relationship between the Company and Employee Entity Bean is
still not modeled. This isdo
ne in Design Pattern V.
 
应杜宝的要求,贴出来,望大家指正。

设计模式4
在设计模式3中我们看到使bean从struct继承后使得代码大
幅缩水并且所有的字段都可定义为cmp字段。这里,我们可
以更进一步修正setdata()和getdata()的实现方法来减少代码量。
我们为这个struct增加一个方法。

Value Object code snippet for Company
public class CompanyStruct implements
java.io.Serializable {
public Integer comId;
public String comName;
public String comDescription;
public Timestamp mutationDate;
public void copyFrom(CompanyStruct struct) {
comId = struct.comId;
comName = struct.comName;
comDescription = struct.comDescription;
mutationDate = struct.mutationDate;
}
}

由于entity bean是从struct继承下来的,在bean的实现类
中也一样可以引用copyfrom()方法,当然,必须注意的是,
这个copyfrom()方法并不是一个商业方法,它不需要在bean
的远程接口中暴露给调用者。
现在,getdata()和setdata()方法可以简化更进一步的简化。

Code snippet for an Entity Bean’s getData()
public CompanyStruct getData() {
CompanyStruct result = new CompanyStruct();
result.copyFrom(this);
return result;
}
这里把this作为一个参数传入copyfrom()。由于enttity bean
从struct继承而来,于是这个entitty bean便可以作为一个
struct传入。
EJB容器并不赞成把this指针作为一个参数传递因为在两个控
制线程中同时访问一个bean的实例可能会引起事务冲突。但事
实上我们所做的并没有违背这个原则,因为我们的并没有在
bean之间传递this的引用并且也没有引用任何可能引起事务冲突的方法。
Code snippet for an Entity Bean’s setData()
public void setData(CompanyStruct struct) {
this.copyFrom(struct);
}
类似的,上面的方法把struct的值赋给enttity bean。
对于一个映射到有很多列的表的entity bean,这种实现
方法的优点是使得bean实现类的代码非常简单。这种设
计模式使得代码及其精简,可读性和可维护性也大大增强。
任何数据库的修改都只需要修改作为基类的struct,而几
乎不需要修改bean的代码。把这种改变从struct分离出来,
当cmp字段发生改变时需要修改部署描述符。这就使得开
发时能够更好的适应设计的改变。
这里,还是没有实现bean之间的关系,这将在设计模式5中解决。
 
接受答案了.
 

Similar threads

A
回复
0
查看
981
Andreas Hausladen
A
A
回复
0
查看
932
Andreas Hausladen
A
A
回复
0
查看
607
Andreas Hausladen
A
A
回复
0
查看
805
Andreas Hausladen
A
后退
顶部