J2EE & EJB Design Patterns Craig Larman

background image

1

www.craiglarman.com

Copyright © Craig Larman. All rights reserved.

J2EE and EJB are ™ Sun Microsystems Inc.

2

Copyright © Craig Larman. www.craiglarman.com

background image

2

3

Copyright © Craig Larman. www.craiglarman.com

correct

4

Copyright © Craig Larman. www.craiglarman.com

background image

3

5

Copyright © Craig Larman. www.craiglarman.com

6

Copyright © Craig Larman. www.craiglarman.com

Portland
Pattern
Repository

c2.com/ppr

Fowler,

Fowler,

. . .

. . .

background image

4

7

Copyright © Craig Larman. www.craiglarman.com

Terminal Client

Presentation

Application

(Sessions)

Domain

(Entities)

Integration

In

te

r-

T

ier
D
a

ta

T

ran
sf

er

T

ran
sact

io

n

s

Pe
rs

is

te

nc
e

Ge
n

e

ra

l E
J

B

C

lie

nt
-S
ide

E
J

B

!"

!"

Terminal Client

Presentation

Application

(Sessions)

Domain

(Entities)

Integration

In

te

r-

Tier

D
a

ta

Tr
ansf

er

T

ran
sact

io

n

s

Pe
rs

is

te

nc
e

Ge
n

e

ra

l E
J

B

C

lie

nt
-S

ide

E
J

B

background image

5

9

Copyright © Craig Larman. www.craiglarman.com

#$

#$

10

Copyright © Craig Larman. www.craiglarman.com

#$

#$

javax.ejb

«interface»

EJBObject

. . .

MyApp

«interface»

IMySessionBase

doWork() throws RemoteException

«interface»

SessionBean

ejbActivate()
ejbPassivate()
ejbRemove()
setSessionContext(...)

«interface»

IMySession

MySessionBeanImpl

ejbActivate()
. . .
doWork()

In UML, means no
operations declared

background image

6

11

Copyright © Craig Larman. www.craiglarman.com

MyApp

«interface»

IMySessionBase

doWork() throws RemoteException

MySessionBeanImpl

ejbActivate()
. . .
doWork()

«interface»

IMySession

Note that doWork() should not
declare throws RemoteException
,
but it can internally throw
EJBExceptions.

12

Copyright © Craig Larman. www.craiglarman.com

javax.ejb

«interface»

EJBLocalObject

. . .

MyApp

«interface»

IMyEntityBase

doEntityWork()

«interface»
EntityBean

. . .

«interface»

IMyEntity

MyEntityBeanImpl

. . .

doEntityWork()

background image

7

13

Copyright © Craig Larman. www.craiglarman.com

%

%

!& ' #$

!& ' #$

! " #

$

14

Copyright © Craig Larman. www.craiglarman.com

%

%

!& ' #$

!& ' #$

MyStockSessionBean

. . .

setData( Map )

setDJIQuotes( List )

getDJIQuotes() : List
getQuotes( symbols : List ) : List

background image

8

15

Copyright © Craig Larman. www.craiglarman.com

$

% "

" & $

16

Copyright © Craig Larman. www.craiglarman.com

«interface»

SessionBean

ejbActivate()
ejbPassivate()
ejbRemove()
setSessionContext(...)

AbstractSession

ejbActivate()
ejbCreate()
ejbPassivate()
ejbRemove()
setSessionContext(...) {frozen}
. . .

Session1

Session2

// no op
{ }

background image

9

17

Copyright © Craig Larman. www.craiglarman.com

public abstract class AbstractSession implements SessionBean {

private SessionContext ejbContext;

. . .

// restrict as only way to cache or return the context

public final void setSessionContext( SessionContext ctx ) . . .

protected final SessionContext getSessionContext() . . .

// common helper services

protected void log( String message ) . . .

protected Connection getConnection( String url ) throws SQLException . . .

}

"

"

%

%

&

&

Terminal Client

Presentation

Application

(Sessions)

Domain

(Entities)

Integration

In

te

r-

Tier

D
a

ta

Tr
ansf

er

T

ran
sact

io

n

s

Pe
rs

is

te

nc
e

Ge
n

e

ra

l E
J

B

C

lie

nt
-S

