Jacques Bosch

Ranch Hand
+ Follow
since Dec 18, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Jacques Bosch

Hi there Jay.

Check these out. They should help.

http://www.vico.org/pages/PatronsDisseny/Pattern%20Adapter%20Class/
http://home.earthlink.net/~huston2/dp/adapter.html
http://www.dofactory.com/patterns/PatternAdapter.aspx

"Design Patterns" in 2.5 Hours:
http://www.cs.nmsu.edu/~jeffery/courses/579/patterns.html

And read the below short tutorial: (Sorry about the formatting, it was copied from PDF).
---------------------------------------------------

Design Patterns In Java Bob Tarr
The
Adapter
Pattern
Bob Tarr Design Patterns In Java The Adapter Pattern
2
The Adapter Pattern The Adapter Pattern
l Intent
� Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.
l Also Known As
� Wrapper
l Motivation
� Sometimes a toolkit or class library can not be used because its interface is
incompatible with the interface required by an application
� We can not change the library interface, since we may not have its source
code
� Even if we did have the source code, we probably should not change the
library for each domain-specific application
Bob Tarr Design Patterns In Java The Adapter Pattern
3
The Adapter Pattern The Adapter Pattern
l Motivation
� Example:
Bob Tarr Design Patterns In Java The Adapter Pattern
4
The Adapter Pattern The Adapter Pattern
l Structure
� A class adapter uses multiple inheritance to adapt one interface to another:
Bob Tarr Design Patterns In Java The Adapter Pattern
5
The Adapter Pattern The Adapter Pattern
l Structure
� An object adapter relies on object composition:
Bob Tarr Design Patterns In Java The Adapter Pattern
6
The Adapter Pattern The Adapter Pattern
l Applicability
Use the Adapter pattern when
� You want to use an existing class, and its interface does not match the one
you need
� You want to create a reusable class that cooperates with unrelated classes
with incompatible interfaces
l Implementation Issues
� How much adapting should be done?
� Simple interface conversion that just changes operation names and order of
arguments
� Totally different set of operations
� Does the adapter provide two-way transparency?
� A two-way adapter supports both the Target and the Adaptee interface. It
allows an adapted object (Adapter) to appear as an Adaptee object or a Target
object
Bob Tarr Design Patterns In Java The Adapter Pattern
7
Adapter Pattern Example 1 Adapter Pattern Example 1
l The classic round pegs and square pegs!
l Here's the SquarePeg class:
/**
* The SquarePeg class.
* This is the Target class.
*/
public class SquarePeg {
public void insert(String str) {
System.out.println("SquarePeg insert(): " + str);
}
}
Bob Tarr Design Patterns In Java The Adapter Pattern
8
Adapter Pattern Example 1 (Continued) Adapter Pattern Example 1 (Continued)
l And the RoundPeg class:
/**
* The RoundPeg class.
* This is the Adaptee class.
*/
public class RoundPeg {
public void insertIntoHole(String msg) {
System.out.println("RoundPeg insertIntoHole(): " + msg);
}
}
l If a client only understands the SquarePeg interface for inserting
pegs using the insert() method, how can it insert round pegs? A
peg adapter!
Bob Tarr Design Patterns In Java The Adapter Pattern
9
Adapter Pattern Example 1 (Continued) Adapter Pattern Example 1 (Continued)
l Here is the PegAdapter class:
/**
* The PegAdapter class.
* This is the Adapter class.
* It adapts a RoundPeg to a SquarePeg.
* Its interface is that of a SquarePeg.
*/
public class PegAdapter extends SquarePeg {
private RoundPeg roundPeg;
public PegAdapter(RoundPeg peg) {this.roundPeg = peg;}
public void insert(String str) {roundPeg.insertIntoHole(str);}
}
Bob Tarr Design Patterns In Java The Adapter Pattern
10 10
Adapter Pattern Example 1 (Continued) Adapter Pattern Example 1 (Continued)
l Typical client program:
// Test program for Pegs.
public class TestPegs {
public static void main(String args[]) {
// Create some pegs.
RoundPeg roundPeg = new RoundPeg();
SquarePeg squarePeg = new SquarePeg();
// Do an insert using the square peg.
squarePeg.insert("Inserting square peg...");
Bob Tarr Design Patterns In Java The Adapter Pattern
11 11
Adapter Pattern Example 1 (Continued) Adapter Pattern Example 1 (Continued)
// Now we'd like to do an insert using the round peg.
// But this client only understands the insert()
// method of pegs, not a insertIntoHole() method.
// The solution: create an adapter that adapts
// a square peg to a round peg!
PegAdapter adapter = new PegAdapter(roundPeg);
adapter.insert("Inserting round peg...");
}
}
l Client program output:
SquarePeg insert(): Inserting square peg...
RoundPeg insertIntoHole(): Inserting round peg...
Bob Tarr Design Patterns In Java The Adapter Pattern
12 12
Adapter Pattern Example 2 Adapter Pattern Example 2
l Notice in Example 1 that the PegAdapter adapts a RoundPeg to a
SquarePeg. The interface for PegAdapter is that of a SquarePeg.
l What if we want to have an adapter that acts as a SquarePeg or a
RoundPeg? Such an adapter is called a two-way adapter.
l One way to implement two-way adapters is to use multiple
inheritance, but we can't do this in Java
l But we can have our adapter class implement two different Java
interfaces!
Bob Tarr Design Patterns In Java The Adapter Pattern
13 13
Adapter Pattern Example 2 (Continued) Adapter Pattern Example 2 (Continued)
l Here are the interfaces for round and square pegs:
/**
*The IRoundPeg interface.
*/
public interface IRoundPeg {
public void insertIntoHole(String msg);
}
/**
*The ISquarePeg interface.
*/
public interface ISquarePeg {
public void insert(String str);
}
Bob Tarr Design Patterns In Java The Adapter Pattern
14 14
Adapter Pattern Example 2 (Continued) Adapter Pattern Example 2 (Continued)
l Here are the new RoundPeg and SquarePeg classes. These are
essentially the same as before except they now implement the
appropriate interface.
// The RoundPeg class.
public class RoundPeg implements IRoundPeg {
public void insertIntoHole(String msg) {
System.out.println("RoundPeg insertIntoHole(): " + msg);
}
}
// The SquarePeg class.
public class SquarePeg implements ISquarePeg {
public void insert(String str) {
System.out.println("SquarePeg insert(): " + str);
}
}
Bob Tarr Design Patterns In Java The Adapter Pattern
15 15
Adapter Pattern Example 2 (Continued) Adapter Pattern Example 2 (Continued)
l And here is the new PegAdapter:
/**
* The PegAdapter class.
* This is the two-way adapter class.
*/
public class PegAdapter implements ISquarePeg, IRoundPeg {
private RoundPeg roundPeg;
private SquarePeg squarePeg;
public PegAdapter(RoundPeg peg) {this.roundPeg = peg;}
public PegAdapter(SquarePeg peg) {this.squarePeg = peg;}
public void insert(String str) {roundPeg.insertIntoHole(str);}
public void insertIntoHole(String msg){squarePeg.insert(msg);}
}
Bob Tarr Design Patterns In Java The Adapter Pattern
16 16
Adapter Pattern Example 2 (Continued) Adapter Pattern Example 2 (Continued)
l A client that uses the two-way adapter:
// Test program for Pegs.
public class TestPegs {
public static void main(String args[]) {
// Create some pegs.
RoundPeg roundPeg = new RoundPeg();
SquarePeg squarePeg = new SquarePeg();
// Do an insert using the square peg.
squarePeg.insert("Inserting square peg...");
// Create a two-way adapter and do an insert with it.
ISquarePeg roundToSquare = new PegAdapter(roundPeg);
roundToSquare.insert("Inserting round peg...");
Bob Tarr Design Patterns In Java The Adapter Pattern
17 17
Adapter Pattern Example 2 (Continued) Adapter Pattern Example 2 (Continued)
// Do an insert using the round peg.
roundPeg.insertIntoHole("Inserting round peg...");
// Create a two-way adapter and do an insert with it.
IRoundPeg squareToRound = new PegAdapter(squarePeg);
squareToRound.insertIntoHole("Inserting square peg...");
}
}
l Client program output:
SquarePeg insert(): Inserting square peg...
RoundPeg insertIntoHole(): Inserting round peg...
RoundPeg insertIntoHole(): Inserting round peg...
SquarePeg insert(): Inserting square peg...
Bob Tarr Design Patterns In Java The Adapter Pattern
18 18
Adapter Pattern Example 3 Adapter Pattern Example 3
l This example comes from Roger Whitney, San Diego State
University
l Situation: A Java class library exists for creating CGI web server
programs. One class in the library is the CGIVariables class
which stores all CGI environment variables in a hash table and
allows access to them via a get(String evName) method. Many
Java CGI programs have been written using this library. The
latest version of the web server supports servlets, which provide
functionality similar to CGI programs, but are considerably more
efficient. The servlet library has an HttpServletRequest class
which has a getX() method for each CGI environment variable.
We want to use servlets. Should we rewrite all of our existing
Java CGI programs??
Bob Tarr Design Patterns In Java The Adapter Pattern
19 19
Adapter Pattern Example 3 Adapter Pattern Example 3
l Solution : Well, we'll have to do some rewriting, but let's attempt
to minimize things. We can design a CGIAdapter class which has
the same interface (a get() method) as the original CGIVariables
class, but which puts a wrapper around the HttpServletRequest
class. Our CGI programs must now use this CGIAdapter class
rather than the original CGIVariables class, but the form of the
get() method invocations need not change.
Bob Tarr Design Patterns In Java The Adapter Pattern
20 20
Adapter Pattern Example 3 (Continued) Adapter Pattern Example 3 (Continued)
l Here's a snippet of the CGIAdapter class:
public class CGIAdapter {
Hashtable CGIVariables = new Hashtable(20);
public CGIAdapter(HttpServletRequest CGIEnvironment) {
CGIVariables.put("AUTH_TYPE", CGIEnvironment.getAuthType());
CGIVariables.put("REMOTE_USER", CGIEnvironment.getRemoteUser());
etc.
}
public Object get(Object key) {return CGIvariables.get(key);}
}
l Note that in this example, the Adapter class (CGIAdapter) itself
constructs the Adaptee class (CGIVariables)
Bob Tarr Design Patterns In Java The Adapter Pattern
21 21
Adapter Pattern Example 4 Adapter Pattern Example 4
l Consider a utility class that has a copy() method which can make
a copy of an vector excluding those objects that meet a certain
criteria. To accomplish this the method assumes that all objects
in the vector implement the Copyable interface providing the
isCopyable() method to determine if the object should be copied
or not.
BankAccount
VectorUtilities
Copyable Interface
isCopyable()
Bob Tarr Design Patterns In Java The Adapter Pattern
22 22
Adapter Pattern Example 4 (Continued) Adapter Pattern Example 4 (Continued)
l Here�s the Copyable interface:
public interface Copyable {
public boolean isCopyable();
}
l And here�s the copy() method of the VectorUtilities class:
public static Vector copy(Vector vin) {
Vector vout = new Vector();
Enumeration e = vin.elements();
while (e.hasMoreElements()) {
Copyable c = (Copyable) e.nextElement();
if (c.isCopyable())
vout.addElemet(c);
}
return vout;
}
Bob Tarr Design Patterns In Java The Adapter Pattern
23 23
Adapter Pattern Example 4 (Continued) Adapter Pattern Example 4 (Continued)
l But what if we have a class, say the Document class, that does not
implement the Copyable interface. We want to be able perform a
selective copy of a vector of Document objects, but we do not
want to modify the Document class at all. Sounds like a job for
(TA-DA) an adapter!
l To make things simple, let�s assume that the Document class has
a nice isValid() method we can invoke to determine whether or
not it should be copied
Bob Tarr Design Patterns In Java The Adapter Pattern
24 24
Adapter Pattern Example 4 (Continued) Adapter Pattern Example 4 (Continued)
l Here�s our new class diagram:
VectorUtilities
Copyable Interface
isCopyable()
Document
isValid()
DocumentAdapter
Bob Tarr Design Patterns In Java The Adapter Pattern
25 25
Adapter Pattern Example 4 (Continued) Adapter Pattern Example 4 (Continued)
l And here is our DocumentAdapter class:
public class DocumentAdapter implements Copyable {
private Document d;
public DocumentAdapter(Document d) {
document = d;
}
public boolean isCopyable() {
return d.isValid();
}
}
Bob Tarr Design Patterns In Java The Adapter Pattern
26 26
Adapter Pattern Example 5 Adapter Pattern Example 5
l Do you see any Adapter pattern here?
public class ButtonDemo {
public ButtonDemo() {
Button button = new Button("Press me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doOperation();
}
});
}
public void doOperation() { whatever }
}
Bob Tarr Design Patterns In Java The Adapter Pattern
27 27
Adapter Pattern Example 5 (Continued) Adapter Pattern Example 5 (Continued)
l Button objects expect to be able to invoke the actionPerformed()
method on their associated ActionListener objects. But the
ButtonDemo class does not have this method! It really wants the
button to invoke its doOperation() method. The anonymous inner
class we instantiated acts as an adapter object, adapting
ButtonDemo to ActionListener!
l Recall that there are some AWT listener interfaces that have
several methods which must be implemented by an event listener.
For example, the WindowListener interface has seven such
methods. In many cases, an event listener is really only interested
in one specific event, such as the Window Closing event.
Bob Tarr Design Patterns In Java The Adapter Pattern
28 28
Adapter Pattern Example 5 (Continued) Adapter Pattern Example 5 (Continued)
l Java provides �adapter� classes as a convenience in this situation.
For example, the WindowAdapter class implements the
WindowListener interface, providing �do nothing�
implementation of all seven required methods. An event listener
class can extend WindowAdapter and override only those
methods of interest
l And now we see why they are called adapter classes!
[ June 20, 2004: Message edited by: Jacques Bosch ]
That is great. Thanx guys!
Thanx much, Sivasundaram and Joyce!
20 years ago
Hi there. Does anybody know when there will be an SCBCD exam for J2EE 1.4?
Hi Hai.

