码迷,mamicode.com
首页 > 其他好文 > 详细

原型模式

时间:2019-02-13 21:10:14      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:code   基于   pac   output   close   类型   etag   inpu   main   

数据内容完全一样,但实例不同

  1. 能够直接拷贝其实际内容的数据类型/只支持9种,八大基本数据类型+String
  2. 深拷贝,其它类型也能拷贝

简单实例:


package prototype.simple;

import java.util.ArrayList;
import java.util.Date;

/**
 * 克隆基于字节码
 * 底层是c语言写的
 * 不走构造方法
 */
public class ConcretePrototype implements Cloneable{
    private int age;
    private String name;
    public ArrayList<String> list = new ArrayList<>();
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        ConcretePrototype prototype = null;

        //能够直接拷贝其实际内容的数据类型/只支持9种,八大基本数据类型+String
        prototype = (ConcretePrototype) super.clone();
        //手动克隆
        prototype.list = (ArrayList<String>) list.clone();

        return prototype;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package prototype.simple;

import java.util.Date;

/**
 * 原型模式
 * 数据内容完全一样,但实例不同
 * 1. 能够直接拷贝其实际内容的数据类型/只支持9种,八大基本数据类型+String
 * 2. 深拷贝,其它类型也能拷贝
 */
public class Test {
    public static void main(String[] args){
        ConcretePrototype cp = new ConcretePrototype();
        cp.setAge(11);
        cp.setName("abc");
        cp.list.add("hhhh");
        cp.setDate(new Date());
        try {
            ConcretePrototype clone = (ConcretePrototype) cp.clone();
            System.out.println(clone==cp);
            System.out.println(clone.list==cp.list);
            System.out.println(clone.getAge()+" "+clone.getName()+" "+clone.list.size());
            System.out.println(clone.getDate().getTime());
            System.out.println(cp.getDate().getTime());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

-------------------------------------------------

package prototype.prototype;

import java.util.Date;

/**
 * 鸣人
 */
public class MingRen {
    protected int height;
    protected Date date;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}
package prototype.prototype;

import java.io.Serializable;

/**
 * 手里剑
 */
public class ShouLiJian implements Serializable {
    private float height = 10;

    public void grow(){
        this.height *= 2;
    }

    public float getHeight() {
        return height;
    }
}
package prototype.prototype;

import java.io.*;
import java.util.Date;

/**
 *
 */
public class YingFenSheng extends  MingRen implements Cloneable, Serializable {
    private ShouLiJian shouLiJian;

    public YingFenSheng() {
        System.out.println("构造执行");
        this.shouLiJian = new ShouLiJian();
        this.height = 170;
        this.date = new Date();
    }

    @Override
    protected Object clone(){
        //深度克隆
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;

        try {
            //序列化
            //对象持久化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            YingFenSheng yingFenSheng = (YingFenSheng) ois.readObject();
            // 设置新的时间
            yingFenSheng.date = new Date();

            return yingFenSheng;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (bos!=null){
                    bos.close();
                }
                if (oos!=null){
                    oos.close();
                }
                if (bis!=null){
                    bis.close();
                }
                if (ois!=null){
                    ois.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    public void change(){
        YingFenSheng copy = (YingFenSheng)clone();
        System.out.println("mingren: "+this.date.getTime()+" "+copy.getHeight());
        System.out.println("copy: "+copy.getDate().getTime()+" "+copy.getHeight());
        System.out.println(this==copy);
        System.out.println(this.getShouLiJian()==copy.getShouLiJian());
        this.getShouLiJian().grow();
        System.out.println("------------");
        System.out.println("yuan ShouLiJian: "+this.getShouLiJian().getHeight());
        System.out.println("copy ShouLiJian: "+copy.getShouLiJian().getHeight());
    }

    public ShouLiJian getShouLiJian() {
        return shouLiJian;
    }

    public void setShouLiJian(ShouLiJian shouLiJian) {
        this.shouLiJian = shouLiJian;
    }
}
package prototype.prototype;

/**
 *
 */
public class Test {
    public static void main(String[] args){
        YingFenSheng yingFenSheng = new YingFenSheng();
        yingFenSheng.change();
    }
}

原型模式

标签:code   基于   pac   output   close   类型   etag   inpu   main   

原文地址:https://www.cnblogs.com/fly-book/p/10371532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!