栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

原型模式详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

原型模式详解

原型模式详解

实现一个原型接口,该接口通过拷贝原型对象创建新的对象。

应用场景
  • Java Object clone方法
  • 对原有对象需要大量复制

例子:

1.首先创建一个实现Cloneable接口的抽象类

public class Car implements Cloneable {
    private String brand;
    private BigDecimal price;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

2.复制对象

        Car car = new Car();
        car.setBrand("大众");
        car.setPrice(BigDecimal.valueOf(150000));

        System.out.println(car.getBrand());
        System.out.println(car.getPrice());

        //克隆
        Car car1 = (Car) car.clone();
        System.out.println(car1.getBrand());
        System.out.println(car1.getPrice());

        System.out.println(car);
        System.out.println(car1);

        car1.setBrand("红旗");
        car1.setPrice(BigDecimal.valueOf(200000));

        System.out.println(car1.getBrand());
        System.out.println(car1.getPrice());

输出结果:

大众
150000
大众
150000
dev.service.impl.mode.prototype.Car@74a14482
dev.service.impl.mode.prototype.Car@1540e19d
红旗
200000

深克隆和浅克隆

浅克隆:复制原有的引用。两个对象同一个引用。

深克隆:复制原对象所有变量并含有原有值,复制的是值。两个对象不同引用

浅克隆
public class Car implements Cloneable {
    static class Country {
        public Country(String name) {
            this.name = name;
        }

        private String name;

        public String getName() {
            return name;
        }

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

        @Override
        public String toString() {
            return "Country{" +
                    "name='" + name + ''' +
                    '}';
        }
    }


    private String brand;
    private BigDecimal price;
    private Country country;

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + ''' +
                ", price=" + price +
                ", country=" + country +
                '}';
    }

    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Car car = (Car) super.clone();
        return car;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Car car = new Car();
        car.setBrand("大众");
        car.setCountry(new Country("德国"));
        car.setPrice(BigDecimal.valueOf(150000));

        //克隆
        Car car1 = (Car) car.clone();
        System.out.println(car1.getBrand());
        System.out.println(car1.getPrice());

        //修改
        car1.setBrand("红旗");
        car1.setPrice(BigDecimal.valueOf(200000));
        car1.getCountry().setName("中国");

        System.out.println(car);
        System.out.println(car1);

    }
}

结果:

大众
150000
Car{brand='大众', price=150000, country=Country{name='中国'}}
Car{brand='红旗', price=200000, country=Country{name='中国'}}
深克隆
public class Car implements Cloneable {
    static class Country implements  Cloneable{
        public Country(String name) {
            this.name = name;
        }

        private String name;

        public String getName() {
            return name;
        }

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

        @Override
        public String toString() {
            return "Country{" +
                    "name='" + name + ''' +
                    '}';
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }


    private String brand;
    private BigDecimal price;
    private Country country;

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + ''' +
                ", price=" + price +
                ", country=" + country +
                '}';
    }

    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Car car = (Car) super.clone();
        Country country = (Country) car.getCountry().clone();
        car.setCountry(country);
        return car;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Car car = new Car();
        car.setBrand("大众");
        car.setCountry(new Country("德国"));
        car.setPrice(BigDecimal.valueOf(150000));

        //克隆
        Car car1 = (Car) car.clone();
        System.out.println(car1.getBrand());
        System.out.println(car1.getPrice());

        //修改
        car1.setBrand("红旗");
        car1.setPrice(BigDecimal.valueOf(200000));
        car1.getCountry().setName("中国");

        System.out.println(car);
        System.out.println(car1);

    }
}

结果:

大众
150000
Car{brand='大众', price=150000, country=Country{name='德国'}}
Car{brand='红旗', price=200000, country=Country{name='中国'}}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/880563.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号