您的位置:建站学院首页 >> 文章 >> JSP技术 >> 设计模式
Prototype 模式
模式描述:Prototype模式用于创建对象,尤其是当创建对象需要许多时间和资源时。
在Java中,Prototype模式的实现是通过方法clone(),该方法定义在Java的根对象Object中,
因此,Java中的其他对象只要覆盖它就行了。通过clone(),我们可以从一个对象获得更多的对象,
并请可以按照我们的需要修改他们的属性。
模式结构:

部分源程序
//Prototype.java (由Retional Rose2000 生成)
package jdeveloper.patterns.prototype;
/**
* @author jdeveloper
* @see http://www.ChinaJavaWorld.com
* @version 1.0
*/
public class Prototype implements Cloneable
{
private String Name;
public Prototype(String Name)
{
this.Name = Name;
}
/**
* @return Object
* @exception
* @author
* @see
* @version
* @roseuid 3AFC9B7700CD
*/
public Object clone()
{
try{
return super.clone();
}catch(CloneNotSupportedException cnse){
cnse.printStackTrace();
return null;
}
}
public void setName(String Name){
this.Name = Name;
}
public String getName(){
return Name;
}
}
//PrototypeApplet.java
package jdeveloper.patterns.prototype;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
/**
* Title: Jdeveloper's Java Projdect
* Description: n/a
* Copyright: Copyright (c) 2001
* Company: http://www.ChinaJavaWorld.com
* @author jdeveloper
* @version 1.0
*/
public class PrototypeApplet extends Applet {
Prototype p = new Prototype("Original Object");
boolean isStandalone = false;
BorderLayout borderLayout1 = new BorderLayout();
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Button button1 = new Button();
Label label1 = new Label();
TextField textField1 = new TextField();
TextArea textArea1 = new TextArea();
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public PrototypeApplet() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
button1.setLabel("Clone");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
label1.setText("新对象名字");
textArea1.setColumns(40);
textArea1.setEditable(false);
textArea1.setRows(10);
textField1.setColumns(15);
this.add(panel2, BorderLayout.NORTH);
panel2.add(button1, null);
panel2.add(label1, null);
panel2.add(textField1, null);
this.add(panel1, BorderLayout.CENTER);
panel1.add(textArea1, null);
}
/**Start the applet*/
public void start() {
}
/**Stop the applet*/
public void stop() {
}
/**Destroy the applet*/
public void destroy() {
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
/**Main method*/
public static void main(String[] args) {
PrototypeApplet applet = new PrototypeApplet();
applet.isStandalone = true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
void button1_actionPerformed(ActionEvent e) {
Prototype cloneObject =(Prototype) p.clone();
cloneObject.setName(textField1.getText());
textArea1.setText("");
textArea1.append("Original Object Name is:" + p.getName()+ "\n");
textArea1.append("Clone Object Name is:" + cloneObject.getName()+ "\n");
}
}
在Java中,Prototype模式的实现是通过方法clone(),该方法定义在Java的根对象Object中,
因此,Java中的其他对象只要覆盖它就行了。通过clone(),我们可以从一个对象获得更多的对象,
并请可以按照我们的需要修改他们的属性。
模式结构:

部分源程序
//Prototype.java (由Retional Rose2000 生成)
package jdeveloper.patterns.prototype;
/**
* @author jdeveloper
* @see http://www.ChinaJavaWorld.com
* @version 1.0
*/
public class Prototype implements Cloneable
{
private String Name;
public Prototype(String Name)
{
this.Name = Name;
}
/**
* @return Object
* @exception
* @author
* @see
* @version
* @roseuid 3AFC9B7700CD
*/
public Object clone()
{
try{
return super.clone();
}catch(CloneNotSupportedException cnse){
cnse.printStackTrace();
return null;
}
}
public void setName(String Name){
this.Name = Name;
}
public String getName(){
return Name;
}
}
//PrototypeApplet.java
package jdeveloper.patterns.prototype;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
/**
* Title: Jdeveloper's Java Projdect
* Description: n/a
* Copyright: Copyright (c) 2001
* Company: http://www.ChinaJavaWorld.com
* @author jdeveloper
* @version 1.0
*/
public class PrototypeApplet extends Applet {
Prototype p = new Prototype("Original Object");
boolean isStandalone = false;
BorderLayout borderLayout1 = new BorderLayout();
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Button button1 = new Button();
Label label1 = new Label();
TextField textField1 = new TextField();
TextArea textArea1 = new TextArea();
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public PrototypeApplet() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
button1.setLabel("Clone");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
label1.setText("新对象名字");
textArea1.setColumns(40);
textArea1.setEditable(false);
textArea1.setRows(10);
textField1.setColumns(15);
this.add(panel2, BorderLayout.NORTH);
panel2.add(button1, null);
panel2.add(label1, null);
panel2.add(textField1, null);
this.add(panel1, BorderLayout.CENTER);
panel1.add(textArea1, null);
}
/**Start the applet*/
public void start() {
}
/**Stop the applet*/
public void stop() {
}
/**Destroy the applet*/
public void destroy() {
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
/**Main method*/
public static void main(String[] args) {
PrototypeApplet applet = new PrototypeApplet();
applet.isStandalone = true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
void button1_actionPerformed(ActionEvent e) {
Prototype cloneObject =(Prototype) p.clone();
cloneObject.setName(textField1.getText());
textArea1.setText("");
textArea1.append("Original Object Name is:" + p.getName()+ "\n");
textArea1.append("Clone Object Name is:" + cloneObject.getName()+ "\n");
}
}