面向对象设计模式:创建型模式之原型模式
创始人
2024-05-30 20:46:58
0

文章目录

      • 一、引入
      • 二、代理模式,Prototype Pattern
        • 2.1 Intent 意图
        • 2.2 Applicability 适用性
        • 2.3 类图
        • 2.4 应用实例:使用下划线或消息框展示字符串
        • 2.4 应用实例:JDK java.lang.Object java.lang.Cloneable

一、引入

二、代理模式,Prototype Pattern

代理模式实现了一个原型接口用于创建当前对象的克隆。

2.1 Intent 意图

  • Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. 使用原型实例指定要创建的对象类型,并通过克隆此原型来创建新对象

2.2 Applicability 适用性

  • When a system should be independent of how its products are created, composed, and represented. 当一个系统应该独立于它的产品创建,构成和表示时
  • When the classes to instantiate are specified at run-time, for example, by dynamic loading. 当要实例化的类是在运行时刻指定时,例如,通过动态装载.
  • To avoid building a class hierarchy of factories that parallels the class hierarchy of products. 为了避免创建一个与产品类层次平行的工厂类层次时. (工厂模式介绍)
  • When instances of a class can have one of only a few different combinations of state. 当一个类的实例只能有几个不同状态组合中的一种时.

2.3 类图

在这里插入图片描述

  • Prototype: Declares an interface for cloning itself. 声明一个用于克隆自身的接口
  • ConcretePrototype: Implements an operation for cloning itself. 实现一个克隆自身的操作

2.4 应用实例:使用下划线或消息框展示字符串

To show a string with underline or message box:
在这里插入图片描述

  • Conventionally, we have to design three different classes to show a string with underline or message box
  • However, we will design too many classes to show different underline or message box styles

  • Prototype:
public interface Product extends Cloneable {public abstract void use(String s);public abstract Product createClone();
}
public class MessageBox implements Product {private char decochar;public MessageBox(char decochar) {this.decochar = decochar;}public void use(String s) {int length = s.getBytes().length;for (int i = 0; i < length + 4; i++) {System.out.print(decochar);}System.out.println("");System.out.println(decochar + " "  + s + " " + decochar);for (int i = 0; i < length + 4; i++) {System.out.print(decochar);}System.out.println("");}public Product clone() {Product p = null;try {p = (Product) super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return p;}
}
public class UnderlinePen implements Product {private char ulchar;public UnderlinePen(char ulchar) {this.ulchar = ulchar;}public void use(String s) {int length = s.getBytes().length;System.out.println("\""  + s + "\"");System.out.print(" ");for (int i = 0; i < length; i++) {System.out.print(ulchar);}System.out.println("");}public Product clone() {Product p = null;try {p = (Product) super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return p;}
}
  • Manager:
public class Manager {private HashMap showcase = new HashMap();public void register(String name, Product proto) {showcase.put(name, proto);}public Product create(String protoname) {Product p = (Product) showcase.get(protoname);return p.clone();}
}
  • ClientDemo
public class Main {public static void main(String[] args) {// 准备Manager manager = new Manager();UnderlinePen upen = new UnderlinePen('~');MessageBox mbox = new MessageBox('*');MessageBox sbox = new MessageBox('/');manager.register("strong message", upen);manager.register("warning box", mbox);manager.register("slash box", sbox);// 生成Product p1 = manager.create("strong message");p1.use("Hello, world.");Product p2 = manager.create("warning box");p2.use("Hello, world.");Product p3 = manager.create("slash box");p3.use("Hello, world.");}
}

在这里插入图片描述

2.4 应用实例:JDK java.lang.Object java.lang.Cloneable

java.lang.Object
在这里插入图片描述

相关内容

热门资讯

美团外卖怎么投诉 有效方法步骤... 点外卖时,我们偶尔也会与商家或者骑手发生不愉快。有时候只是一点小事,却因为彼此不好的态度而无法释怀,...
民生易贷是正规贷款吗?民生易贷...   民生助粒贷是属于民生易贷旗下运营的一款针对个人消费开放的信贷产品这款产品自上市以来就十分稳定,审...
灵活就业养老保险交哪个档次好?...   众所周知,灵活就业人员缴纳的养老保险有着不同的档次,选择不同的档次也意味着不同的投入和回报,那么...
微信怎么查医保卡余额,如何在微...   很多小伙伴都想要查询自己的社保卡的余额信息,但是去线下查询慢效率不高。但是其实在手机微信上就可以...
社保断交有什么影响?社保断交影...   很多小伙伴都知道社保需要每个月都交,但是社保断缴会怎么样呢?如果社保断缴会导致什么严重的后果吗?...
每年交7000社保15年领多少...   退休金是很多老人之后生活的凭据,那么自己缴纳十五年的社保,每年都交7000元,那么在退休之后能领...
退休金上调2022的最新消息,...   进入8月份,2022年的退休人员的养老金上涨,那么这一部分调整了什么内容呢?这是很多小伙伴啊都存...
公积金封存满6个月当天就能取?...   公积金是很多小伙伴在工作后都会交的,那么公积金封存后满6个月可以取吗?公积金封存之后可以提现吗?...
67类“示范文本”推广使用!对... 转自:新华社客户端“机动车交通事故责任纠纷中,受害人对于具体赔偿项目通常不清楚,示范文本将常见的12...
延迟退休年龄一览表2022,延...   现在延迟退休将于2022年开始启动,预计将在2027年实施,那么很多小伙伴就存在着疑惑了,不知道...