笨蛋学设计模式创建者模式-原型模式【6】

6.5原型模式

6.5.1概念

​ 原型模式是指通过复制已有对象来创建新对象,而无需再次调用构造函数,当一个对象的构造需要消耗大量的资源时,通过使用原型模式,可以避免重复的创建过程,从而提高性能。

6.5.2场景

​ 在我们用到的音乐播放器软件都会有一些各种类型的歌单,但是有一些歌单的歌曲可能是重复的。而当我们想要创建一个新的歌单时,需要引用其他歌单中的大量的歌曲时,我们就可以复制别人的歌单,然后筛选掉哪些不需要的歌单,再去添加自己喜欢的歌曲到新歌单,这就节省了大量的时间去搜索新的歌曲添加到新歌单。

6.5.3优势 / 劣势

  • 提高性能:通过复制已有对象来创建新的对象,避免重复的创建过程,提高了代码性能
  • 简化对象创建:只是复制已有的对象,而不是重新创建,可以减少代码量,简化了对象创建

  • 需要实现接口:必须实现Cloneable接口或序列号Serializable接口,会对某些类造成不必要的负担
  • 实现深拷贝复杂:默认执行浅拷贝(只复制对象的引用而不复制对象本身),在实现深拷贝时需要编写较为复杂的代码

6.5.4原型模式可分为

  • 抽象原型接口prototype:声明一个克隆自身的方法clone
  • 具体原型类ConcretePrototype:实现clone方法,复制当前对象中的原始对象中的成员变量并返回一个新对象

6.5.5原型模式

  • 创建一个抽象类或接口,声明一个克隆方法clone
  • 实现具体原型类,重写克隆方法
  • 客户端中实例化具体原型类的对象,并调用其克隆方法来创建新的对象新的对象
package com.designpattern.mode.prototype;

public class Prototype {
    public static void main(String[] args) {

        //创建原型对象
        AbstractPrototype original = new ConcretePrototype("Original Data");

        //克隆原型对象
        AbstractPrototype clone = original.clone();

        //输出克隆对象的数据
        System.out.println("Clone Data:"+((ConcretePrototype) clone).getData());

    }
}

//定义抽象原型类
abstract class AbstractPrototype implements Cloneable{
    public abstract AbstractPrototype clone();
}

//创建具体原型类
class ConcretePrototype extends AbstractPrototype{
    private String data;

    public ConcretePrototype(String data) {
        this.data = data;
    }

    @Override
    public AbstractPrototype clone() {
        return new ConcretePrototype(this.data);
    }

    public String getData(){
        return data;
    }

}

6.5.6实战

6.5.6.1题目描述

公司正在开发一个图形设计软件,其中有一个常用的图形元素是矩形。设计师在工作时可能需要频繁地创建相似的矩形,而这些矩形的基本属性(如颜色、宽度、高度)相同,但具体的位置可能不同。

为了提高设计师的工作效率,请你使用原型模式设计一个矩形对象的原型。该原型可以根据用户的需求进行克隆,生成新的矩形对象。

6.5.6.2输入描述

第一行输入一个整数 N(1 ≤ N ≤ 100),表示需要创建的矩形数量。

接下来的 N 行,每行输入一个字符串,表示矩形的属性信息,分别为颜色,长度,宽度,比如 “Red 10 5”。

6.5.6.3输出描述

对于每个矩形,输出一行字符串表示矩形的详细信息,如 “Color: Red, Width: 10,Height: 5”。

6.5.6.4代码
package com.designpattern.mode.prototype;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int nums = scanner.nextInt();

        ConcreteRectangle concreteRectangle =null;
        for(int i=0;i<nums;i++){
            String rectangle = scanner.nextLine();

            String[] rectangleAttributes = rectangle.split(" ");
            String color= scanner.next();
            int width= scanner.nextInt();
            int height=scanner.nextInt();

            //创建原型对象
            AbstractRectanglePrototype abstractRectanglePrototype =
                    new ConcreteRectangle(color,width,height);

            //克隆对象并输出详细信息
//            AbstractRectanglePrototype cloneRectangle = abstractRectanglePrototype.clone();
//            System.out.println(cloneRectangle);

            AbstractRectanglePrototype cloneRectangle = abstractRectanglePrototype.clonePrototype();
            System.out.println(cloneRectangle.getDetails());


        }
    }
}

//创建抽象类,声明克隆方法
abstract class AbstractRectanglePrototype implements Cloneable{

    //创建复制矩形的方法
    public abstract AbstractRectanglePrototype clone();

    public AbstractRectanglePrototype clonePrototype(){
        try {
            return (AbstractRectanglePrototype) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    //打印矩形
    public abstract String getDetails();
}

//创建具体的矩形类
class ConcreteRectangle extends AbstractRectanglePrototype{

    //分别创建颜色、宽度、高度
    private String color;
    private int width;
    private int height;

    public ConcreteRectangle() {
    }

    public ConcreteRectangle(String color, int width, int height) {
        this.color = color;
        this.width = width;
        this.height = height;
    }

    public String getColor() {
        return color;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }



    @Override
    public AbstractRectanglePrototype clone() {
        return new ConcreteRectangle(this.color,this.width,this.height);
    }

    @Override
    public String getDetails() {
        return "color:" + color +
                ", width:" + width +
                ", height:" + height;
    }

    @Override
    public String toString() {
        return "color:" + color +
                ", width:" + width +
                ", height:" + height;
    }
}

6.5.7总结

  • 原型模式

优点:减少对象的创建和初始化开销,提高性能

总结:无需再次调用构造函数就能通过复制已有对象来创建新对象

场景:适用于频繁创建和销毁大量相似对象的场景

  • 28
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值