Case study Java ECommerce Martin Cooke 2003 Todays

  • Slides: 50
Download presentation
Case study Java E-Commerce © Martin Cooke, 2003

Case study Java E-Commerce © Martin Cooke, 2003

Today’s lecture • Case study, illustrating design patterns • Open-source frameworks 2/24/2021 Java E-Commerce

Today’s lecture • Case study, illustrating design patterns • Open-source frameworks 2/24/2021 Java E-Commerce © Martin Cooke, 2003 2

Case study Java E-Commerce © Martin Cooke, 2003

Case study Java E-Commerce © Martin Cooke, 2003

Phonetic transcription 2/24/2021 Java E-Commerce © Martin Cooke, 2003 4

Phonetic transcription 2/24/2021 Java E-Commerce © Martin Cooke, 2003 4

Quick tour 2/24/2021 Java E-Commerce © Martin Cooke, 2003 5

Quick tour 2/24/2021 Java E-Commerce © Martin Cooke, 2003 5

Some stats • • • 95 Java classes 27 dynamic screens (XMLC) 15 static

Some stats • • • 95 Java classes 27 dynamic screens (XMLC) 15 static screens 2 CSS stylesheets 13 images 2 XML files per language/accent combination, plus 2 DTDs • 6 RDB tables, totalling 70 fields 2/24/2021 Java E-Commerce © Martin Cooke, 2003 6

Architecture of WTT servlet controller process event 1 forward process event 2 XML forward

Architecture of WTT servlet controller process event 1 forward process event 2 XML forward process event 3 forward view 2/24/2021 model Java E-Commerce © Martin Cooke, 2003 database 7

Architecture of WTT Data servlet controller Screen flows process event 1 forward process event

Architecture of WTT Data servlet controller Screen flows process event 1 forward process event 2 Keyboard defn forward process event 3 XML forward view 2/24/2021 model Java E-Commerce © Martin Cooke, 2003 database Transcriptions Attempts etc 8

Architecture of WTT Presentation servlet controller process event 1 forward process event 2 XML

Architecture of WTT Presentation servlet controller process event 1 forward process event 2 XML forward process event 3 forward view XMLC screens 2/24/2021 model Java E-Commerce © Martin Cooke, 2003 database 9

Architecture of WTT Client Applets Serialised objects servlet controller HTTP POST process event 1

Architecture of WTT Client Applets Serialised objects servlet controller HTTP POST process event 1 forward process HTML event 2 XML forward process event 3 forward view 2/24/2021 model Java E-Commerce © Martin Cooke, 2003 database 10

Architecture of WTT Technologies & tools Applets Servlets in Tomcat servlet JNDI controller JDOM

Architecture of WTT Technologies & tools Applets Servlets in Tomcat servlet JNDI controller JDOM process event 1 forward process HTML event 2 CSS XML forward process event 3 forward XEmacs view model XMLC Ant 2/24/2021 Java E-Commerce © Martin Cooke, 2003 database My. SQL 11

Design pattern 1: Front Controller aim Centralise view management into a single object which

Design pattern 1: Front Controller aim Centralise view management into a single object which handles all client request motivation Avoid repeating code (maintenance nightmare) for common tasks eg security, exception-handling, logging, interpretation of user requests, template application, … drawbacks Complexity transferred to front controller pattern type Mediator (colleagues are its views) implementation servlet often used with Request-dispatcher 2/24/2021 Java E-Commerce © Martin Cooke, 2003 12

Design pattern 2: Request-dispatcher aim motivation Dispatches requests to appropriate handlers implementation Subclass of

Design pattern 2: Request-dispatcher aim motivation Dispatches requests to appropriate handlers implementation Subclass of Event. Handler class Often interacts with a screen flow manager often used with Front controller 2/24/2021 Remove request handling code from front controller Java E-Commerce © Martin Cooke, 2003 13

Front controller initialisation 2/24/2021 Java E-Commerce © Martin Cooke, 2003 14

Front controller initialisation 2/24/2021 Java E-Commerce © Martin Cooke, 2003 14

Front controller event handling 2/24/2021 Java E-Commerce © Martin Cooke, 2003 15

Front controller event handling 2/24/2021 Java E-Commerce © Martin Cooke, 2003 15

Screen flow manager • Reads XML file and builds event to screen mapping •

Screen flow manager • Reads XML file and builds event to screen mapping • Validates screens e. g. ensures that key screens are defined 2/24/2021 Java E-Commerce © Martin Cooke, 2003 16

