Upcoming SlideShare
×

# GoF J2EE Design Patterns

1,575 views

Published on

GoF J2EE Design Patterns

Published in: Technology
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,575
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
0
0
Likes
3
Embeds 0
No embeds

No notes for slide

### GoF J2EE Design Patterns

1. 1. @Diego  Thanh  Nguyen  -­‐  http://free.smartbiz.vn  J2EE  DESIGN  PATTERN  10/8/12   www.smartbiz.vn   1
2. 2. Table  Of  Content  q I.  GoF  Design  Pattern   v I.1.  Creation  Patterns   v I.2.  Structural  Patterns   v I.3.  Behavioral  Patterns  q II.  J2EE  Presentation:    q III.  J2EE  Business:    q IV.  J2EE  Integration   10/8/12   www.smartbiz.vn   2
3. 3. I.  How  Design  Patterns  Arise  ?   Pattern  1.   Context Problem   a design situation giving rise to a design problem   Problem a set of forces occuring in that context Solution 2.  Context   a form or rule that can be applied to resolve these forces Forces   IF    you  Nind  yourself  in  CONTEXT   3.                              for  example  EXAMPLES,   Solution                              with  PROBLEM,                                entailing  FORCES   THEN    for  some  REASONS,                              apply  DESIGN  FORM  AND/OR  RULE  BeneFits   Consequences                              to  construct  SOLUTION   Related  Patterns                              leading  to  NEW  CONTEXT  &  OTHER  PATTERNS   10/8/12   www.smartbiz.vn   3
4. 4. I.  Design-­‐Pattern  Catalog   ! ! Purpose! Creational! Structural! Behavioral! Class! • Factory!Method! • Adapter! • Interperter! • Singleton! • Façade! • Observer! • Factory! • Decorator! • Command! • Abstract!Factory! • Composite! • Iterator! Scope! • Builder! • Adapter! • Chain!of!Responsibility! Object! • Prototype! • Bridge! • Mediator! • Object!Pool! • Flyweight! • Momento! • Proxy! • State! • Strategy! • Visitor! ! ! Defer  object  creation  to     Describe  algorithms  and     another  object   Nlow  control   Describe  ways  to     assemble  objects  10/8/12   www.smartbiz.vn   4
5. 5. I.1.  Creation  Patterns  q Creational  Patterns    prescribe  the  way   that  objects  are  created.  These  patterns  are   used  when  a  decision  must  be  made  at  the   time  a  class  is  instantiated.    q Singleton:  ensure  a  class  has  one  Instance,   and  provide  a  global  point  of  access  to  it.  q Abstract  Factory:  provide  an  interface  for   creating  families  of  related  or  dependent   objects  without  specifying  their  concrete   classes.  10/8/12   www.smartbiz.vn   5
6. 6. I.1.1  Abstract  Factory  q  Intent:  create  families  of  related  objects  without   specifying  subclass  names  q  Applicability:  when  clients  cannot  anticipate   groups  of  classes  to  instantiate  q  Concrete  factories  create  groups  of  strategies    10/8/12   www.smartbiz.vn   6
7. 7. I.2.  Structural  Patterns  q Structural  Patterns    prescribe  the   organization  of  classes  and  objects.  q Adapter   v   Convert  the  interface  of  a  class  into  another   interface  that  clients  expect   v   Adapter  lets  classes  work  together  that  couldn’t   otherwise  because  of  incompatible  interfaces  q Decorator   v   Extend  the  functionality  of  the  original  class  in  a   way  that  is  transparent  to  the  client  class  10/8/12   www.smartbiz.vn   7
8. 8. I.2.1.  Composite  q Intent   v treat  individual  objects  &  multiple,  recursively-­‐ composed  objects  uniformly  q Applicability   v Objects  must  be  composed  recursively,   v and  no  distinction  between  individual  &  composed   elements,   v and  objects  in  structure   can  be  treated  uniformly   10/8/12   www.smartbiz.vn   8
9. 9. I.2.1.  Composite   q CORBA  Naming  Service  example  using   CosNaming::BindingIterator  (which  is  an   example  of  the  “Batch  Iterator”  pattern   compound  from  POSA5)  Composite   Node   Leaf  Node   10/8/12   www.smartbiz.vn   9
10. 10. I.3.  Behavioral  Patterns  q Behavioral Patterns prescribe the way objects interact with each other. They help make complex behavior manageable by specifying the responsibilities of objects and the ways they communicate with each other.q State:  allow  an  object  to  alter  its  behavior   when  its  internal  state  changes  10/8/12   www.smartbiz.vn   10
11. 11. I.3.1.  Strategy  q Intent   v  deNine  a  family  of  algorithms,  encapsulate  each  one,  &   make  them   v  interchangeable  to  let  clients  &  algorithms  vary   independently  q Applicability   v  When  object  is  conNigurable  with  one  of  many  algorithms,   v  and  all  algorithms  can  be  encapsulated,   v  and  one  interface  covers  all  encapsulations     10/8/12   www.smartbiz.vn   11
12. 12. I.3.1.  Strategy  q Strategy  applied  in  distributed  (middleware)   Hook  for   marshaling   Hook  for   strategy   the  request   demuxing   Hook  for  the  event   strategy   demuxing  strategy   Hook  for  the   Hook  for  the   connection   concurrency   management   strategy   strategy   Hook  for  the   underlying   transport   strategy  10/8/12   www.smartbiz.vn   12
13. 13. I.3.1.  Oracle  Fusion  Component  OracleFusion Groupware API: A unified interface for all connectors thataccess the groupware servers via WebDAV, applying Singleton, Façade andFactory design patterns All connectors utilize the Jakarta EnterConnect Groupware API Jackrabbit (client-side) API to send requests to and receive Exchange OpenGroupware Lotus Domino responses from a variety of Connector Connector Connector WebDAV-enabled groupware servers MS Exchange 2003 Email Jakarta Slide API WebDAV Server XML HTTP request Calendar Jakarta Commons HTTP Client JDOM (JSR 102) Contact XML HTTP response Jakarta Commons Logging Java Transaction API Task Uploaded DocumentJakarta Jackrabbit re-uses some of open source libraries such as theJakarta Commons library (HTTP Client &Logging), JDOM (JSR 102), JTA, JMX, … 13
14. 14. I.3.2.  OF  Design  Pattern  q Singleton:  the  Connector  class  must  only  have  one  instance  because  it’s  wasteful  &   useless  to  keep  several  instances  on  memory.  Keeping  so  many  connectors  will  difNicult   to  manage  lifecycle  &  impact  the  performance  of  application.  q Factory  Method:  because  of  we  assume  our  API  can  be  used  against  multiple  servers   so   that   there   must   be   a   Connector   for   each   server   (i.e.   ECExchangeConnector,   ECLotusDominoConnector,   ECOpenGroupwareConnector).   Each   connector   is   responsible   for   manufacturing   its   own   managers   (MailManager,   CalendarManager,   TasksManager)  and  delegate  the  jobs  to  them.  The  connector  will  only  be  determined   by  the  client  depending  on  the  conNiguration.   v For   example,   if   a   client   (portlet)   need   to   send   a   mail   by   Exchange   server   then   it   will   call   the   ECExchangeConnector;   this   connector   then   will   use   its   localized   manager   ExchangeMailManager  to  make  life  easier.  q Abstract  Factory:  ECGroupwareFactory  is  the  abstract  factory  class  for  all  connectors   because  it  will  expose  a  uniFied  interface  for  connectors  to  client.  q Facade:   the   uniFied   interface   will   be   deFined   in   the   Façade   pattern.   As   stated   before,   a   speciNic   Connector   will   delegate   the   appropriate   jobs   to   its   subsystems   (managers).  The  façade  pattern  also  decouples  the  managers  from  the  client  and  other   managers,  thereby  promoting  independence  and  portability.   10/8/12   www.smartbiz.vn   14
15. 15. I.3.1.  OF  Design  Pattern   <<Factory Method>> «interface» TasksManager <<Abstract Factory>> ECGroupwareFactory <<Façade>> ExchangeTasksManager <<Singleton>> ECLotusDominoConnector ECOpenGroupwareConnector ECExchangeConnector ExchangeCalendarManager ExchangMailManager «interface» «interface» MailManager CalendarManager Apache Slide Class 15  10/8/12   www.smartbiz.vn
16. 16. I.4.1.  Observer  Pattern  q Easier  to  keep  a  consistent  and  maintainable   view  of  the  data   v Half  as  many  connections  between  actions,  views   v Views  are  independent  and  unaware  of  each  other   Data   Action   Action   Action   Action   Action   Action   Action   Action   View  à    View  &  Action  à     Data  notiNies  observers  via  events   View  Messages  about  the  data   when  the  state  of  the  data  changes  10/8/12   www.smartbiz.vn   16
17. 17. I.4.2.  Implementing  an  Observer   1.  Assign  the  subject  to  the   observers  (MainForm_Load)       IssueSubject         10/8/12   www.smartbiz.vn   17
18. 18. I.4.3.  Implementing  an  Observer   1.  Assign the subject to the observers (MainForm_Load) m_subject = new IssueSubject() paneA.Subject = m_subjectb paneB.Subject = m_subject ... c da IssueSubject10/8/12   www.smartbiz.vn   18
19. 19. I.4.4.  Implementing  an  Observer   1.  Assign the subject to the observers (MainForm_Load) m_subject = new IssueSubject() paneA.Subject = m_subject b paneB.Subject = m_subject ... c d 2.  Observers bind and save changes to Subject.DataSet a IssueSubject 10/8/12   www.smartbiz.vn   19
20. 20. I.4.5.  Implementing  an  Observer   1.  Assign  the  subject  to  the   observers  (MainForm_Load)   m_subject  =  new  IssueSubject()   paneA.Subject  =  m_subject   paneB.Subject  =  m_subject  ...   b     c   d   2.  Observers  bind  and  save   changes  to  Subject.DataSet   a   3.  When  data  changes,  subject     raises  DataChanged  event     IssueSubject         10/8/12   www.smartbiz.vn   20
21. 21. I.4.6.  Implementing  an  Observer   1.  Assign  the  subject  to  the   observers  (MainForm_Load)   m_subject  =  new  IssueSubject()   paneA.Subject  =  m_subject   b   paneB.Subject  =  m_subject  ...   c   d   2.  Observers  bind  and  save   changes  to  Subject.DataSet   a   3.  When  data  changes,  subject     raises  DataChanged  event         4.  Observers  handle  subject     events  to  rebind  data,  if   IssueSubject   they  care       m_subject.DataChanged  +=  …     ‘  rebind     10/8/12   www.smartbiz.vn   21
22. 22. I.5.1.  Coordinate  Command  State  q Related  menu  and  toolbar  widgets  are  not   automatically  handled  together  q Command  Pattern   v  Command  objects  unify  the  state  and  action  for  related   UI  widgets    q Example:   v “Save”  action   Ø  Menu  item   Ø  Toolbar  button   Ø  Context  menu    10/8/12   www.smartbiz.vn   22
23. 23. I.5.2.  Implementing  a  Command   1.  Create  a  Command  for  each  action  in  MainForm_Load()   action  =  new  Command.Action(this.WorkOfNline_Action)   ofNlineCommand  =  new  Command(action)   2.  Wire  menu  items  and  toolbar  buttons  to  the  Command   using  Commander  objects   MenuItemCommander.Connect(menuWorkOfNline,  ofFlineCommand  )   ToolBarButtonCommander.Connect(tlbOfNline,  ofFlineCommand  )      3.  Control  button  and  menu  item  state  through  the   Command    Disable  all  UI  widgets  connected  to  this  command   ofNlineCommand.IsEnabled  =  false   10/8/12   www.smartbiz.vn   23
24. 24. I.6.1.  Accelerated  J2EE  Development  q Architect  Service-­‐Oriented  Components   v  Design  for  Flexible,  Agile  Applications  and     Iterative  Development  q Apply/Extend  Pre-­‐Built  COTS  Components   v  Infrastructure  and  Common  Business  Components  q Generate/Extend  Custom  Components   v  Rapidly  Specify  and  Provision  New  Components  q Leverage  Industry  Development  Standards   v  Frameworks  (STRUTS,  Spring  /  Weld,  Hibernate)   v  Patterns  (MVC)   v  XML,  Best  Practices  10/8/12   www.smartbiz.vn   24
25. 25. II.  J2EE  Application  Design  Patterns   Business  Logic  &   Data  Integration   Presentation   Messaging  Fabric   &  Persistence   EJB  Container   EJB  Container   Response   8.   Execute   Business   Resources   Biz  Method   13.   Request   Service   14.   CRUD   Facade   10b.   Data   Data   Query   9.   Get/Set   Access   Database   <<de-­‐serialize>>  Web  Server  &   18.  Response   Assemble   Remove   Object   Serialized   RMI   JMS  Servlet  Engine   Value   Data   Directory   Object   16.  Forward   JSF,  JSP   JMS   (Taglibs)   <<creates>>   Value   12.   1.  Request   Object   Get/Set   17.  State?   Assembler   <<uses>>   Action   Remove   Data   Servlet   10a.   Access   <<uses>>   Invoke   Proxy   4.  Dispatch   2.  Fill  State   Data   XML   11.   Entity   Connection   3.  Validate   CRUD   Connection   Action  to   Bean     Pool   (Stateless)   Data   Factory   Form    &   Value   Session   Forward   <<uses>>   Object   Bean   Mappings   Action   Form   5.  Get  Delegate   Action   Object   15.  Fill  State   <<uses>>   Key   Other  Infrastructure   6.  Execute     <<supplies>>   RMI   Business   Business   <<uses>>   EJB  Container Value   Model   Delegate   Delegate   M   JMS   Object   Pattern   Factory   Adapter   7a.   Event   Security   Messaging   Event   Execute   View   Interceptor   Management   Command   NotiFication   Biz  Method   Business   V   Pattern   Framework   Framework   Factory   Framework   Service   Proxy   <<uses>>   Controller   C   Pattern   <<uses>>   7b.  CRUD  Data   Data   Message  Q   Filter   Service   Access   &  Channel   Other   Interceptors   Activation   Proxy   To  DAO   P   Pattern   Locator   10/8/12   www.smartbiz.vn   25
26. 26. II.  Model-­‐View-­‐Controller  –  Passive   :Controller :Model :View handleEvent service update getData10/8/12   www.smartbiz.vn   26
27. 27. II.  Model-­‐View-­‐Controller  –  Active   Controller <<interface>> Observer Model +update() View :Model :View handleEvent notify update getData data10/8/12   www.smartbiz.vn   27
28. 28. II.1.  Presentation  Tier  Patterns  q Intercepting  Filter  q Front  Controller:  Use  of  a  Controller  to   handle  all  web  requests  q Dispatcher  View:  The  controller  works  with   a  Dispatcher  that  handles  navigation  q Composite  View  q View  Helper  q Service  to  Worker  10/8/12   www.smartbiz.vn   28
29. 29. II.2.  Intercepting  Filter  q Problems:   v Verify  authentication   v Check  type  of  client   v Is  the  session  valid?      q Solution:  implement  one  or  more   Intercepting  Nilters.  q Sample:  Filter-­‐servlet  -­‐  can  transparently   pre  and  post  process  all  requests  in  an   unlimited  length  Nilter  chain.  10/8/12   www.smartbiz.vn   29
30. 30. II.3.  Front  Controller  &  Dispatcher  q Front  Controller  -­‐  Problems:   v Multiple  views.   v Navigation  logic  embedded  in  views.   v DifNicult  to  maintain  view  logic  as  application   grows.   v Want  to  reuse  view  navigation  within  multiple   clients.  10/8/12   www.smartbiz.vn   30
31. 31. II.3.  Front  Controller  -­‐  Dispatcher  Client FrontController Dispatcher Vi ew Helper 1: Send Request 1.1: Delegate Request 1.1.1: Forward Request 2: Send Request 2.1: Forward Request 3: Send Request 3.1: Process Request10/8/12   www.smartbiz.vn   31
32. 32. III.4.  View  Helper  q Problem:   v Many  common  operations  in  each  view.   v Often  view  output  is  dependant  on  business   data.   v Create  more  cohesive  look  and  feel.   v Like  to  hand  off  UI  to  web  design  group  q Solution:   v Basically  anything  that  helps  you  display  your   model  data  in  a  reusable  fashion.   v Implemented  as  JavaBeans,  Tags  or  XML/XSLT  q See  the  JSTL,  JSF  and  Struts.  10/8/12   www.smartbiz.vn   32
33. 33. II.5.  Composite  View   q Problem:   v Many  common  &  recurring  components  in  a  view.     v Fragments  may  display  differently  given  user  type   (i.e.  faculty,  student).   v Look  and  feel  in  Nlux  needs  to  be  easily   updateable.   q Differs  from  View  Helper  as  the  Composite   View  decides  which  sub-­‐views  to  include   while  View  Helper  generally  decides  how  to   display  a  given  sub-­‐view.  10/8/12   www.smartbiz.vn   33
34. 34. III.  Business  Tier  Patterns  q  Business  Delegate:  use  of  a  Business  Delegate  to   hide  Implementation  Detail.  q Session  Façade  q Service  Locator  q Value  Object  q Composite  Entity  q Value  Object  Assembler  q Value  List  Handler  10/8/12   www.smartbiz.vn   34
35. 35. III.1.  Business  Delegate   Cl ient BusinessService 1: Creat e wi th ID BusinessDelegate 1.1: Convert ID to Handle Handle 1.1.1: Create 2: Invoke 2.1: Get Business Service 2.1. 1: Connect 2.1. 2: Return Business Service 2.2: Invoke10/8/12   www.smartbiz.vn   35
36. 36. III.2.  Session  Façade  Pattern  q Problem:   v Need  very  loose  coupling  between  client  and   ever  changing  business  tier.   v Data  tier  changing;  need  to  improve  reuse.  q Session  Bean     EJB Tier provides  uniNied  approach  q Façade  hides     Entity  Bean  Details   Session Client Entity Bean Bean10/8/12   www.smartbiz.vn   36
37. 37. IV.  Integration  q Data  Access  Object  q Service  Activator  q User  WorkNlow  Integrator  10/8/12   www.smartbiz.vn   37
38. 38. IV.1.  Data  Access  Object   Business DataAcces accesses Object sObject inherits inherits inherits Oracle Sybase DB2DAO DAO DAO adapts adapts adapts Oracle DB2 Sybase Database Database Database10/8/12   www.smartbiz.vn   38
39. 39. 10/8/12   www.smartbiz.vn   39