ide

E
J

B

background image

10

19

Copyright © Craig Larman. www.craiglarman.com

" '

( ) ' (*

$ $ $

(+, # $

20

Copyright © Craig Larman. www.craiglarman.com

1

ServiceLocator

- instance : ServiceLocator
- ctx : InitialContext
- ejbHomes : Map of EJBHome
- queueFactory : QueueConnectionFactory
- topicFactory : TopicConnectionFactory

+ getInstance() : ServiceLocator

- «constructor» ServiceLocator()

«EJB Services»

+ getEJBHome( homeType : Class ) : EJBHome
+ getBean( handleID : String ) : EJBObject
+ getHandleID( EJBObject ) : String

«JMS Services»

+ getQueueConnectionFactory() : QueueConnectionFactory
+ getTopicConnectionFactory() : TopicConnectionFactory

or "ServiceFactory"

Correct UML way to
indicate Singleton use in the
class diagram.
(very few know this UML
tidbit)

// plus exception handling...
{
ctx = new InitialContext(
System.getProperties() );
}

background image

11

21

Copyright © Craig Larman. www.craiglarman.com

class MyClient {

public void foo() {

ForumServicesHome forumServicesHome =

(ForumServicesHome) ServiceLocator.getInstance().

getEJBHome(ForumServicesHome.class );

IForumServices forumServices = forumServicesHome.create();

forumServices.createForum( "J2EE and JMS" );

}

}

22

Copyright © Craig Larman. www.craiglarman.com

class ServiceLocator {

public EJBObject getBean( String handleID ) {

try {

byte[ ] bytes = handleID.getBytes();

InputStream is = new ByteArrayInputStream( bytes );

ObjectInputStream = ois = new ObjectInputStream( is );

Handle handle = (Handle) ois.readObject();

return handle.getEJBObject();

}

. . .

}

}

background image

12

23

Copyright © Craig Larman. www.craiglarman.com

!!

" #$ % &

(- % . / 0

& ! " #$'

. 1 (!) $'

24

Copyright © Craig Larman. www.craiglarman.com

" #$ "()* ! +

%! %

,! !+-

. /()0

background image

13

25

Copyright © Craig Larman. www.craiglarman.com

// web.xml

<ejb-ref>

<ejb-ref-name> com.foo.forumapp.ForumServicesHome </ejb-ref-name>

<ejb-ref-type> Session </ejb-ref-type>

<home> com.foo.forumapp.ForumServicesHome </home>

<remote> com.foo.forumapp.IForumServices </remote>

</ejb-ref>

// weblogic.xml

<reference-descriptor>

<ejb-ref-name> com.foo.forumapp.ForumServicesHome </ejb-ref-name>

<jndi-name> ForumServicesHomeActualJNDIKey </ jndi-name >

</reference-descriptor >

26

Copyright © Craig Larman. www.craiglarman.com

//

"java:comp/env/ejb/" is used for all JNDI lookups that have been made

// available in the environment

class ServiceLocator {

public EJBHome getEJBHome( Class homeType ) {

try {

Object ref = ctx.lookup("java:comp/env/ejb/" + homeType.getName() );

return (EJBHome) PortableRemoteObject.narrow( ref, homeType );

}

. . .

}

}

background image

14

27

Copyright © Craig Larman. www.craiglarman.com

1 +$ ,- ! %

%'

#$ 2 "( ,!

/! / "('-

3 +$4

+$ %) !

28

Copyright © Craig Larman. www.craiglarman.com

5657 7

2 )

'

