Designing Classes and Programs
Download
Report
Transcript Designing Classes and Programs
XML and XSLT and other TLA's
XML: more than buzzword compliancy
Future of information interchange
Leverages Internet and TCP/IP
Readable by humans and machines
What does XSLT do?
Transform XML to: HTML, XML, …
Programming Language
How to use these?
With Java (or other languages)
With IE
Software Design
8.1
Design Patterns: Abstract Factory
Provide an interface for creating families of related or
dependent objects without specifying their concrete
classes.
System is independent of how its products are created,
composed, represented
Configured with one of multiple families of products
Family of products designed to be used together, this
constraint should be enforced
Provide library, but reveal interfaces, not
implementations
Makes exchanging product families simple
Supporting new products is difficult
Software Design
8.2
Design Patterns: Decorator
Attach additional responsibilities to an object
dynamically. Decorators provide an alternative to
subclassing
Add responsibilities dynamically and transparently,
without affecting other objects
Withdraw responsibilities
Extension by subclass might be impractical
Decorate a component so that the component and the
decorator (container) share the same interface
Lots of little objects that look alike, easy to customize
if you understand, but hard to learn
Software Design
8.3
How Java works
The java compiler takes a .java file and generates a .class file
The .class file contains Java bytecodes, the assembler
language for Java programs
Bytecodes are executed in a JVM (java virtual machine),
the valid bytecodes are specified by Sun
• What if third parties create platform/OS specific codes?
IBM has Jikes, a lightning fast compiler, others?
The JVM interprets the bytecodes
JVM is platform/OS specific, must ultimately run the code
Different JVMs will have different performance
Sun has HotSpot, previously the big thing was JITs
• Adaptive compilation vs. compile when executed
Software Design
8.4
From JITs to Deoptimization
JITs compile bytecodes when first executed
If we can cache translated code we can avoid re-translating
the same bytecode sequence
Spend time compiling things that aren’t frequently
executed (optimistic optimization?)
Errors indicate “compiled code” rather than line number
Sun’s HotSpot VM uses a different strategy for performance
Adaptive compilation: save time over JIT, compile
“hotspots” rather than everything, uses less memory, starts
program faster, …
No method inlining, but uses dynamic deoptimization
• Program loads new subclass, compiled code invalid, so …?
What does the class loader do?
Software Design
8.5
Loading .class files
The bytecode verifier “proves theorems” about the bytecodes
being loaded into the JVM
These bytecodes may come from a non-Java source, e.g.,
compile Ada into bytecodes (why?)
This verification is a static analysis of properties such as:
.class file format (including magic number 0xCAFEBABE)
Methods/instances used properly, parameters correct
Stack doesn’t underflow/overflow
…
Verification is done by the JVM, not changeable
Contrast ClassLoader, which is changeable, can modify
classes before they’re loaded into the JVM
http://securingjava.com
http://java.sun.com/sfaq/verifier.html
Software Design
8.6
The ClassLoader
The “boot strap” loader is built-in to the JVM
Sometimes called the “default” loader, but it’s not
extensible or customizable the way other loaders are
Loads classes from the platform on which the JVM runs
(what are loader and JVM written in?)
Applet class loader, RMI class loader, user loaders
Load .class files from URLs, from other areas of platform
on which JVM runs
A class knows how it was loaded and new instances will
use the same loader
Why implement a custom loader?
Work at Duke with JOIE
Software Design
8.7
Applications and Applets
An applet is a program delivered via the web
security issues, sandbox model
where does code/images/etc come from? How is it
delivered?
what browsers support JDK 2.0 out-of-the box?
Use IE/Netscape with plugin, use Opera as is, use
appletviewer for debugging, testing
Possible to wrap up lots of classes in a .jar file
java archive, similar to a tar file, possible to include
.class files, .au, .gif, etc, so all code transferred at once
Software Design
8.8
CGI: Common Gateway Interface
http://hoohoo.ncsa.uiuc.edu/cgi/overview.html
How do programs running on the server communicate
with clients over the web?
Can do simple UI stuff in HTML: textfield, buttons,
radio buttons, select/choicebox, checkbox, …
Communicate choices to client program via
“parameters”
The communication is done by encoding text in a string
that’s passed to program running on server
Server program can be written in Perl, C, C++, Java, …
Two forms of communication: get and post
• Post is more robust, no limit on size of string sent
Software Design
8.9
Setting up CGI programs
http://www.oit.duke.edu/sa/cgi/
Request a CGI key
Create a directory and give cgi.ola rliw permission
Put code in the directory, html pages can live anywhere
Debugging is tricky, probably want to debug from the
command-line rather than over the web
Be sure to send appropriate header to start HTML page from
server program when sending to client/browser
Missing header causes “internal server” or other error
See code examples and HTML sample for details
Server must parse arguments
Replace “+” with “ “, look for %xx args, replace them
Software Design
8.10