It means that the web app is distributed over more than one VM / server.
Say you have 5 servers running together as a distributed environment, and something goes wrong with 1 or two of them, there are still the others to carry the load until the problems can be sorted out. I.e. the clients don't even know there was a problem.

Plus other benefits like faster processing of requests etc.
Hi Edward.


1. RequestDispatcher--include / forward. The books said " include ", the request is not "forwarded" permanently. INstead, it is passed to the other resource temporarily.
What is meaning of "temporarily"? , give me an example, please.


Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes. The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

The include method of the RequestDispatcher interface may be called at ANY time. The target servlet of the include method has access to all aspects of the request object, but its use of the response object is more limited. It can only write information to the ServletOutputStream or Writer of the response object and commit a response by writing content past the end of the response buffer, or by explicitly calling the flushBuffer method of the ServletResponse interface. It CANNOT set headers or call any method that affects the headers of the response. Any attempt to do so must be ignored.


2. "RequesDispatcher.forward() is transparent to the browser while HttpServletResponse.sendRedirect() is not".


It means that when you use HttpServletResponse.sendRedirect(), the browser is notified to go to a new location. Where as when RequestDispatcher.forward() is used, the client browser doesn't even know about it (i.e. it is transparent).
Hi Vipin.
Perhaps this link will be of some help.:

