ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

原型模式

2020-04-14 11:00:10  阅读:186  来源: 互联网

标签:name 模式 hashCode public 原型 User id Subject


一:浅拷贝

被引用的类

package com.wing.mall.base.prototype;

import lombok.Data;

/**
 * @ProjectName: baby
 * @Package: com.wing.mall.base.prototype
 * @ClassName: Subject
 * @Author: heluwei
 * @Description: 对象拷贝的类
 * @Date: 2020/4/14 10:14
 * @Version: 1.0
 */
@Data
public class Subject {
    private String name;

    public Subject(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "[Subject: " + this.hashCode() + ",name:" + name + "]";
    }
}

要拷贝的对象

package com.wing.mall.base.prototype;

import lombok.Data;

import java.io.*;

/**
 * @ProjectName: baby
 * @Package: com.wing.mall.base.prototype
 * @ClassName: User
 * @Author: heluwei
 * @Description: 原型模式
 * @Date: 2020/4/14 8:56
 * @Version: 1.0
 */
@Data
public class User implements Serializable, Cloneable {
    private static final long serialVersionUID = 1L;
    //引用类型
    private Subject subject;
    //基本类型
    private Long id;
    private String name;

    public User() {
    }

    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    //浅拷贝
    protected Object shallowClone(){
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

    @Override
    public String toString() {
        return "User{" +
                "subject=" + subject +
                ", id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

测试:


package com.wing.mall.base.prototype;

import java.util.ArrayList;
import java.util.List;

/**
* @ProjectName: baby
* @Package: com.wing.mall.base.prototype
* @ClassName: TestPrototype
* @Author: heluwei
* @Description: 利用原型模式
* @Date: 2020/4/14 8:57
* @Version: 1.0
*/
public class TestPrototype {
public static void main(String[] args) {
//浅拷贝
Subject subject = new Subject("yuwen");
User userA = new User();
userA.setSubject(subject);
userA.setId(1L);
userA.setName("张三");
User userB = (User) userA.shallowClone();
userB.setId(2L);
userB.setName("李四");
Subject subjectB = userB.getSubject();
subjectB.setName("lishi");
System.out.println("userA:" + userA.toString()+":"+userA.hashCode());
System.out.println("userB:" + userB.toString()+":"+userB.hashCode());
}

}
 

 

输出结果:

userA:User{subject=[Subject: 102982226,name:lishi], id=1, name='张三'}:1999823465
userB:User{subject=[Subject: 102982226,name:lishi], id=2, name='李四'}:1999890696

可以看到输出的hashCode(). User类继承了Cloneable。输出的hashCode不一致。当修改userA的时候。userB不会被影响。但是Subject会被影响。

二:深拷贝

Subject实现Cloneable接口
package com.wing.mall.base.prototype;

import lombok.Data;

/**
 * @ProjectName: baby
 * @Package: com.wing.mall.base.prototype
 * @ClassName: Subject
 * @Author: heluwei
 * @Description: 对象拷贝的类
 * @Date: 2020/4/14 10:14
 * @Version: 1.0
 */
@Data
public class Subject implements Cloneable {
    private String name;

    public Subject(String name) {
        this.name = name;
    }

    //重写Object的clone()方法。
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "[Subject: " + this.hashCode() + ",name:" + name + "]";
    }
}

在User类中重写clone方法。

//深拷贝
    protected Object deepClone(){
        try {
            // 直接调用父类的clone()方法
            User user = (User) super.clone();
            user.subject = (Subject)subject.clone();
            return user;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

输出:

userA:User{subject=[Subject: 115349247,name:yuwen], id=1, name='张三'}:2099750606
userB:User{subject=[Subject: 102982226,name:lishi], id=2, name='李四'}:1999890696

应用:在for循环中创建对象

public static void main(String[] args) {
        User user = new User();
        List<User> userList = new ArrayList<>();
        for (long i = 0; i < 5; i++) {
            User prototypeUser = (User)user.clone();
            prototypeUser.setId(i);
            prototypeUser.setName(String.valueOf(i));
            userList.add(prototypeUser);
        }
        userList.forEach(user1 -> {
            System.out.println(user1+"hashCode:"+user1.hashCode());
        });
    }

输出结果:

User{, id=0, name='0'}hashCode:3529
User{, id=1, name='1'}hashCode:3589
User{, id=2, name='2'}hashCode:3649
User{, id=3, name='3'}hashCode:3709
User{, id=4, name='4'}hashCode:3769

 

标签:name,模式,hashCode,public,原型,User,id,Subject
来源: https://www.cnblogs.com/bulrush/p/12696513.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有