📄 lib0067.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Chapter 10: Building Value Objects</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0066.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0068.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch10"></a>
<h1 class="chapter-title"><span class="chapter-titlelabel">Chapter 10: </span>Building Value Objects</h1><div class="section">
<h2 class="sect2-title">
<a name="301"></a>Overview</h2>
<a name="302"></a><a name="IDX-121"></a>
<p class="para">A value object (VO) is a lightweight, serializable (i.e., implements <span class="fixed">java.io.Serializable</span>) object that structures groups of data items into a single logical construct. For example, <span class="fixed">EmployeeVO</span> is a class in a human resources application that is a value object containing the data last name, first name, employee ID, job position, and start date. <span class="fixed">EmployeeVO</span> is used in all layers of the application, from presentation to data access. The term <i class="emphasis">value object</i> comes directly from the value object design pattern. Some texts use <i class="emphasis">data transfer object</i> instead.</p>
<p class="para">The value object design pattern, as written, is intended to minimize network traffic between enterprise beans and their callers (because each argument passed initiates a network transmission). In addition, it is designed to improve the performance of enterprise beans by minimizing the number of method arguments, and thus network transmissions, needed to call them. For example, it's more efficient to call the <span class="fixed">addEmployee()</span> method of an enterprise bean passing an <span class="fixed">EmployeeVO</span> than it is to require individual arguments for last name, first name, and so on. I think of the value object design pattern in much broader terms and use VOs to communicate information between all layers of the application, as illustrated in <a class="internaljump" href="#ch10fig01">figure 10.1</a>.</p>
<div class="figure">
<a name="303"></a><a name="ch10fig01"></a><span class="figuremediaobject"><a href="images/fig138%5F01%5F0%2Ejpg" NAME="IMG_29" target="_parent"><img src="images/fig138_01.jpg" height="251" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 10.1: </span>Using Value Objects Within a Layered Architecture</span>
</div>
<p class="para">At first glance, my broader definition of a value object appears to contradict the principles of object-oriented design that tell us to combine data <a name="304"></a><a name="IDX-122"></a>with the business logic. Those principles would have us think of "employee" as an object that contains its data (e.g., last name, first name) and methods such as <span class="fixed">add()</span>, <span class="fixed">terminate()</span>, and <span class="fixed">oppress()</span> that represent business logic. For many practical considerations, such as increasing the performance of enterprise beans, we need to have the option for referencing data outside the business logic context.</p>
<p class="para">
<a href="LiB0089.html#429" target="_parent" class="chapterjump">Chapter 13</a> will show you ways of constructing objects in the business logic layer that adhere to object-oriented design principles and also allow you to reference the data portion of these objects as a value object. For example, the <span class="fixed">Employee</span> class could easily provide a <span class="fixed">getEmployeeVO()</span> accessor that provides the data for an employee without its business logic.</p>
<div class="highlights">
<p class="first-para">Because the technical architect is responsible for establishing coding standards and guidelines and mentoring development staff, this chapter provides several implementation tips and techniques for value objects. Additionally, the chapter explains several concepts needed for effectively structuring value objects. And to make implementing these recommendations <a name="305"></a><a name="IDX-123"></a>easier and less time consuming, the chapter presents a <span class="fixed">ValueObject</span> class, which I've included in the CementJ initiative (<a target="_top" class="url" href="http://sourceforge.net/projects/cementj/">http://sourceforge.net/projects/cementj/</a>).</p>
</div>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0066.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0068.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -