当前位置: 首页 > article >正文

Flyweight(享元)设计模式 软考 享元 和 代理属于结构型设计模式

1.目的:运用共享技术有效地支持大量细粒度的对象

Flyweight(享元)设计模式 是一种结构型设计模式,它的核心目的是通过共享对象来减少内存消耗,特别是在需要大量相似对象的场景中。Flyweight 模式通过将对象的共享细节与不变的部分分离,尽量减少系统中对象的数量,进而提高系统的性能。

主要问题:

如果一个系统中有大量相似的对象,它们的状态可能大部分是相同的。每个对象都会占用内存,并且在程序中创建大量相似对象会导致内存浪费和性能下降。Flyweight 模式的解决方案是将这些相似对象中共享的部分提取出来,集中存储共享,而把变动的部分存储在客户端。

主要组成部分:

  1. Flyweight(享元)接口:定义享元对象的行为。享元对象通常是不可变的(immutable),因此它们可以安全地在多个客户端之间共享。

  2. ConcreteFlyweight(具体享元):实现了 Flyweight 接口,存储共享的状态。

  3. FlyweightFactory(享元工厂):负责管理享元对象的创建和共享,确保每个享元对象都被重复使用。

  4. UnsharedConcreteFlyweight(非共享的具体享元):不共享的享元对象,通常用于保存可变状态(例如,特定于客户端的状态)。

示例:

假设我们有一个系统,需要表示大量的 棋盘。棋盘上每个格子都有颜色、位置等属性,但在同一个棋盘上,很多格子可能具有相同的颜色或样式。我们可以使用 Flyweight 模式来优化内存消耗,减少重复的对象实例。

代码示例:

// 享元接口
interface ChessPiece {void draw();
}// 具体享元类
class ConcreteChessPiece implements ChessPiece {private String color; // 享元的共享部分public ConcreteChessPiece(String color) {this.color = color;}@Overridepublic void draw() {System.out.println("Drawing " + color + " chess piece.");}
}// 享元工厂
class ChessPieceFactory {private Map<String, ChessPiece> chessPieces = new HashMap<>();public ChessPiece getChessPiece(String color) {ChessPiece piece = chessPieces.get(color);if (piece == null) {piece = new ConcreteChessPiece(color);
// 给向享元工厂, put对象,下一次就不用判断为空了, 直接从工程里取出来就值chessPieces.put(color, piece);System.out.println("Creating new " + color + " chess piece.");}return piece;}
}// 客户端代码
public class FlyweightPatternDemo {public static void main(String[] args) {ChessPieceFactory factory = new ChessPieceFactory();ChessPiece whitePiece1 = factory.getChessPiece("White");whitePiece1.draw();ChessPiece blackPiece1 = factory.getChessPiece("Black");blackPiece1.draw();ChessPiece whitePiece2 = factory.getChessPiece("White");whitePiece2.draw();ChessPiece blackPiece2 = factory.getChessPiece("Black");blackPiece2.draw();ChessPiece whitePiece3 = factory.getChessPiece("White");whitePiece3.draw();}
}运行结果
Creating new White chess piece.
Drawing White chess piece.
Creating new Black chess piece.
Drawing Black chess piece.
Drawing White chess piece.
Drawing Black chess piece.
Drawing White chess piece.

http://www.lryc.cn/news/2384790.html

相关文章:

  • Win/Linux安装flash attention2
  • 【原创】ubuntu22.04下载编译AOSP 15
  • 服务器网络配置 netplan一个网口配置两个ip(双ip、辅助ip、别名IP别名)
  • 响应面法(Response Surface Methodology ,RSM)
  • 针对面试-java集合篇
  • Spring Boot 拦截器:解锁5大实用场景
  • 展锐 Android 15 锁定某个App版本的实现
  • 有两个Python脚本都在虚拟环境下运行,怎么打包成一个系统服务,按照顺序启动?
  • 【Linux cmd】查找进程信息
  • 与网格共舞 - 服务网格的运维与问题排查 (Istio 实例)
  • Python 脚本执行命令的深度探索:方法、示例与最佳实践
  • PotPlayer 4K 本地万能影音播放器
  • 2025年电工杯A题第一版本Q1-Q4详细思路求解+代码运行
  • 基于阿里云DashScope API构建智能对话指南
  • HOW - 基于组件库组件改造成自定义组件基本规范
  • 九州未来十三载:开源赋能 智启未来
  • 2025年AI搜索引擎发展洞察:技术革新与市场变革
  • dify调用Streamable HTTP MCP应用
  • HCIP实验五
  • java将图片转Base64字符串存储mysql数据库
  • 题目 3330: 蓝桥杯2025年第十六届省赛真题-01 串
  • 初识 Flask 框架
  • MYSQL故障排查和环境优化
  • vivado fpga程序固化
  • OpenCV CUDA模块图像特征检测与描述------图像中快速检测特征点类cv::cuda::FastFeatureDetector
  • SpringMVC(结合源码浅析工作流程)
  • 学习STC51单片机13(芯片为STC89C52RC)
  • Claude 4 系列 Opus 4 与 Sonnet 4正式发布:Claude 4新特性都有哪些?
  • Swagger API 未授权访问漏洞【原理扫描】修复
  • 深度“求索”:DeepSeek+Dify构建个人知识库