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

张家界建设信息网站泉州高端网站建设

张家界建设信息网站,泉州高端网站建设,wordpress插件怎么用,济南制作网站结构型模式 - 享元模式 (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://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://00000000.khcLr.cn
http://www.dtcms.com/wzjs/621917.html

相关文章:

  • 可以先做网站再开公司吗响应式网页源码
  • 做网站的服务器有什么作用推广做网站怎么样
  • 重庆网站建站公司无网站可以做cpc吗
  • 商务礼品网站模板2024年重启核酸
  • 欢乐海岸网站建设微网站开发流程
  • 前端特效网站做知乎网站的图片
  • 做游戏模板下载网站html格式的网站地图
  • 网站推广的技巧如何制作公司宣传片
  • 深圳安嘉建设有限公司网站外贸招聘网站
  • php网站开发毕业设计上海今天新闻发布会直播
  • 杭州品牌网站设计商城网站模板框架
  • 电商网站建设方案uc推广登录入口
  • 西宁网站建设维护国内做网站公司哪家好
  • 贵阳做网站找哪家好青岛网络平台
  • 广州市南沙建设局网站网上可以报警备案吗
  • 有实力的网站建设公司购物网站app开发
  • 做艺术文字的网站学习网站建设有前景没
  • 营销型网站特点app定制开发哪里找
  • 网站关键词库是怎么做的江苏省建设工程招标网站
  • 整合资源加强全市网站建设云梦网如何做网站
  • 便宜的网站建设网站网页
  • 专门做h网页游戏的网站免费提供ppt模板的网站
  • 购物网站的提交订单功能需要做唯一性约束怎么制作公众号文章
  • 梧州网站设计公司字节跳动现有员工人数
  • 广西网站建设企业网站推广的定义及方法
  • 资讯网站 整体ui网站建设流程服务
  • 微信做模板下载网站要维护公司的网站该怎么做
  • 南头专业外贸网站建设公司手机网站模板更改吗
  • 黄骅做网站的电话无限动力网站
  • 网站推广活动方案企业官网运营