面向对象设计模式:创建型模式之原型模式
创始人
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
在这里插入图片描述

相关内容

热门资讯

巴基斯坦总理与巴各政党领导人通... 【环球网快讯】印巴局势升级引发广泛关注。据巴基斯坦《黎明报》10日援引巴基斯坦国家电视台PTV最新消...
这一天!邯郸市中心医院…… 转自:邯郸新闻网邯郸道 天使情——邯郸市中心医院的特色护士节活动编者按:在国际护士节来临之际,邯郸市...
泰国4月份通胀率现负增长 转自:中国新闻网  中新社曼谷5月10日电 据泰国《民族报》10日报道,泰国4月份通货膨胀率为-0....
5月12日现场对接!辽宁省工信... 原标题:需求清单,向全球发布!  需求是市场,是合作的开始。近日,省工业信息化厅围绕重点产业链条、工...
特朗普高兴早了!首位美籍罗马教... 当地时间5月8日,枢机主教罗伯特·普雷沃斯特当选第267任天主教罗马教皇,称为利奥十四世,成为首位美...
马光远谈中国文化IP:中国每一...   2025世界IP经济发展大会暨全球IP授权博览会将于5月10日-5月12日在广州举行。著名经济学...
媒体评:学术近亲繁殖伤害教育公... 【媒体评:#学术近亲繁殖伤害教育公平#】近日,因发表14篇SCI论文,并获得3项国家发明专利,重庆大...
熟练到脑子跟不上嘴!护理专业男... 【熟练到脑子跟不上嘴!#护理专业男生丝滑操作婴儿急救#[666]】5月9日,四川一护理专业男同学,在...
深化改革开放再发力 国务院部署... 深化对外开放,国务院又有新部署。5月9日,国务院常务会议听取推动自贸试验区建设提质增效工作汇报,并研...
46岁章子怡,突传喜讯! 今天,章子怡工作室发文官宣好消息:《卧虎藏龙》25周年之际,章子怡在GoldGala出席奥斯卡博物馆...
面板双虎4月营收持平,Q2电视... 来源:Wit Display面板双虎友达光电今天公布4月合并营收新台币231.4亿元新台币,较3月减...
16楼“飞”出电竞椅!原因查明... 据四川成都市公安局高新技术产业开发区分局消息近日成都高新区某小区发生了一起高空抛物事件一男子情绪失控...
富采Q1亏1亿! 来源:Wit DisplayLED大厂富采5月9日举行法说会,公布2025年第1季财报。尽管单季仍呈...
小市值、反转因子表现较好   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! 因子IC跟踪    ...
全球专家共话科技与产业融合的脑... 5月10日,“2025浦江创新论坛”第七届神经科技国际创新论坛暨中国神经科学学会神经调控基础与转化分...
璀璨夺目!这场焰火展演点亮南昌... 转自:南昌发布5月9日晚,随着暮色降临,“星焰焕新·城启璀璨”——中国南昌烟花爆竹产业博览会城市焰火...
5月楼市开局冷热分化 公积金利... (转自:地产红榜)  5月1日国家新版《住宅项目规范》正式实施。从“五一”假期楼市表现来看,分化明显...
清华大学夏清:新型电力系统的构...   5月10日消息,远东控股集团40周年庆暨企业家论坛今天在宜兴举行。清华大学电机系教授、国家能源互...
十二载从无到有 海军舰载航空兵... 今天(5月10日)是人民海军首支舰载航空兵部队成立12周年的日子。12年前,舰载航空兵部队在渤海湾畔...
今天,该如何看尖岗山? 来源:苗头有朋友打来电话,说完全同意我的意见,全款买了一套万科未来之光80平方米的房子。有时候,你不...