Java Tutorial/Design Pattern/Prototype Pattern
Версия от 17:44, 31 мая 2010; (обсуждение)
Prototype Pattern Demo
public class PrototypeDemo {
private Message message;
PrototypeDemo(Message message) {
this.message = message;
}
Message makeMessage() {
return this.message.makeCopy();
}
public static void main(String[] args) {
Message prototype = new EmailMessage();
PrototypeDemo demo = new PrototypeDemo(prototype);
System.out.println("Message " + demo.makeMessage());
}
}
abstract class Message {
public Message makeCopy() {
try {
return this.getClass().newInstance();
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
return null;
}
}
}
class EmailMessage extends Message {
@Override
public String toString() {
return "EmailMessage";
}
}