http://www.jdiscuss.com/Enthuse/jsp/ViewPosts.jsp?forumid=26&topicid=635

J


Release date is set before the JavaOne conference which starts at June 28.


Thanx man.
But two things. I need the material in electronic format, as I use a screen reader to access my PC.
And I need to write the exam in the next two weeks.

J


I just checked the APIs at http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/jsp/tagext/BodyTag.html
But I could not find this information.
Please advice,


You seem to be checking the 1.3 info. I think it was only added in 1.4. Check out the 1.4 specs.

Jacques
Hi guys.
Where can I get study material for the following?


The additional topics for 1.4 are:
- Expression Language
- Simple Tag and Tag files
- JSTL 1.1



I am fine with all the rest, but haven't found resources for these.

Thanx much!
Hi kiran.

Check out the Java Ranch's SCWCD links here:
http://www.javaranch.com/scwcdlinks.jsp

There is also a nice simple tutorial from IBM Developer Works called: Java certification success, Part 2: SCWCD. Get it from http://www-106.ibm.com/developerworks/edu/j-dw-java-scwcd-i.html?S_TACT=104AHW02

Hope that is of help.
Hi Vipin.

Only the BodyTag interface can return that value.


BodyTag interface
The BodyTag interface extends IterationTag by defining additional methods that let a tag handler manipulate the content of evaluating its body:

* The doStartTag() method can return SKIP_BODY, EVAL_BODY_INCLUDE, or EVAL_BODY_BUFFERED.

* If EVAL_BODY_INCLUDE or SKIP_BODY is returned, then evaluation happens as in IterationTag.

* If EVAL_BODY_BUFFERED is returned, setBodyContent() is invoked, doInitBody() is invoked, the body is evaluated, doAfterBody() is invoked, and then, after zero or more iterations, doEndTag() is invoked. The doAfterBody() element returns EVAL_BODY_AGAIN or EVAL_BODY_BUFFERED to continue evaluating the page and SKIP_BODY to stop the iteration.

(Sorry for reporting this here - don't know who else to let know.)
Hi there to the administrator.
On the SCWCD links page, the link to Mika Hirvasoja's Mock Exam seems to be dead. URL is: http://www.withmilk.com/scwcd.jsp


404 Not Found
/scwcd.jsp was not found on this server.

Resin 2.1.11 (built Mon Sep 8 09:36:19 PDT 2003)



J