(

This name

captures the core
pattern applied in

many contexts

background image

15

29

Copyright © Craig Larman. www.craiglarman.com

"

"

!

"! !

!

! #!! $

%! ! ! !&

!

30

Copyright © Craig Larman. www.craiglarman.com

"

"

MyServlet

. . .

. . .

ForumServices

forumServices : IForumServices
handle : javax.ejb.Handle
. . .

createForum( String ) {guarded}
. . .

«interface»

IForumServicesBase

createForum( String )
. . .

«interface»

EJBObject

1

ServiceLocator

HttpServlet

«interface»

IForumServices

1

«interface»

Serializable

probably stored in the
ServletSession

background image

16

31

Copyright © Craig Larman. www.craiglarman.com

"

"

Also supports Convert Exceptions.

class ForumServices implements IForumServicesBase, Serializable {

private IForumServices forumServices;

. . .

public void synchronized createForum( String forumName ) {

try {

forumServices.createForum( forumName );

}

catch( RemoteException ex ) {

// crack open and throw appropriate ForumException. . .

if ( . . . ) throw new DuplicateForumNameException( . . . );

}

32

Copyright © Craig Larman. www.craiglarman.com

"

"

Notes on Java interface implementation:

interface IForumServicesBase {

public void createForum ( String forumName ) throws RemoteException;

}

class ForumServices implements IForumServicesBase {

// legal to add “synchronized” and remove “throws”

public void synchronized createForum( String forumName ) {

}

background image

17

33

Copyright © Craig Larman. www.craiglarman.com

"

"

(

(

org.apache.struts

action

1

ActionServlet

doGet(...)
doPost(...)
process(...)
processXXX(...)
...

Action

perform(ActionForm, ...): ActionForward
...

URI pattern

1

MyPresentationSubsystem

JSPServlet1

doGet(...)
doPost(...)
...

JSPServlet2

doGet(...)
doPost(...)
...

CreateForumAction

perform(...): ActionForward
...

ForumServices

createForum(. . .)
. . .

34

Copyright © Craig Larman. www.craiglarman.com

"

"

background image

18

Terminal Client

Presentation

Application

(Sessions)

Domain

(Entities)

Integration

In

te

r-

Tier

D
a

ta

Tr
ansf

er

T

ran
sact

io

n

s

Pe
rs

is

te

nc
e

Ge
n

e

ra

l E
J

B

C

lie

nt
-S

ide

E
J

B

36

Copyright © Craig Larman. www.craiglarman.com

) ""

) ""

background image

19

37

Copyright © Craig Larman. www.craiglarman.com

!

!

"

"

class CommandControllerServlet extends HttpServlet {

// called from doGet and doPost

protected void processRequest(

HttpServletRequest request, HttpServletResponse response ) {

Command cmd = CommandFactory.getInstance().getNewCommand(

request.getParameter( "op" ) );

resultPage = cmd.execute( request, response );

}

. . .

38

Copyright © Craig Larman. www.craiglarman.com

!

!

"#

"#

org.apache.struts

action

1

ActionServlet

+doGet(...)
+doPost(...)
#process(...)
#processActionCreate(...)
#processActionPerform(...)
#processXXX(...)
...

Action

+perform(ActionForm, ...): ActionForward
...

MyPresentationSubsystem

MyAction1

+perform(...): ...
...

ActionForm

...

MyActionForm1

+getZipCode(): String
+setZipCode(String)
...

Template
Method
pattern

Hook
methods

Invoke the
process
template
method

1

HttpRequest

background image

20

*"

*"

Terminal Client

Presentation

Application

(Sessions)

Domain

(Entities)

Integration

In

te

r-

Tier

D
a

ta

Tr
ansf

er

T

ran
sact

io

n

s

Pe
rs

is

te

nc
e

Ge
n

e

ra

l E
J

B

C

lie

nt
-S

ide

E
J

B

40

Copyright © Craig Larman. www.craiglarman.com

*"

*"

$



%&

# ' ( )

* ()

background image

21

41

Copyright © Craig Larman. www.craiglarman.com

)

)

+

+

&

&

+ ,

#

# #

-. /

42

Copyright © Craig Larman. www.craiglarman.com

)

)

+

+

&

&

: TraderSession

: Object

buy( total, order )

IAccountEntity

b := getBalance()

: Object

IBuyerSession

[total < b] buy( order )

SessionBean

ITraderSessionBase

background image

22

43

Copyright © Craig Larman. www.craiglarman.com

)

)

+

+

&

&

: TransferSession

: Object

transfer( amt, acct1, acct2 )

IWithdrawSession

withdraw(amt, acct1)

: Object

IDepositSession

deposit(amt, acct2)

SessionBean

ITransferSessionBase

44

Copyright © Craig Larman. www.craiglarman.com

)

)

+

+

&

&