Event Handler base class 2/24/2021 Java E-Commerce © Martin Cooke, 2003 17

Event Handler base class 2/24/2021 Java E-Commerce © Martin Cooke, 2003 17

Event Handler subclass 2/24/2021 Java E-Commerce © Martin Cooke, 2003 18

Event Handler subclass 2/24/2021 Java E-Commerce © Martin Cooke, 2003 18

2/24/2021 Java E-Commerce © Martin Cooke, 2003 19

2/24/2021 Java E-Commerce © Martin Cooke, 2003 19

2/24/2021 Java E-Commerce © Martin Cooke, 2003 20

2/24/2021 Java E-Commerce © Martin Cooke, 2003 20

2/24/2021 Java E-Commerce © Martin Cooke, 2003 21

2/24/2021 Java E-Commerce © Martin Cooke, 2003 21

Design pattern 3: Data access object aim Decouple business logic from data access logic

Design pattern 3: Data access object aim Decouple business logic from data access logic so that the data resource can change independently motivation Persistent storage may be RDB, XML, other file structure Hide form of persistent storage from business logic Easier code maintenance Can select type of data source at deployment time drawbacks Adds a layer of indirection to data access Have to deal with different transactional behaviours pattern type Bridge and Adapter implementation Data. Access. Object abstract superclass Implementation subclasses for each type of data source (not necessary when only one type of source) 2/24/2021 Java E-Commerce © Martin Cooke, 2003 22

Data access object • JDBC cannot guarantee complete interchangeability of database (eg legacy code)

Data access object • JDBC cannot guarantee complete interchangeability of database (eg legacy code) • Later extensions to object databases are easily catered for • Can be used to manage connections • Reduces code complexity in business logic • Better to use container-managed persistence (see later Enterprise Java Beans) 2/24/2021 Java E-Commerce © Martin Cooke, 2003 23

Data access object 2/24/2021 Java E-Commerce © Martin Cooke, 2003 24

Data access object 2/24/2021 Java E-Commerce © Martin Cooke, 2003 24

Data access object In front controller servlet 2/24/2021 Java E-Commerce © Martin Cooke, 2003

Data access object In front controller servlet 2/24/2021 Java E-Commerce © Martin Cooke, 2003 25

Data access object 2/24/2021 Java E-Commerce © Martin Cooke, 2003 26

Data access object 2/24/2021 Java E-Commerce © Martin Cooke, 2003 26

Session-binding 2/24/2021 Java E-Commerce © Martin Cooke, 2003 27

Session-binding 2/24/2021 Java E-Commerce © Martin Cooke, 2003 27

Open source frameworks Java E-Commerce © Martin Cooke, 2003

Open source frameworks Java E-Commerce © Martin Cooke, 2003

Two examples • Struts • Barracuda 2/24/2021 Java E-Commerce © Martin Cooke, 2003 29

Two examples • Struts • Barracuda 2/24/2021 Java E-Commerce © Martin Cooke, 2003 29

The Struts Framework • Open source framework for 3 tier applications • Based around

The Struts Framework • Open source framework for 3 tier applications • Based around JSP, servlets and tags • Model II architecture 2/24/2021 Java E-Commerce © Martin Cooke, 2003 30

‘Type II’ JSP solution request servlet controller Forward event response client 2/24/2021 bean JSP

‘Type II’ JSP solution request servlet controller Forward event response client 2/24/2021 bean JSP view State change event data model web Java E-Commerce © Martin Cooke, 2003 database data 31

Benefits of Struts • Enforces separation of business, control and presentation logic • Developer

Benefits of Struts • Enforces separation of business, control and presentation logic • Developer can focus on their application without rebuilding the infrastructure each time • Support for: – XML parsing – Form population – I 18 N • Extensive JSP tag libraries 2/24/2021 Java E-Commerce © Martin Cooke, 2003 32

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view)

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view) Action (model’s business logic) <<use>> taglib (view) Action. Mapping Adapted from Professional JSP, p 781 2/24/2021 Java E-Commerce © Martin Cooke, 2003 33

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view)

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view) <<use>> Action (model’s business logic) Action. Servlet • Controller, acting as a request dispatcher • Configured using an XML file <<use>> taglib (view) Action. Mapping Adapted from Professional JSP, p 781 2/24/2021 Java E-Commerce © Martin Cooke, 2003 34

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view)

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view) <<use>> Action (model’s business logic) Action. Mapping • Maps URL pattern to business logic component (Action) • Defines specific forward targets based on business logic <<use>> taglib (view) Action. Mapping Adapted from Professional JSP, p 781 2/24/2021 Java E-Commerce © Martin Cooke, 2003 35

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view)

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view) <<use>> Action (model’s business logic) <<use>> taglib (view) Action. Form • Java Bean representing input from a view component • Bean poperties are automatically populated • Has validate() method Action. Mapping – Eg form validation Adapted from Professional JSP, p 781 2/24/2021 Java E-Commerce © Martin Cooke, 2003 36

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view)

