上海网站建设 推荐站霸网络,三合一网站是什么,怎么在地图上设置自己店的位置,外贸网站建设需要什么说明#xff1a;说明#xff1a;本文介绍设计模式中结构型设计模式中的#xff0c;享元模式#xff1b;
游戏地图
在一些闯关类的游戏#xff0c;如超级玛丽、坦克大战里面#xff0c;游戏的背景每一个关卡都不相同#xff0c;但仔细观察可以发现#xff0c;其都是用…说明说明本文介绍设计模式中结构型设计模式中的享元模式
游戏地图
在一些闯关类的游戏如超级玛丽、坦克大战里面游戏的背景每一个关卡都不相同但仔细观察可以发现其都是用一些基础图标组成的背景的变化实际上就是改变了其基础图标的位置。
如坦克大战游戏背景实际上就是通过敌方坦克、我方坦克、砖墙、铁墙、海洋、草丛、基地等这些图标组成的。 基础图标坦克、草地、河流…… 现在如果要我们来开发这样一个游戏地图。首先我们会创建一个地图块类如下
Tile图块类
/*** 图块类*/
public class Tile {/*** 图块名称*/private String image;/*** 图块的x坐标*/private int x;/*** 图块的y坐标*/private int y;/*** 创建图块对象*/public Tile(String image, int x, int y) {this.image image;this.x x;this.y y;System.out.println(加载图片 image);}/*** 绘制图块*/public void draw() {System.out.println(绘制图片 image 坐标 x , y);}
}Client客户端演示游戏地图创建过程
/*** 客户端*/
public class Client {public static void main(String[] args) {// 在地图上绘制两个“海洋”图块new Tile(海洋, 1, 2).draw();new Tile(海洋, 2, 3).draw();// 在地图上绘制两个“草丛”图块new Tile(草丛, 1, 3).draw();new Tile(草丛, 2, 4).draw();// 在地图上绘制两“砖墙”图块new Tile(砖墙, 1, 4).draw();new Tile(砖墙, 2, 5).draw();}
}运行程序加载游戏地图 分析以上过程会发现问题很大。每次创建一个图块对象都需要new加载时间特别长且浪费资源。
为了解决这个问题我们似乎可以考虑用原型设计模式解决。但是我们需要考虑对于一个游戏地图来说图块种类的数量可能是非常庞大的使用原型模式对对象进行克隆会有大量的内存开销如果没有良好的内存回收机制可能会导致内存溢出造成系统崩溃。因此使用原型模式来解决游戏地图的设计不一定是合适的。
享元模式
享元元指的是最小的单元也就是上面坦克大战中草丛、海洋这些最小的图块享元就是共享这些最小图块。通过享元模式对上面地图加载代码改进如下
Drawable绘画接口
/*** 绘画接口*/
public interface Drawable {/*** 绘制图块*/void draw(int x, int y);
}Grass草地图块
/*** 草地图块*/
public class Grass implements Drawable {/*** 图块名称*/private String name;public Grass() {this.name 草地;System.out.println(创建图块 name);}Overridepublic void draw(int x, int y) {System.out.println(绘制【 name 】图块 name 位置( x , y ));}
}Wall墙壁图块
/*** 墙壁图块*/
public class Wall implements Drawable {/*** 图块名称*/private String name;public Wall() {this.name 墙壁;System.out.println(创建墙壁图块 name);}Overridepublic void draw(int x, int y) {System.out.println(绘制【 name 】图块 name 位置( x , y ));}
}River河流图块
/*** 河流图块*/
public class River implements Drawable {/*** 图块名称*/private String name;public River() {this.name 河流;System.out.println(创建图块 name);}Overridepublic void draw(int x, int y) {System.out.println(绘制【 name 】图块 name 位置( x , y ));}
}Home基地图块
/*** 基地图块*/
public class Home implements Drawable {/*** 基地图块名称*/private String name;public Home() {this.name 基地;System.out.println(创建图块 name);}Overridepublic void draw(int x, int y) {System.out.println(绘制【 name 】图块 name 位置( x , y ));}
}TileFactory图块工厂
import java.util.HashMap;
import java.util.Map;/*** 图块工厂*/
public class TileFactory {/*** 图块集合*/private MapString, Drawable Tiles;/*** 图块工厂构造函数*/public TileFactory() {Tiles new HashMapString, Drawable();}/*** 根据图块名称获取图块没有就创建并返回有就直接从集合中获取并返回*/public Drawable getTile(String name) {// 没有就创建if (!Tiles.containsKey(name)) {switch (name) {case 河流:Tiles.put(name, new Wall());break;case 草地:Tiles.put(name, new Grass());break;case 基地:Tiles.put(name, new Home());break;case 墙壁:Tiles.put(name, new Wall());break;default:break;}}// 有就直接返回return Tiles.get(name);}
}Client客户端演示游戏地图加载过程
/*** 客户端*/
public class Client {public static void main(String[] args) {// 创建图块工厂TileFactory tileFactory new TileFactory();// 绘制地图tileFactory.getTile(草地).draw(0, 0);tileFactory.getTile(草地).draw(1, 0);tileFactory.getTile(草地).draw(2, 0);tileFactory.getTile(河流).draw(0, 1);tileFactory.getTile(河流).draw(1, 1);tileFactory.getTile(墙壁).draw(0, 2);tileFactory.getTile(墙壁).draw(1, 2);tileFactory.getTile(基地).draw(0, 3);}
}可以发现各个图块构造器内的输出语句只执行了一次说明后续图块的创建没有调用其构造方法实现了享元 以上就是通过享元模式对游戏地图设计的过程实际上就是对最小单元对象的共享使其不重复去创建对象。
需要注意区分于工厂设计模式工厂模式是创建型设计模式关注对象的创建而享元模式是对已创建对象的一种利用是结构型设计模式。
总结
本文参考《设计模式的艺术》、《秒懂设计模式》两书 文章转载自: http://www.morning.xrksf.cn.gov.cn.xrksf.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.cnlmp.cn.gov.cn.cnlmp.cn http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.flhnd.cn.gov.cn.flhnd.cn http://www.morning.rzcfg.cn.gov.cn.rzcfg.cn http://www.morning.gpryk.cn.gov.cn.gpryk.cn http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.jpwkn.cn.gov.cn.jpwkn.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.kpqjr.cn.gov.cn.kpqjr.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.lsgjf.cn.gov.cn.lsgjf.cn http://www.morning.fqcdh.cn.gov.cn.fqcdh.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn