设计模式-原型模式实例-03-原型模式扩展

设计模式-原型模式实例-03-原型模式扩展

实例1:颜色原型管理器

代码实现:

代码结构:

 

 

MyColor.java:

//抽象原型类
public interface MyColor extends Cloneable{
    public Object clone();
    public void display();
}

 

Red.java:

//具体原型类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.java:

//具体原型类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!");
    }
}

 

PrototypeManager.java:

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();
    }
}

 

Client.java:

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);
    }
}

 

 

 

实例2:相似对象的复制

代码实现:

代码结构:

 

 

Student.java:

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;
     }
}

 

MainClass.java:

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();

    }
}

 

 

 
posted @ 2022-03-13 20:36  临易  阅读(49)  评论(0编辑  收藏  举报