代码实现:
代码结构:
//抽象原型类
public interface MyColor extends Cloneable{
public Object clone();
public void display();
}
//具体原型类Red
public class Red implements MyColor{
public Object clone(){
Red r = null;
try {
r = (Red) super.clone();
}catch (CloneNotSupportedException e){
}
return r;
}
public void display(){
System.out.println("This is Red!");
}
}
//具体原型类Blue
public class Blue implements MyColor{
public Object clone(){
Blue b = null;
try {
b = (Blue) super.clone();
}catch (CloneNotSupportedException e){
}
return b;
}
public void display(){
System.out.println("This is Blue!");
}
}
import java.util.Hashtable;
//原型管理器类
public class PrototypeManager {
private Hashtable ht = new Hashtable();
public PrototypeManager(){
ht.put("red",new Red());
ht.put("blue",new Blue());
}
public void addColor(String key,MyColor obj){
ht.put(key,obj);
}
public MyColor getColor(String key){
return (MyColor) ((MyColor)ht.get(key)).clone();
}
}
public class Client {
public static void main(String[] args) {
PrototypeManager pm = new PrototypeManager();
MyColor obj1 = (MyColor) pm.getColor("red");
obj1.display();
MyColor obj2 = (MyColor) pm.getColor("blue");
obj2.display();
System.out.println(obj1 == obj2);
}
}
代码实现:
代码结构:
public class Student implements Cloneable{
private String stuName;
private String stuSex;
private int stuAge;
private String stuMajor;
private String stuCollege;
private String stuUniversity;
public Student(String stuName,String stuSex,int stuAge,String stuMajor,String stuCollege,String stuUniversity){
this.stuName = stuName;
this.stuSex = stuSex;
this.stuAge = stuAge;
this.stuMajor = stuMajor;
this.stuCollege = stuCollege;
this.stuUniversity = stuUniversity;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuMajor() {
return stuMajor;
}
public void setStuMajor(String stuMajor) {
this.stuMajor = stuMajor;
}
public String getStuCollege() {
return stuCollege;
}
public void setStuCollege(String stuCollege) {
this.stuCollege = stuCollege;
}
public String getStuUniversity() {
return stuUniversity;
}
public void setStuUniversity(String stuUniversity) {
this.stuUniversity = stuUniversity;
}
public Student clone(){
Student cpStudent = null;
try {
cpStudent = (Student) super.clone();
}catch (CloneNotSupportedException e){
}
return cpStudent;
}
}
public class MainClass {
public static void main(String[] args) {
Student stu1,stu2,stu3;
stu1 = new Student("张无忌","男",24,"软件工程","软件学院","tyut");
//使用原型模式
stu2 = stu1.clone();
stu2.setStuName("杨过");
//使用原型模式
stu3 = stu1.clone();
stu3.setStuName("小龙女");
stu3.setStuSex("女");
System.out.print("姓名:" + stu1.getStuName());
System.out.print(",性别:" + stu1.getStuSex());
System.out.print(",年龄:" + stu1.getStuAge());
System.out.print(",专业:" + stu1.getStuMajor());
System.out.print(",学院:" + stu1.getStuCollege());
System.out.print(",学校:" + stu1.getStuUniversity());
System.out.println();
System.out.print("姓名:" + stu2.getStuName());
System.out.print(",性别:" + stu2.getStuSex());
System.out.print(",年龄:" + stu2.getStuAge());
System.out.print(",专业:" + stu2.getStuMajor());
System.out.print(",学院:" + stu2.getStuCollege());
System.out.print(",学校:" + stu2.getStuUniversity());
System.out.println();
System.out.print("姓名:" + stu3.getStuName());
System.out.print(",性别:" + stu3.getStuSex());
System.out.print(",年龄:" + stu3.getStuAge());
System.out.print(",专业:" + stu3.getStuMajor());
System.out.print(",学院:" + stu3.getStuCollege());
System.out.print(",学校:" + stu3.getStuUniversity());
System.out.println();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128220.html