📄 prototype.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="description" content="Java,JDBC,EJB,Open Source,jdk,rmi">
<meta name="Keywords"
content="Java, servlets, Java servlet, Javascript, ActiveX, VRML,
applet, applets, directory, news, jdbc, applications,
Java applications, Java developer, Java development, developer,
classes, Jars.com, Jars, intranet, Java applet, Javabeans,
Java products, JDK, Java development kit, java development environment, JIT,
JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources,
SUN, CGI, Perl, database, network, html,
xml, dhtml, rating, ratings, review, jars, cgi, programming,
software review, software rating">
<title>csdn_Prototype 模式</title>
<style>
.news { BACKGROUND: #007cd3; font-family: "宋体"; font-size: 9pt }
.t { font-family: "宋体"; font-size: 9pt }
.t1 { color:#007cd3; font-family: "宋体"; font-size: 9pt }
.white { font-family: "宋体"; font-size: 9pt;color:#FFFFFF }
.red { font-family: "宋体"; font-size: 9pt;color:#FF0000 }
A:visited {color:#0000FF}
A:hover {color: #ff6666; text-decoration: none}
.text {font-size: 12px; line-height: 160%; font-family: "宋体"}
.text1 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text1:visited {color:#000000}
.text1:hover {color: #000000}
.text2 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text2:visited {color:#000000}
.text2:hover {color: #000000}
.text3 {font-size: 12px; line-height: 100%; font-family: "宋体"; text-decoration: none}
.large {font-size: 14.8px; line-height: 130%}
</style>
</head>
<body
<center>
<tr>
<td WIDTH="100%" VALIGN="TOP">
<tr>
<td WIDTH="100%" CLASS="white"></td>
</tr>
<tr>
<td WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" align="center" nowrap>Prototype 模式</td>
<p> <td WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" align="center" nowrap>作者:jdeveloper </td></p>
</tr>
<tr>
<td WIDTH="100%" bordercolor="#FFFFFF" CLASS="t" bgcolor="#F0F0F0" colspan="2">
模式描述:<br>
Prototype模式用于创建对象,尤其是当创建对象需要许多时间和资源时。<br>
在Java中,Prototype模式的实现是通过方法clone(),该方法定义在Java的根对象Object中,<br>
因此,Java中的其他对象只要覆盖它就行了。通过clone(),我们可以从一个对象获得更多的对象,<br>
并请可以按照我们的需要修改他们的属性。<br><br>
模式结构:
<img src="prototype.gif" >
<br><br>
模式实现:
<br><br>
部分源程序<br>
//Prototype.java (由Retional Rose2000 生成)
<pre>
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
*/
<font color=red>
public Object clone()
{
try{
return super.clone();
}catch(CloneNotSupportedException cnse){
cnse.printStackTrace();
return null;
}
}
</font>
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 {
<font color=red>
Prototype p = new Prototype("Original Object");
</font>
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) {
<font color=red>
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");
</font>
}
}
</pre>
<br><br>
相关下载 <br>
<a href="prototype-src.zip">源程序下载</a><br>
<a href="prototype.jar.zip">jar文件下载</a> 下载后如此运行: java -jar prototype.jar <br>
</td>
</tr>
</td>
</tr>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -