码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 设计模式-原型模式


            原型模式是一种创建型设计模式,它用于创建重复的对象,同时又能保证性能。这种类型的设计模式是基于一个原型实例,通过复制(克隆)自身来创建新的对象。

            以下是一个简单的 Java 版本的原型模式的示例:

    1. public abstract class Shape implements Cloneable {
    2. private String id;
    3. protected String type;
    4. abstract void draw();
    5. public String getType(){
    6. return type;
    7. }
    8. public String getId() {
    9. return id;
    10. }
    11. public void setId(String id) {
    12. this.id = id;
    13. }
    14. public Object clone() {
    15. Object clone = null;
    16. try {
    17. clone = super.clone();
    18. } catch (CloneNotSupportedException e) {
    19. e.printStackTrace();
    20. }
    21. return clone;
    22. }
    23. }
    24. public class Rectangle extends Shape {
    25. public Rectangle(){
    26. type = "Rectangle";
    27. }
    28. @Override
    29. public void draw() {
    30. System.out.println("Inside Rectangle::draw() method.");
    31. }
    32. }
    33. public class Square extends Shape {
    34. public Square(){
    35. type = "Square";
    36. }
    37. @Override
    38. public void draw() {
    39. System.out.println("Inside Square::draw() method.");
    40. }
    41. }
    42. public class ShapeCache {
    43. private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();
    44. public static Shape getShape(String shapeId) {
    45. Shape cachedShape = shapeMap.get(shapeId);
    46. return (Shape) cachedShape.clone();
    47. }
    48. public static void loadCache() {
    49. Rectangle rectangle = new Rectangle();
    50. rectangle.setId("1");
    51. shapeMap.put(rectangle.getId(), rectangle);
    52. Square square = new Square();
    53. square.setId("2");
    54. shapeMap.put(square.getId(), square);
    55. }
    56. }
    57. public class PrototypePatternDemo {
    58. public static void main(String[] args) {
    59. ShapeCache.loadCache();
    60. Shape clonedShape = (Shape) ShapeCache.getShape("1");
    61. System.out.println("Shape : " + clonedShape.getType());
    62. Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
    63. System.out.println("Shape : " + clonedShape2.getType());
    64. }
    65. }


            在这个示例中,Shape 是一个实现了 Cloneable 接口的抽象类,Rectangle 和 Square 是 Shape 的具体子类。ShapeCache 类负责存储和克隆原型对象。当需要一个新的 Shape 对象时,可以通过 ShapeCache.getShape 方法获取,这个方法会返回一个克隆的对象,而不是创建一个新的对象。

  • 相关阅读:
    Kotlin 协程之取消与异常处理探索之旅(上)
    UT代码编译至build文件夹
    [oeasy]python0020换行字符_feed_line_lf_反斜杠n_B语言_安徒生童话
    如何提升爬虫IP使用效率?精打细算的方法分享
    ORA-12514:TNS:监听程序当前无法识别链接描述符中请求的服务
    翻译: Transformer一种用于语言理解的新型神经网络架构 Google AI
    使用Spring Boot注册整合方式整合Servlet三大组件
    java 歌词解析 源代码, 在windows10下调试运行成功。
    计算机毕业设计选题推荐-基于数据可视化的智慧社区内网平台-Python项目实战
    【会议征稿通知】第二届数字化经济与管理科学国际学术会议(CDEMS 2024)
  • 原文地址:https://blog.csdn.net/m0_65014849/article/details/133905353
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号