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

张家界建设信息网站免费免费建站

张家界建设信息网站,免费免费建站,广州市口碑全网推广报价,龙岩网站建设论坛结构型模式 - 享元模式 (Flyweight Pattern) 享元模式是一种结构型设计模式,它通过共享对象来减少内存使用和提高性能,尤其适用于存在大量细粒度对象且这些对象有部分状态可共享的场景。 举个经典案例, 下围棋, 黑子和白子可以用享元,每一颗棋子除了放置…

结构型模式 - 享元模式 (Flyweight Pattern)

享元模式是一种结构型设计模式,它通过共享对象来减少内存使用和提高性能,尤其适用于存在大量细粒度对象且这些对象有部分状态可共享的场景。


举个经典案例, 下围棋, 黑子和白子可以用享元,每一颗棋子除了放置的位置之外,只有颜色区分.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;// 享元接口,定义棋子的放置方法
interface ChessPieceFlyweight {String getColor();
}// 具体享元类,代表黑子和白子
class ConcreteChessPiece implements ChessPieceFlyweight {private String color;public ConcreteChessPiece(String color) {this.color = color;}@Overridepublic String getColor() {return color;}
}// 享元工厂类,负责创建和管理享元对象
class ChessPieceFactory {private static final Map<String, ChessPieceFlyweight> flyweights = new HashMap<>();public static ChessPieceFlyweight getChessPiece(String color) {ChessPieceFlyweight flyweight = flyweights.get(color);if (flyweight == null) {flyweight = new ConcreteChessPiece(color);flyweights.put(color, flyweight);}return flyweight;}
}// 表示棋盘上的位置
class Position {private int x;private int y;public Position(int x, int y) {this.x = x;this.y = y;}public int getX() {return x;}public int getY() {return y;}@Overridepublic String toString() {return "(" + x + ", " + y + ")";}
}// 记录棋子及其位置的类
class PlacedChessPiece {private ChessPieceFlyweight piece;private Position position;public PlacedChessPiece(ChessPieceFlyweight piece, Position position) {this.piece = piece;this.position = position;}public ChessPieceFlyweight getPiece() {return piece;}public Position getPosition() {return position;}@Overridepublic String toString() {return piece.getColor() + " 棋子放置在位置 " + position;}
}// 客户端代码
public class GoGameExample {public static void main(String[] args) {// 存储所有已放置的棋子List<PlacedChessPiece> placedPieces = new ArrayList<>();// 获取黑子和白子对象ChessPieceFlyweight blackPiece = ChessPieceFactory.getChessPiece("黑色");ChessPieceFlyweight whitePiece = ChessPieceFactory.getChessPiece("白色");// 放置黑子Position blackPos1 = new Position(1, 2);Position blackPos2 = new Position(3, 4);placedPieces.add(new PlacedChessPiece(blackPiece, blackPos1));placedPieces.add(new PlacedChessPiece(blackPiece, blackPos2));// 放置白子Position whitePos1 = new Position(2, 3);Position whitePos2 = new Position(4, 5);placedPieces.add(new PlacedChessPiece(whitePiece, whitePos1));placedPieces.add(new PlacedChessPiece(whitePiece, whitePos2));// 输出所有已放置的棋子及其位置for (PlacedChessPiece placedPiece : placedPieces) {System.out.println(placedPiece);}}
}

文章转载自:

http://tTbunCff.ryywf.cn
http://Jl4H9lrC.ryywf.cn
http://KSDfZIy2.ryywf.cn
http://LgZV5lAH.ryywf.cn
http://5JXYTeCA.ryywf.cn
http://BaEeEX0i.ryywf.cn
http://eKqoeZSr.ryywf.cn
http://JfviJAVi.ryywf.cn
http://H7f1Yjrm.ryywf.cn
http://0V15QVSe.ryywf.cn
http://ywwNeOJk.ryywf.cn
http://byhQJofv.ryywf.cn
http://Sg2CJKwm.ryywf.cn
http://Z9MeXzUJ.ryywf.cn
http://zMdewrMR.ryywf.cn
http://g3zNzyzd.ryywf.cn
http://3VRsCAXs.ryywf.cn
http://KY8REJ5B.ryywf.cn
http://ex8MGHHf.ryywf.cn
http://G08OUJNt.ryywf.cn
http://Ps87cZxD.ryywf.cn
http://Rh6GEbZU.ryywf.cn
http://WqooYvRr.ryywf.cn
http://lUuKWppy.ryywf.cn
http://oikDWAlg.ryywf.cn
http://OXObUn0r.ryywf.cn
http://ouNyp6Cl.ryywf.cn
http://pE8Prz1B.ryywf.cn
http://M6etTInS.ryywf.cn
http://giFTFxYz.ryywf.cn
http://www.dtcms.com/wzjs/621602.html

相关文章:

  • 旅游网站开发需求文档模板云虚拟主机怎么建网站
  • 如何让移动网站更优秀网站弹出窗口js
  • 北仑网站建设培训学校惠州网络推广哪里找
  • 企业网站报价方案下载网站开发初学
  • 建设网站的公司汇总wordpress直播购物插件
  • 珠海知名网站专业制作简历收费
  • 包头做网站上海 房地产网站建设
  • 公司网站做的好的公司企业网站优化技巧
  • 武隆网站建设费用关于加强网站建设的意见
  • 推荐一本学做网站的书网站怎么做能让人搜到
  • 做网站主要是做什么2345网址导航下载官网
  • 重新建设网站的报告泰安招聘网站有哪些
  • 广东网站建设网站汉中门户网
  • 微信公众号怎么创建文章多网站怎么做seo
  • 1688网站一起做网店单位网站怎么做
  • 厚街网站建设费用欢迎页网页设计作品欣赏
  • 云主机怎么装网站网站怎么做 流程图
  • 公司网站关键词优化分析网络营销的特点
  • 网站开发流程步骤枣庄网站建设枣庄
  • 网站域名与网站首页网址福建省住房建设厅网站
  • 网站建设的运营计划网络内容建设包括什么
  • 做网站需要那些软件濮阳做网站的公司
  • 装饰工程设计东莞网站建设如何找百度做网站
  • 国外源代码网站网站快照长期不更新
  • 网站建设推广语制作钓鱼网站的费用
  • 论文引用网站怎样做脚注吉林省建设厅网站
  • 青州网站建设青州my最新域名是什么
  • 商城平台网站开发深圳做网站关键词必须要中文网址
  • 英文WordPress站点切换为中文做企业的网站都要准备什么
  • 要学做游戏上什么网站学好网络工程师招聘