Main Struts components <<instantiate>> Action. Servlet Action. Form (model’s data) <<call>> <<send>> JSP (view) <<use>> Action (model’s business logic) Action • Business logic • Must be thread-safe because controller shares same instance for multiple requests <<use>> taglib (view) Action. Mapping Adapted from Professional JSP, p 781 2/24/2021 Java E-Commerce © Martin Cooke, 2003 37

Struts config fragment 2/24/2021 Java E-Commerce © Martin Cooke, 2003 38

Struts config fragment 2/24/2021 Java E-Commerce © Martin Cooke, 2003 38

Struts config fragment 2/24/2021 Java E-Commerce © Martin Cooke, 2003 39

Struts config fragment 2/24/2021 Java E-Commerce © Martin Cooke, 2003 39

Barracuda • • Another model II event-dispatching framework Based around servlets and XMLC, no

Barracuda • • Another model II event-dispatching framework Based around servlets and XMLC, no JSPs in sight Event model closer to Swing Easy localisation Form-mapping and validation component Well-documented Open source, not as mature as Struts 2/24/2021 Java E-Commerce © Martin Cooke, 2003 40

Events in webapps call URLs Controller? Show. Users&group. Id=23 Controller? Show. Trans&group. Id=23 Controller?

Events in webapps call URLs Controller? Show. Users&group. Id=23 Controller? Show. Trans&group. Id=23 Controller? Analyse. Group&group. Id=23 Controller? Delete. Group&group. Id=23 Controller? Tutor. Home But wouldn’t it be nicer if they acted like UI widgets and called routines directly? 2/24/2021 Java E-Commerce © Martin Cooke, 2003 41

Events in Barracuda are bound to Actions Action Event Listener Bound in server-side code

Events in Barracuda are bound to Actions Action Event Listener Bound in server-side code • Barracuda handles all the innards of connecting client side events to event listeners • Can define actions for <A>, <FORM>, <INPUT>, <SELECT> & <BUTTON> elements 2/24/2021 Java E-Commerce © Martin Cooke, 2003 42

Component-binding 1. 2. 3. 4. load DOM template create component hierarchy and bind it

Component-binding 1. 2. 3. 4. load DOM template create component hierarchy and bind it to DOM template Render component hierarchy; invokes render on all the child components. BText component renders its text value. Source: Barracuda documentation 2/24/2021 Java E-Commerce © Martin Cooke, 2003 43

Component-binding 5. 7. 2/24/2021 Java E-Commerce © Martin Cooke, 2003 BList renders by querying

Component-binding 5. 7. 2/24/2021 Java E-Commerce © Martin Cooke, 2003 BList renders by querying its model to determine how many items it contains, and adds them in as new items to the list. Finally, we render our updated DOM template to generate the client view and return it in response to the original HTTP request. 44

2/24/2021 Java E-Commerce © Martin Cooke, 2003 45

2/24/2021 Java E-Commerce © Martin Cooke, 2003 45

2/24/2021 Java E-Commerce © Martin Cooke, 2003 46

2/24/2021 Java E-Commerce © Martin Cooke, 2003 46

2/24/2021 Java E-Commerce © Martin Cooke, 2003 47

2/24/2021 Java E-Commerce © Martin Cooke, 2003 47

Summary Frameworks: + don’t reinvent wheel - learning curve - tie-in, to some extent

Summary Frameworks: + don’t reinvent wheel - learning curve - tie-in, to some extent - have to adopt someone else’s architectural preferences 2/24/2021 Java E-Commerce © Martin Cooke, 2003 48

Resources Java E-Commerce © Martin Cooke, 2003

Resources Java E-Commerce © Martin Cooke, 2003

Book • Professional JSP contains information on many of the topics in this lecture

Book • Professional JSP contains information on many of the topics in this lecture (albeit somewhat repeated in different chapters). Chapter 21 contains a detailed example which employs the Struts framework. 2/24/2021 Java E-Commerce © Martin Cooke, 2003 50