public class Main {
public static void main(String[] args) {
String str1 = "fly";
String str2 = "weight";
String str3 = "flyweight";
String str4;
str4 = str1 + str2;
System.out.println(str3 == str4);
str4 = (str1 + str2).intern();
System.out.println(str3 == str4);
}
}
import java.util.*;
public class MessageApplication {
public void showAllMessage(Enumeration enum) {
Object msg;
while(enum.hasMoreElements()) {
msg = enum.nextElement();
System.out.println(msg);
}
}
}
import java.util.*;
public class MessageClient {
private MessageApplication msgApp;
public void run() {
Vector vector = new Vector();
for(int i = 0; i < 10; i++)
vector.addElement("物g " + i);
msgApp = new MessageApplication();
msgApp.showAllMessage(vector.elements());
}
public static void main(String[] args) {
MessageClient msgClient = new MessageClient();
msgClient.run();
}
}
import java.util.*;
public class IteratorAdapter implements Enumeration {
private Iterator iterator;
IteratorAdapter(Iterator iterator) {
this.iterator = iterator;
}
// 轉接介面
public boolean hasMoreElements() {
return iterator.hasNext();
}
public Object nextElement()
throws NoSuchElementException {
return iterator.next();
}
}
import java.util.*;
public class MessageClient {
// We could still use MessageApplication
private Enumeration iteratorAdapter;
public void run() {
List arrayList = new ArrayList();
for(int i = 0; i < 10; i++)
arrayList.add("物g " + i);
iteratorAdapter =
new IteratorAdapter(arrayList.iterator());
// We could still use MessageApplication
MessageApplication msgApp = new MessageApplication();
msgApp.showAllMessage(iteratorAdapter);
}
public static void main(String[] args) {
MessageClient msgClient = new MessageClient();
msgClient.run();
}
}
如程式所C的Q透過Adapter模式Q您原有E式中已a計好的別不用更動Q就可以引進新別的功能,上面的E式UML別i構畫出如下Q?br>
import java.util.logging.*;
public class HelloSpeaker {
private Logger logger =
Logger.getLogger(this.getClass().getName());
public void hello(String name) {
logger.log(Level.INFO, "hello method starts....");
System.out.println("Hello, " + name);
logger.log(Level.INFO, "hello method ends....");
}
}
public interface IHello {
public void hello(String name);
}
public class HelloSpeaker implements IHello {
public void hello(String name) {
System.out.println("Hello, " + name);
}
}
import java.util.logging.*;
public class HelloProxy implements IHello {
private Logger logger =
Logger.getLogger(this.getClass().getName());
private IHello helloObject;
public HelloProxy(IHello helloObject) {
this.helloObject = helloObject;
}
public void hello(String name) {
logger.log(Level.INFO, "hello method starts....");
helloObject.hello(name);
logger.log(Level.INFO, "hello method ends....");
}
}
import java.util.logging.*;
import java.lang.reflect.*;
public class LogHandler implements InvocationHandler {
private Logger logger =
Logger.getLogger(this.getClass().getName());
private Object delegate;
public Object bind(Object delegate) {
this.delegate = delegate;
return Proxy.newProxyInstance(
delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(),
this);
}
public Object invoke(Object proxy,
Method method,
Object[] args) throws Throwable {
Object result = null;
try {
logger.log(Level.INFO,
"method starts..." + method);
result = method.invoke(delegate, args);
logger.log(Level.INFO,
"method ends..." + method);
} catch (Exception e){
logger.log(Level.INFO, e.toString());
}
return result;
}
}
public interface IHello {
public void hello(String name);
}
public class HelloSpeaker implements IHello {
public void hello(String name) {
System.out.println("Hello, " + name);
}
}