this lecture - cornell universitythis lecture e gamma, r. helm, r. johnson and j. vlissides, design...
TRANSCRIPT
![Page 1: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/1.jpg)
This Lecture
E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.
M. Fowler, Patterns of Enterprise Application Architecture. Pearson, 2003.
M. Hall and L. Brown, Core Servlets and Java Server Pages. Prentice-Hall 2004.
![Page 2: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/2.jpg)
Some Patterns
• Presentation Layer
• Model View Controller
• Controller
• Page Controller
• Front Controller
• View
• Template View
• Transform View
![Page 3: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/3.jpg)
Revenue Recognition
• Revenue Recognition Problem
Product Contract Rev Recog
1 * 1 * amountdate
date signedrevenue
type (W,D,S)
Rules for different product types:
W: all right away S: 1/3 now, 1/3 60 days, 1/3 90 days D: 1/3 now, 1/3 30 days, 1/3 60 days
![Page 4: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/4.jpg)
Domain Model
An object model of the domain that incorporates both behavior and data
Contract
recognizedrevenuecomputeRecogs
Product
computeRecogs
Strategy
recognizedrevenuecomputeRecogs
CompleteStrategy
recognizedrevenuecomputeRecogs
![Page 5: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/5.jpg)
Original Design ...
Contract
recogRevenuecalcRecogs
Product
calcRecogs(c)
Recog Strategy
calcRecogs(c)
CompleteStrat
calcRecogs(c)
3WayStrat
calcRecogs(c)
RevRecog
recogBy(d)
amountdate
*
1
*1
* 1
![Page 6: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/6.jpg)
Problem
• The rules are made up by accountants and lawyers ...
• They might change (retroactively) for non-technical reasons
• Computing and inserting recognitions in database when contract is signed won’t work!
![Page 7: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/7.jpg)
New Design ...
• Calculate recognized revenue dynamically
Contract
recogRevenue(d)
Product
recogRevenue(c)
Recog Strategy
recogRevenue(c)
CompleteStrat
recogRevenue(c)
3WayStrat
recogRevenue(c)
*1
* 1
![Page 8: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/8.jpg)
How a Strategy Calculates Recognized Revenue
• Delegate old revenue to previous strategy
![Page 9: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/9.jpg)
Remarks
• This works b/c strategy is an object not a value ... so as law changes the strategy can be updated “in place”
• What if new law changes strategy for some products but not others?
• A more elaborate scheme might cache previously computed recognitions for performance
![Page 10: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/10.jpg)
Caching Design ...
Contract
recogRevenue
Product
recogRev(c)
Recog Strategy
recogRev(c)
CompleteStrat
recogRev(c)
3WayStrat
recogRev(c)
RevRecogamountdate
*
1
*1
* 1
![Page 11: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/11.jpg)
Table Module
A single instance that handles the business logic for all rows in a database table
Contract
computeRecogs
Product
getProductType
Rev Recog
insert(id, amt, date)regdRev(id, date)
DB1
![Page 12: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/12.jpg)
Table Module - II
• Works well only for simple hierarchical schemas
• Same problem (early binding) as the Domain Model solution above
• similar solution
• but “caching” interpretation of recognition table is problematic
![Page 13: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/13.jpg)
Presentation
• Model-View-Controller
• Dates back to SmallTalk in 1970s
• [HB04] M. Hall and L. Brown, Core Servlets and Java Server Pages. Prentice-Hall 2004.
![Page 14: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/14.jpg)
Model-View-Controller
• Separate the presentation from the model
• Separate the controller from the view
View Controller
Model
![Page 15: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/15.jpg)
Model-View-Controller
• Separate the presentation from the model
• presentation depends on model, but model is independent of presentation
• application may have multiple presentations for the same model
• or even multiple presentations on screen simultaneously
• Observer pattern!
![Page 16: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/16.jpg)
Model-View-Controller
• Separate the controller from the view
• clean ...
• may share a view among controllers
• editable vs readonly
• the Servlet vs JSP distinction ...
![Page 17: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/17.jpg)
Model-View-Controller
Web Svr Ctlr HTTP Req
Model Objects
View
Req
Reply
Fwd to View
Get data
Get data
Create with data
Execute domain logic
Add data for view
Fwd to View
Generate reply
![Page 18: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/18.jpg)
Model-View-Controller in J2EE
• Controller is a servlet
• may immediately invoke a session bean
• Model is (session and entity) EJBs
• Data for view is entity beans
• stored in accessible place by controller
• View is a JSP
• controller forwards to view
![Page 19: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/19.jpg)
Implementation in J2EE
• Servlet handles request
• Servlet invokes business logic (EJBs) to obtain results (entity EJBs)
• Put results in view-accessible location (request, session or servlet context)
• Forward request to view (a JSP page)
• Extract data from beans and display
![Page 20: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/20.jpg)
Example - Request Based Data
![Page 21: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/21.jpg)
Example - Session Based Data
![Page 22: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/22.jpg)
Example - Application Based Data
![Page 23: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/23.jpg)
Page Controller
• An object that handles a request for a specific page or action on a Web site
• Client URL directly reaches page controller
Model
PageController
View
![Page 24: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/24.jpg)
Front Controller
• A single controller handles all requests for a Web site
• Passes request to appropriate command controller
Controller
doGetdoPost
Abstract Command
process
Concrete Command
process(...)
Concrete Command
process(...)
![Page 25: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/25.jpg)
Application Controller
• A centralized point for handling screen navigation and the flow of an application
• “Wizard” style of interaction
• Treat application UI as a state machine
![Page 26: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/26.jpg)
Template View
• Render information into HTML by embedding syntactic markers in an HTML page
• Reminds you of JSP, doesn’t it!
![Page 27: This Lecture - Cornell UniversityThis Lecture E Gamma, R. Helm, R. Johnson and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. M](https://reader033.vdocuments.site/reader033/viewer/2022050519/5fa311e544f5fd18752172d3/html5/thumbnails/27.jpg)
Transform View
• A view that processes domain data element-by-element and transforms it into HTML
• Contrast:
• A template view is organized around the output page
• A transform view is organized around the data elements
• Dominant example:
• data is XML (or a data transfer object that can serialize itself into XML)
• transformations are XSLT