蝇量模式(Flyweight Pattern)
蝇量模式:让某个类的一个实例能用来提供许多“虚拟实例”。
在有大量对象时,有可能造成内存溢出,把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重复创建。(JAVA中的String,如果没有则创建一个字符串保存在字符串常量池里,否则直接返回)
类图:

public interface Planet {
public abstract void display(int x, int y);
}
public class Tree implements Planet {
private int x;
private int y;
private String height;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Tree(String height) {
this.height = height;
}
@Override
public void display(int x, int y) {
System.out.println(height + "Tree in x=" + x + " y=" + y);
}
}
public class PlanetFactory {
private static final HashMap<String, Planet> planetMap = new HashMap<String, Planet>();
public static Planet getTree(String height) {
Tree tree = (Tree) planetMap.get(height);
if(tree == null) {
tree = new Tree(height);
planetMap.put(height, tree);
System.out.println("Creating tree of height : " + height);//只有在map中找不到对象才创建一个,将这句话打印出来
}
return tree;
}
}
public class Client {
private static final String height[] = {"Tall", "Medium", "Short"};
private static String getRandomHeight() {
return height[(int) (Math.random() * height.length)];
}
private static int getRandomX() {
return (int) (Math.random() * );
}
private static int getRandomY() {
return (int) (Math.random() * );
}
public static void main(String[] args) {
for(int i = ; i < ; i++) {
Tree tree = (Tree) PlanetFactory.getTree(getRandomHeight());
tree.display(getRandomX(), getRandomY());
}
}
}
某次测试结果:
Creating tree of height : Tall
TallTree in x=57 y=88
TallTree in x=10 y=24
Creating tree of height : Short
ShortTree in x=8 y=61
ShortTree in x=92 y=27
ShortTree in x=58 y=73
TallTree in x=22 y=25
ShortTree in x=95 y=3
Creating tree of height : Medium
MediumTree in x=0 y=1
MediumTree in x=95 y=38
MediumTree in x=35 y=56
TallTree in x=67 y=5
MediumTree in x=35 y=47
ShortTree in x=37 y=32
ShortTree in x=40 y=94
MediumTree in x=43 y=11
MediumTree in x=4 y=94
ShortTree in x=68 y=70
ShortTree in x=62 y=56
ShortTree in x=13 y=69
MediumTree in x=21 y=49
蝇量模式(Flyweight Pattern)的更多相关文章
- JAVA设计模式:蝇量模式
声明:转载请说明来源:http://www.cnblogs.com/pony1223/p/7554686.html 一.引出蝇量模式 现在假设有一个项目,这个项目是为公园设计一个景观的部署,那么这个时 ...
- 二十四种设计模式:享元模式(Flyweight Pattern)
享元模式(Flyweight Pattern) 介绍运用共享技术有效地支持大量细粒度的对象. 示例有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要运用共享技术支 ...
- 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern)
原文:乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) 作者:weba ...
- Java享元模式(Flyweight Pattern)
享元模式(Flyweight Pattern)主要用于减少创建的对象数量,并减少内存占用并提高性能. 这种类型的设计模式属于结构模式,因为该模式提供了减少对象计数的方法,从而改善应用的对象结构. 享元 ...
- 设计模式-11享元模式(Flyweight Pattern)
1.模式动机 在面向对象程序设计过程中,有时会面临要创建大量相同或相似对象实例的问题.创建那么多的对象将会耗费很多的系统资源,它是系统性能提高的一个瓶颈. 享元模式就是把相同或相似对象的公共部分提取出 ...
- 设计模式系列之享元模式(Flyweight Pattern)——实现对象的复用
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- Head First设计模式之享元模式(蝇量模式)
一.定义 享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能.这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式. ...
- php享元模式(flyweight pattern)
周日一大早,今天要送老婆孩子去火车站, 所以练代码要早点哈. <?php /* The flyweight pattern is about performance and resource r ...
- 享元模式(Flyweight Pattern)
定义: 采用一个共享来避免大量拥有相同内容对象的开销.这种开销中最常见.直观的就是内存的损耗.享元模式以共享的方式高效的支持大量的细粒度对象. 享元的英文是flyweight,是一个来自体育方面的专业 ...
随机推荐
- IDF实验室-简单的ELF逆向 writeup
题目:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=39 下载得到ElfCrackMe1文件,直接用IDA打开 ...
- 创建性能监视器(logman)
在本地计算机上抓取性能信息 Logman.exe create counter Perf-1Second -f bincirc -max 500 -c "\Processor(*)\% Pr ...
- Project Management - 2) Estimate Your Work
1. Delphi估算 1) 项目经理召集小组成员,就项目有关事宜向大家进行阐述. 2)大家提出各种问题,然后进行分配任务. 每个人写下自己的任务列表和时间估算. 3)项目经理收集好任务之后进行复查, ...
- Ubuntu安装和配置redis
1.用root用户登录 2.执行 sudo apt-get install redis-server 部分截图
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication的解决方法
直接命令行操作没有问题,但是PHP连接就会报上面的错误. SET old_passwords =0; USE mysql; UPDATE user SET password =PASSWORD('yo ...
- Qualcomm Web Site For Android Development
https://www.codeaurora.org/xwiki/bin/QAEP/release https://support.cdmatech.com/login/ https://chipco ...
- [Webpack] Use the Webpack Dashboard to Monitor Webpack Operations
Learn how to use the new Webpack Dashboard from Formidable Labs to display a pretty, useful output f ...
- mysqldump原理4
http://blog.csdn.net/dba_waterbin/article/details/23611601?utm_source=tuicool&utm_medium=referra ...
- wcf自身作为宿主的一个小案例
第一步:创建整个解决方案 service.interface:用于定义服务的契约(所有的类的接口)引用了wcf的核心程序集system.ServiceModel.dll service:用于定义服务类 ...
- springMVC学习笔记三
十三.springMVC和spring集成 配置文件,spring的配置路径applicationContext.xml 在默认的web-inf下面 strut的配置文件默认在src下面 用了什么框架 ...