Professional Documents
Culture Documents
2XB3 - Week - 8 - Searching - ArchitecturalPattern
2XB3 - Week - 8 - Searching - ArchitecturalPattern
2XB3 - Week - 8 - Searching - ArchitecturalPattern
Lecture 7
– Applica6on of Search Algorithms
– So=ware Design PaAerns
Dr. Reza Samavi
3
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 3
Symbol tables
Announcement
Key-value pair abstraction.
・ Insert a value with specified key.
• Next week lecture on
・Given a key, search for the corresponding value.
Ex. DNS lookup.
・Insert domain name with specified IP address.
・Given domain name, find corresponding IP address.
domain name IP address
www.cs.princeton.edu 128.112.136.11
www.princeton.edu 128.112.128.15
www.yale.edu 130.132.143.21
www.harvard.edu 128.103.060.55
www.simpsons.com 209.052.165.60
key value
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 4 !X
Symbol table applications
web search find relevant web pages keyword list of page names
hasNiceSyntaxForAssociativeArrays["Python"] = true
hasNiceSyntaxForAssociativeArrays["Java"] = false
void put(Key key, Value val) put key-value pair into the table a[key] = val;
void delete(Key key) remove key (and its value) from table
Intended consequences.
・Easy to implement contains().
public boolean contains(Key key)
{ return get(key) != null; }
built-in to Java
(stay tuned)
do x and y refer to
the same object?
Default implementation. (x == y)
Customized implementations. Integer, Double, String, java.io.File, …
User-defined implementations. Some care needed.
P 10 C 4
ST test client for analysis
% more tinyTale.txt
it was the best of times
it was the worst of times
it was the age of wisdom
it was the age of foolishness
it was the epoch of belief
it was the epoch of incredulity
it was the season of light
it was the season of darkness
it was the spring of hope
it was the winter of despair tiny example
(60 words, 20 distinct)
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 15 "X
© J. Christopher Beck 21
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 21
Like Engineers
© J. Christopher Beck 22
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 22
Outline
§ Applica6on of Search Algorithms
Instructor
sees uses
updates changes
List of Marks
© J. Christopher Beck 28
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 28
MVC Diagram
User
sees uses
updates changes
Model
29
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 29
MVC in the JSP/Servlet world
• Model (Java code) Holds the real business logic and the state of an
applica6on
• View (JSP) Responsible for the presenta6on.
• Controller (Servlet) Takes the user input from the request and figures
out what it means to the model
User
sees uses
updates manipulates
Model
sees uses
HTML Pages +
View View View Controller
Java classes
updates manipulates
Model
Java classes
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 31
Outline
§ Applica6on of Search Algorithms
sees uses
changes
updates
Java App:
Beer Expert
Java classes
sees uses
result: Servlet:
Brands Beer Select
changes
updates
Java App:
Beer Expert
sees uses
2. html page view
result: Servlet:
Brands Beer Select
changes
updates
Java App:
Beer Expert
<html>
<head>
<title>Beer Selection</title>
</head>
<body>
<h1 align="center">Beer Selection Page</h1>
</body>
</html>
sees uses
2. html page view
result: Servlet:
Brands Beer Select
3. The BeerSelect Controller
(Java)
changes
updates
Java App:
Beer Expert
package com.example.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
String c = request.getParameter("color");
request.setAttribute("styles", result);
RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}
sees uses
1. html page
4. The result view (JSP)
result: Servlet:
Brands Beer Select
3. The BeerSelect Controller
(Java)
changes
updates
Java App:
Beer Expert
<html>
<body>
<h1 align="center">Beer Recommendations JSP</h1>
<p>
<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while (it.hasNext()) {
out.print("<br>TRY: " + it.next());
}
%>
</body>
</html>
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 50
MVC
sees uses
1. html page
4. The result view (JSP)
3. The BeerSelect Controller
result: (Java) Servlet:
Brands Beer Select
changes
updates
Java App:
Beer Expert
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>Beer</servlet-name>
<servlet-class>
com.example.web.BeerSelect
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
Workspace
src
com
example
web
BeerSelect.java
model
BeerExpert.java
form.html
result.jsp
web.xml
bin
com
example
web
BeerSelect.class
model
BeerExpert.class