(

(

" #$

" #$

class TraderSession implements ITraderSessionBase, SessionBean {

private AccountLocalHome acctHome;

public void buy( . . . ) {

try {

initializeHomes();

IAccount acct = acctHome.findByPrimaryKey( . . . );

Money balance = acct.getBalance();

. . .

}

}

private void initializeHomes( . . . ) {

try {

acctHome = (AccountLocalHome) ServiceLocator.getInstance().

getEJBHome( AccountLocalHome.class );

}

. . .

background image

23

45

Copyright © Craig Larman. www.craiglarman.com

)

)

+

+

&

&

(

(

,

,

!"

#

46

Copyright © Craig Larman. www.craiglarman.com

)

)

+

+

&

&

" % -

" 0

-

0

&

background image

24

47

Copyright © Craig Larman. www.craiglarman.com

- )

- )

+

+

&

&

1

" 0/

!

48

Copyright © Craig Larman. www.craiglarman.com

- )

- )

+

+

&

&

"

# $ %

!

' 23"

#


!

background image

25

49

Copyright © Craig Larman. www.craiglarman.com

- )

- )

+

+

&

&

:Server

«process»

:JVM

: Object

1: t := isValidID(patronID)
2 * : borrowResource(callNum)
3: complete()

All system operations for the "Borrow Library Resources" use
case are handled by the same stateful SessionBean.

IBorrowResources

Session

50

Copyright © Craig Larman. www.craiglarman.com

- )

- )

+

+

&

&

&

#

"

#

! " !

# $

" 4

# (#) #

#

background image

26

51

Copyright © Craig Larman. www.craiglarman.com

. )&

. )&

+ & -.

&/

52

Copyright © Craig Larman. www.craiglarman.com

. )&

. )&

ReserveSeatMDB

. . .

onMessage()

«interface»

MessageDrivenBean

ejbRemove()
setMessageDrivenContext(...)

1

ServiceLocator

«interface»

IAirline

. . .

«interface»

MessageListener

onMessage( Message )

«interface»

IFlight

. . .

background image

27

53

Copyright © Craig Larman. www.craiglarman.com

5

5

0

0

!

!

# *

# *

MyServlet

. . .

. . .

AirlineServices

. . .

reserveSeat(...)
. . .

1

ServiceLocator

HttpServlet

JMS

:Server

ReserveSeatMDB

onMessage(...)
. . .

:Client

54

Copyright © Craig Larman. www.craiglarman.com

. )&

. )&

background image

28

Terminal Client

Presentation

Application

(Sessions)

Domain

(Entities)

Integration

In

te

r-

Tier

D
a

ta

Tr
ansf

er

T

ran
sact

io

n

s

Pe
rs

is

te

nc
e

Ge
n

e

ra

l E
J

B

C

lie

nt
-S

ide

E
J

B

56

Copyright © Craig Larman. www.craiglarman.com

/

/

%

,

#

6# /

!

background image

29

57

Copyright © Craig Larman. www.craiglarman.com

/

/

" 0

7

# %

! !

##&#! #

'(

58

Copyright © Craig Larman. www.craiglarman.com

/

/

Use local interfaces for other sessions and
especially entities.

class TraderSession implements ITraderSessionBase, SessionBean {

private AccountLocalHome acctHome;

. . .

}

private void initializeHomes( . . . ) {

try {

acctHome = (AccountLocalHome) ServiceLocator.getInstance().

getEJBHome( AccountLocalHome.class );

}

. . .

background image

30

59

Copyright © Craig Larman. www.craiglarman.com

" % -

" % -

!

!

6#

6#

' () *

) ')

+

!

60

Copyright © Craig Larman. www.craiglarman.com

" % -

" % -

!

!

6#

6#

,+ )

*

$!)! -./&

0% '

!

background image

31

61

Copyright © Craig Larman. www.craiglarman.com

" % -

" % -

!

!

6#

6#

62

Copyright © Craig Larman. www.craiglarman.com

" % -

" % -

!

!

6#

6#

background image

32

63

Copyright © Craig Larman. www.craiglarman.com

/

/

.* 3

* 14

-

*1 -

64

Copyright © Craig Larman. www.craiglarman.com

) #&*+ ,$- ,$- ." ( .$

