首页 > 其他分享 >原型模式(Prototype Pattern)

原型模式(Prototype Pattern)

时间:2023-05-21 19:23:03浏览次数:37  
标签:draw Square Pattern Shape 原型 Circle Prototype public Rectangle

原型模式(Prototype Pattern)

一、意图

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

二、优缺点

优点:

1、性能提高。

2、逃避构造函数的约束。

缺点:

1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。

2、必须实现 Cloneable 接口。

三、具体实现

3.1 模型结构
  1. Prototype 抽象原型类:抽象原型类是定义具有克隆自己的方法的接口,是所有具体原型类的公共父类,可以是抽象类,也可以是接口。
  2. ConcretePrototype 具体原型类:具体原型类实现具体的克隆方法,在克隆方法中返回自己的一个克隆对象。
  3. Client 客户类:客户类让一个原型克隆自身,从而创建一个新的对象。在客户类中只需要直接实例化或通过工厂方法等方式创建一个对象,再通过调用该对象的克隆方法复制得到多个相同的对象。
3.2 实现

1、 定义shape

/**
 * @author zhongtao
 * @date 2023/5/21 18:35
 */
public abstract class Shape implements Cloneable {

    private String id;
    protected String type;

    abstract void draw();

    public String getType() {
        return type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public Object clone() {
        Object clone = null;
        try {
            clone = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return clone;
    }
}

2、 定义具体的shape

/**
 * @author zhongtao
 * @date 2023/5/21 18:36
 */
public class Rectangle extends Shape {

    public Rectangle() {
        type = "Rectangle";
    }

    @Override
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}

/**
 * @author zhongtao
 * @date 2023/5/21 18:36
 */
public class Square extends Shape {

    public Square() {
        type = "Square";
    }

    @Override
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}
/**
 * @author zhongtao
 * @date 2023/5/21 18:37
 */
public class Circle extends Shape {

    public Circle() {
        type = "Circle";
    }

    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

3、缓存类

import java.util.Hashtable;

/**
 * @author zhongtao
 * @date 2023/5/21 18:37
 */
public class ShapeCache {

    private static Hashtable<String, Shape> shapeMap
            = new Hashtable<String, Shape>();

    public static Shape getShape(String shapeId) {
        Shape cachedShape = shapeMap.get(shapeId);
        return (Shape) cachedShape.clone();
    }

    // 对每种形状都运行数据库查询,并创建该形状
    // shapeMap.put(shapeKey, shape);
    // 例如,我们要添加三种形状
    public static void loadCache() {
        Circle circle = new Circle();
        circle.setId("1");
        shapeMap.put(circle.getId(), circle);

        Square square = new Square();
        square.setId("2");
        shapeMap.put(square.getId(), square);

        Rectangle rectangle = new Rectangle();
        rectangle.setId("3");
        shapeMap.put(rectangle.getId(), rectangle);
    }
}
/**
 * @author zhongtao
 * @date 2023/5/21 18:38
 */
public class PrototypePatternDemo {
    public static void main(String[] args) {
        ShapeCache.loadCache();

        Shape clonedShape = (Shape) ShapeCache.getShape("1");
        System.out.println("Shape : " + clonedShape.getType());
        clonedShape.draw();

        Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
        System.out.println("Shape : " + clonedShape2.getType());
        clonedShape2.draw();

        Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
        System.out.println("Shape : " + clonedShape3.getType());
        clonedShape3.draw();
    }
}

输出

Shape : Circle
Inside Circle::draw() method.
Shape : Square
Inside Square::draw() method.
Shape : Rectangle
Inside Rectangle::draw() method.

标签:draw,Square,Pattern,Shape,原型,Circle,Prototype,public,Rectangle
From: https://www.cnblogs.com/zt19994/p/17419004.html

相关文章

  • 软件开发与创新——原型设计工具介绍
    这周软件开发与创新的实验课上老师向我们初步介绍了软件开发的原型设计工具,经过查询相关资料,我了解到软件开发原型设计工具在开发过程中起着重要的作用。以下是几个主要作用:界面设计和布局:原型设计工具允许开发人员创建应用程序的用户界面,包括页面布局、组件和元素的放置等。它......
  • 桥接模式(Bridge Pattern)
    模式动机桥接模式(BridgePattern)是一种很实用的结构型模式,如果系统中某个类存在两个独立变化的维度,通过该模式可以将这两个维度分离出来,使得两者可以独立扩展。桥接模式用一种巧妙的方式处理多层继承存在的问题,用抽象关联取代了传统的多重继承,将类之间的静态继承关系转换为动态的......
  • 主流原型设计工具介绍
    原型设计是将想法转变为设计过程中十分重要的环节,而原型工具允许我们在设计过程中快速创建交互式模型,模拟应用程序的功能和流程。当下原型工具种类繁多,下面将介绍几种主流的原型设计工具及其使用。 一.AxureRP AxureRP是美国AxureSoftwareSolution公司旗舰产品,是......
  • 主流原型设计工具介绍
    AxureRP:AxureRP是一款功能强大、专业水平较高的原型设计工具,它的核心特点如下:高度交互性:AxureRP可以轻松地创建复杂的交互,比如各种复杂的表单交互和动态操作。实时数据模拟:AxureRP可以与现有的数据库进行连接,从而可以实时模拟动态数据。多设备适配:AxureRP提供了......
  • 一些原型设计工具简介
    原型设计是UI/UX设计中至关重要的一步,就像用户体验中的其他环节一样,有数不清的原型工具可以帮你完成原型设计。01PixsoPixso是一个集多种功能于一身的在线设计工具,可以完成从线框图、原型设计、UI设计到协作、设计系统管理、开发人员交付等各种环节。使用Pixso制作原型......
  • cpp: Facade Pattern
     /*****************************************************************//***\fileGoldGraphic.h*\briefFacadePattern外观模式*涂聚文GeovinDuVisualStudio2022edit.*\authorgeovindu*\dateMay2023*************************************......
  • cpp: Singleton Pattern
     //GoldConfig.h:此文件包含"GoldConfig"类。装饰器模式SingletonPatternC++14////SingletonPattern单例模式单件模式、Singleton//2023年5月20日涂聚文GeovinDuVisualStudio2022edit.#pragmaonce#ifndefGOLDCONFIG_H#defineGOLDCONFIG_H#i......
  • 从零玩转设计模式之原型模式-yuanxingmoshi
    title:从零玩转设计模式之原型模式date:2022-12-1120:05:35.488updated:2022-12-2315:35:44.159url:https://www.yby6.com/archives/yuanxingmoshicategories:-设计模式tags:-设计模式-原型模式什么是原型模式设计模式?原型模式是一种软件设计模式,它允许您......
  • 主流原型设计工具介绍
    一、原型设计工具的使用方法与特点对比原型工具使用方法特点墨刀使用者可以通过创建项目、添加页面、设计界面和设置交互来快速设计应用程序和网页原型。它具有实时协作功能,支持多人同时编辑和评论,方便团队协作。墨刀还提供丰富的交互设计选项,可以创建出丰富的交互......
  • 主流原型设计工具介绍
    什么是原型?即把系统主要功能和接口通过快速开发制作为“软件样机”,以可视化的形式展现给用户,及时征求用户意见,从而明确无误地确定用户需求。同时,原型也可用于征求内部意见,作为分析和设计的接口之一,可方便于沟通。原型设计工具:原型设计工具可以更好地明确需求,发现设计......