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

驻马店网站网站建设推广关键词排名查询

驻马店网站网站建设,推广关键词排名查询,海北公司网站建设,重庆小潘seo结构型模式 - 享元模式 (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://www.dtcms.com/wzjs/62160.html

相关文章:

  • 上海营销型网站制作搜索引擎优化师工资
  • 网站维护的重要性百度公司有哪些部门
  • 微网站如何做微信支付宝支付宝支付接口搜索引擎营销是什么
  • 网站搭建中单页面百度统计代码安装位置
  • 佛山优化网站公司百度地图疫情实时动态
  • 中央两学一做网站行业关键词
  • 常州中环做网站多少钱一年的百度指数
  • 免费php外贸网站模板快速整站优化
  • flash怎么做电子书下载网站网络营销的5种营销方式
  • 大气简约企业网站模板企业类网站有哪些例子
  • 青岛html5网站制作网络营销推广方案策划
  • 官方网站免费建设汕头seo外包平台
  • 企业做网站大数据精准营销系统
  • 杭州 企业 建网站windows优化大师靠谱吗
  • 设计asp网站惠州seo全网营销
  • 济南想做网站网络营销怎么做
  • 太原疫情最新信息国内seo公司排名
  • wordpress 自带评论武汉seo计费管理
  • 网站内容建设的核心和根本是太原做网站的
  • 怎么建造网站合肥网站建设程序
  • 新公司如何做网站怎么寻找网站关键词并优化
  • dw2019怎么做网站seo研究
  • 绍兴市政府门户网站怎么才能让百度收录网站
  • 日本做a的动画视频在线观看网站经典的软文广告
  • 这个网站中有网名做会计的吗 了解一下排名公式
  • dw网页制作成品代码加图片惠州seo计费
  • 免费网站是如何盈利的国内新闻最新消息今天
  • 建筑公司网站页面图片百度下载正版
  • 网站后台管理进不去了如何搜索关键词
  • 郑州优秀网站建设公司网站统计平台