/ ( 0

(" !&(

# # #

1%# %2 #%$

.!%$$$

%(% 3$

4' #(%%($$$

background image

33

65

Copyright © Craig Larman. www.craiglarman.com

A_EB

B

C

D

H_EB

I

J

Entity Beans

"Regular" Java objects

A

H

*

1

1

*

1

1

1

EntityBean

EntityBean

66

Copyright © Craig Larman. www.craiglarman.com

-

-

!

!

* 14

* 14

AccountEntity

PhoneNumber

Address

Account

*

1

EntityBean

Person

1

Name

1

Contract

1

*

AccountEntity

PhoneNumber

Address

*

1

EntityBean

Person

Name

1

Contract

1

*

PersonEntity

1

EntityBean

Account

personPK

If Person only exists as long as
the account exists--depends on
business rules.

Asset

*

Asset

*

background image

34

67

Copyright © Craig Larman. www.craiglarman.com

-

-

!

!

-. 89 5

-. 89 5

! "

#$% #%

!

&

68

Copyright © Craig Larman. www.craiglarman.com

-

-

!

!

"## :

"## :

AccountEntity

1

EntityBean

Contract

1

Account

...

Asset

*

EntityBean

Contract

1

AccountEntity

...

Asset

*

Bridge pattern variation.
AccountEntity is the Bridge Abstraction.
Account is the Bridge Implementor.

background image

35

69

Copyright © Craig Larman. www.craiglarman.com

-

-

!

!

3

3

!

!

AccountEntity

Address

1

EntityBean

Person

Name

1

Contract

1

*

PersonEntity

1

EntityBean

Account

personPK

Asset

*

70

Copyright © Craig Larman. www.craiglarman.com

-

-

!

!

-. ;; "#

-. ;; "#

#'

('% )*+ , --

#. (

%

. ( +/

( .

background image

36

71

Copyright © Craig Larman. www.craiglarman.com

-

-

!

!

0

1

:# , ,

72

Copyright © Craig Larman. www.craiglarman.com

-

-

!

!

1

1

Asset

descrip
...

AccountEntity

// promoted operations ( LOD )
setAssetDescription(...)
setContractDate(...)

// group setter. (serialized data).
// Implement with Reflection.
setData( keyValuePairs )

// set child objects (serialized)
setAsset( Asset )
setContract( Contract )

// entire graph (serialized)
getGraph() : Account
setGraph( Account )

Contract

date
...

Account

personPK
...

EntityBean

1

*

1

background image

37

73

Copyright © Craig Larman. www.craiglarman.com

$ '&

$ '&

2

% #

/

2

<$; * 5 5

-. 89

5 4

74

Copyright © Craig Larman. www.craiglarman.com

$ '&

$ '&

,2

.*

32 4

1 #

#

background image

38

75

Copyright © Craig Larman. www.craiglarman.com

$ '&

$ '&

35

" 0 *

14

6 5

3"

76

Copyright © Craig Larman. www.craiglarman.com

$ '&

$ '&

72

=#

1, *

4 *

> #

- # * #&

3 exact *

1, * #

background image

39

77

Copyright © Craig Larman. www.craiglarman.com

$ '&

$ '&

.8.9 , 9 :2

? 3

78

Copyright © Craig Larman. www.craiglarman.com

%

%

&

&

2

+

/ %

# /

background image

40

79

Copyright © Craig Larman. www.craiglarman.com

%

%

&

&

,2

' "

% -

- #

# 7



80

Copyright © Craig Larman. www.craiglarman.com

*

*

2

" @

&

5 >

.*1 .*

:

(3* 1* ?* )

? #

( )

background image

41

81

Copyright © Craig Larman. www.craiglarman.com

*

*

,2

*1 #

5 .*

( )

82

Copyright © Craig Larman. www.craiglarman.com

*

*

«interface»

IAccountDAO

delete(...)
insert(...)
find(...) : ...
select(...) : List
update(...)
. . .

JDBCAccountDAO

. . .

InMemoryAccountDAO

. . .

background image

42

83

Copyright © Craig Larman. www.craiglarman.com

* 14

* 14

!

!

*1

*1

1

DAOFactory

- instance : DAOFactory
- daos : Map

+ getInstance() : DAOFactory

+ getAccountDAO() : IAccountDAO
+ getCustomerDAO() : ICustomerDAO
. . .

{
if ( in map )
return from map
else
String className = get the property
Class c = Class.forName( className )
IAccountDAO dao = (IAccountDAO) c.newInstance()
save dao in map
return dao
}

84

Copyright © Craig Larman. www.craiglarman.com

* 14

* 14

!

!

' */

' */

! / #(%

3

5

)

* ' 14

background image

43

85

Copyright © Craig Larman. www.craiglarman.com

*

*

5

"

5

"

86

Copyright © Craig Larman. www.craiglarman.com

*

*

.8.9 , 9 :2

background image

44

87

Copyright © Craig Larman. www.craiglarman.com

"

"

2

3 #

88

Copyright © Craig Larman. www.craiglarman.com

"

"

,2

-. 89

1

*1 >

" 0

background image

45

#

#

%

%

0

0

0$

0$

Terminal Client

Presentation

Application

(Sessions)

Domain

(Entities)

Integration

In

te

r-

Tier

D
a

ta

Tr
ansf

er

T

ran
sact

io

n

s

Pe
rs

is

te

nc
e

Ge
n

e

ra

l E
J

B

C

lie

nt
-S

ide

E
J

B

90

Copyright © Craig Larman. www.craiglarman.com

0$

0$

2

+

& #

/

background image

46

91

Copyright © Craig Larman. www.craiglarman.com

0$

0$

,2

.

# #

92

Copyright © Craig Larman. www.craiglarman.com

0$

0$

.

! ?

'

background image

47

93

Copyright © Craig Larman. www.craiglarman.com

0$

0$

;

", ! 3

,

*'1

94

Copyright © Craig Larman. www.craiglarman.com

0$

0$

OrderDTO

+ number
+ submissionDate
+ fulfillmentDate
. . .

OrderDTO

- number
- submissionDate
- fulfillmentDate
. . .

getNew( . . . ) : OrderDTO

getNumber()
getSubmissionDate()
. . .

Serializable

Serializable

The Immutable DTO variant

background image

48

95

Copyright © Craig Larman. www.craiglarman.com

* ' 14

* ' 14

!

!

# *'1

# *'1

<5

?

' #

# (

5+)

96

Copyright © Craig Larman. www.craiglarman.com

* ' 14

* ' 14

!

!

# *'1

# *'1

= 9

>

(

)*+

?

background image

49

97

Copyright © Craig Larman. www.craiglarman.com

* ' 14

* ' 14

!

!

# *'1

# *'1

# *'1

OrderDTO o = oldDTO.getNew( newData1, newData2 );

98

Copyright © Craig Larman. www.craiglarman.com

* ' 14

* ' 14

!

!

# *'1

# *'1

OrderDTO o = oldDTO.getNew( newData1, newData2 );

background image

50

99

Copyright © Craig Larman. www.craiglarman.com

* ' 14

* ' 14

!

!

:

:

> 2

*'1

" 6#

4 (

)

100

Copyright © Craig Larman. www.craiglarman.com

* ' 14

* ' 14

!

!

:

:

:)"

"

try { OrderValues o = new OrderValues ( . . . ) ; }

catch ( ValidationException ex ) { . . . }

background image

51

101

Copyright © Craig Larman. www.craiglarman.com

* ' 14

* ' 14

!

!

:

:

=

-

102

Copyright © Craig Larman. www.craiglarman.com

0$

0$

3 0>@5

*1 ##

*1 *'1 *1

' *'1 ! #

background image

52

103

Copyright © Craig Larman. www.craiglarman.com

0$

0$

. 0>@

5

A

104

Copyright © Craig Larman. www.craiglarman.com

0$

0$

3

" # ( '"")

*'1

#

#

'

-. 89

6# 5

#

background image

53

105

Copyright © Craig Larman. www.craiglarman.com

0$

0$

.8.

:# 14

14

3 14

" +

* 14

? ", 14

* ' 14

%B

106

Copyright © Craig Larman. www.craiglarman.com

0 .

0 .

2

*'1 # 6#

*'1

#

*'1

background image

54

107

Copyright © Craig Larman. www.craiglarman.com

0 .

0 .

,2

' 5 ( +5)

108

Copyright © Craig Larman. www.craiglarman.com

0 .

0 .

.2

-

1 4

0 72

?

& 6#

background image

55

109

Copyright © Craig Larman. www.craiglarman.com

*'1 5

*'1 5

!

!

" C

" C

class Keys implements Serializable {

public static final String ORDER_ID = "ID";

. . .

}

. . .

Object value = dtoMap.get( Keys.ORDER_ID );

110

Copyright © Craig Larman. www.craiglarman.com

"

"

2

+ 5

5 #/

background image

56

111

Copyright © Craig Larman. www.craiglarman.com

"

"

AccountEntity

- ctx : EntityContext

«accessors»

+ getBalance() : Money
+ setBalance( Money )
. . .

«ejb supporting»

+ ejbCreate(...)
. . .

«business»

+ deposit(. . .)
. . .

AccountBMPEntity

- balance
. . .

«accessors»

+ getBalance() : Money
+ setBalance( Money )
. . .

«ejb supporting»

+ ejbCreate(...)
. . .

«finders»

+ ejbFindByPrimaryKey(. . .)
. . .

EntityBean

112

Copyright © Craig Larman. www.craiglarman.com

--# $

--# $

2

< #6# &

< * & @

*

% #

(* &

4 D)

background image

57

113

Copyright © Craig Larman. www.craiglarman.com

--# $

--# $

,2

# #6# * (;E @ F8

) *

114

Copyright © Craig Larman. www.craiglarman.com

--# $

--# $

0 -A2 " B

,>

0 C-D2 " B

!

0 -EF2 "

,G @(

0 HB2 B

(,<

background image

58

115

Copyright © Craig Larman. www.craiglarman.com

--# $

--# $

private int timeLow; //32 bits

private String hexInetAddress; //32 bits
private String thisHashCode; // 32 bits
private String midValue;

public void initialize() {

// initalise the secure random instance
seeder = new SecureRandom();

// get the inet address

InetAddress inet = InetAddress.getLocalHost();
byte [ ] bytes = inet.getAddress();
hexInetAddress = hexFormat( getInt( bytes ), 8 );

// get the hashcode

thisHashCode = hexFormat( hashCode(), 8 );

116

Copyright © Craig Larman. www.craiglarman.com

--# $

--# $

//...-xxxx-xxxx-xxxx-xxxx.. mid part of the sequence

StringBuffer buffer = new StringBuffer();

buffer.append("-");
buffer.append(hexInetAddress.substring(0,4));
buffer.append("-");
buffer.append(hexInetAddress.substring(4));
buffer.append("-");
buffer.append(thisHashCode.substring(0,4));
buffer.append("-");
buffer.append(thisHashCode.substring(4));
midValue = buffer .toString();

}

background image

59

117

Copyright © Craig Larman. www.craiglarman.com

--# $

--# $

public String getUUID()

{

long timeNow = System.currentTimeMillis();
timeLow = (int) timeNow & 0xFFFFFFFF;

int node = seeder.nextInt();

return

(hexFormat(timeLow, 8) + midValue + hexFormat(node, 8));

}

background image

60

119

Copyright © Craig Larman. www.craiglarman.com

! I-+ 4,0

-CCA- J

3/ )*+5

3 5

, *0@ 5

120

Copyright © Craig Larman. www.craiglarman.com

#$

#$


Wyszukiwarka

Podobne podstrony:
eBook EJB Design Patterns(ebook pdf wiley)
(Oracle) J2EE Design Patterns
Learning Python Design Patterns Gennadiy Zlobin signed
BYT 2004 Design Patterns
16352479 Software Design Patterns Made Simple
Design Patterns Elements of Reusable Object Oriented Software Examples
Ebook Delphi Modelmaker Design Patterns Mmdesignpatterns
[eBook] Automatic Code Generation from Design Patterns
(eBook PDF GUI) Design Patterns as Tools for User Interface Design
A Classification of Design Patterns
Developing A Universal Data Access Layer Leveraging Ado Net, C Sharp And Factory Design Pattern
design patterns
J2EE EJB UML Diagrams
Addison Wesley An Introduction to Design Patterns
Pagan Patterns and Designs by Eliza Fegley

więcej podobnych podstron