蝇量模式:让某个类的一个实例能用来提供许多“虚拟实例”。

在有大量对象时,有可能造成内存溢出,把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重复创建。(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)的更多相关文章

  1. JAVA设计模式:蝇量模式

    声明:转载请说明来源:http://www.cnblogs.com/pony1223/p/7554686.html 一.引出蝇量模式 现在假设有一个项目,这个项目是为公园设计一个景观的部署,那么这个时 ...

  2. 二十四种设计模式:享元模式(Flyweight Pattern)

    享元模式(Flyweight Pattern) 介绍运用共享技术有效地支持大量细粒度的对象. 示例有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要运用共享技术支 ...

  3. 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern)

    原文:乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) 作者:weba ...

  4. Java享元模式(Flyweight Pattern)

    享元模式(Flyweight Pattern)主要用于减少创建的对象数量,并减少内存占用并提高性能. 这种类型的设计模式属于结构模式,因为该模式提供了减少对象计数的方法,从而改善应用的对象结构. 享元 ...

  5. 设计模式-11享元模式(Flyweight Pattern)

    1.模式动机 在面向对象程序设计过程中,有时会面临要创建大量相同或相似对象实例的问题.创建那么多的对象将会耗费很多的系统资源,它是系统性能提高的一个瓶颈. 享元模式就是把相同或相似对象的公共部分提取出 ...

  6. 设计模式系列之享元模式(Flyweight Pattern)——实现对象的复用

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  7. Head First设计模式之享元模式(蝇量模式)

    一.定义 享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能.这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式. ...

  8. php享元模式(flyweight pattern)

    周日一大早,今天要送老婆孩子去火车站, 所以练代码要早点哈. <?php /* The flyweight pattern is about performance and resource r ...

  9. 享元模式(Flyweight Pattern)

    定义: 采用一个共享来避免大量拥有相同内容对象的开销.这种开销中最常见.直观的就是内存的损耗.享元模式以共享的方式高效的支持大量的细粒度对象. 享元的英文是flyweight,是一个来自体育方面的专业 ...

随机推荐

  1. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  2. 并行编程之多线程共享非volatile变量,会不会可能导致线程while死循环

    背景 大家都知道线程之间共享变量要用volatilekeyword.可是,假设不用volatile来标识,会不会导致线程死循环?比方以下的伪代码: static int flag = -1; void ...

  3. 进程关系之tcgetpgrp、tcsetpgrp和tcgetsid函数

    需要有一种方法来通知内核哪一个进程组是前台进程组,这样,终端设备驱动程序就能了解将终端输入和终端产生的信号送到何处. #include <unistd.h> pid_t tcgetpgrp ...

  4. Microsoft Office Visio Professional 2007密钥

    Microsoft Office Visio Professional 2007 产品密钥: W2JJW-4KYDP-2YMKW-FX36H-QYVD8

  5. C# 之 日常积累(二)

    主要涉及(1)数字前补0:(2)去掉decimal类型后边无效的0相关问题. 1.数字前补0 ; ) { returnnumber.ToString(); } else { returnnumber. ...

  6. GoF的23个经典设计模式

    以文本和思维导图的方式简明扼要的介绍了GoF的23个经典设计模式,可当成学习设计模式的一个小手册,偶尔看一下,说不定会对大师的思想精髓有新的领悟. GoF(“四人帮”,又称Gang of Four,即 ...

  7. Android打地鼠游戏源码带道具购买的Android游戏开发

    这是一款基于安卓的打地鼠游戏,界面简洁,有level模式打地鼠和无尽模式打地鼠两种游戏模式,并可以通过商店使用金币进行道具的购买,道具可以让你更容易通关:同时金币可以在游戏通关的时候获取.工程中有较为 ...

  8. 使用RecyclerView实现瀑布流的效果

    主函数: public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; privat ...

  9. typedef的使用3——使用经过typedef定义的函数构成的函数数组

    #include <stdio.h> #include <string.h>//不加还能跑,加上反而跑不了了...笑哭 #pragma warning(disable:4996 ...

  10. 【转载】Java的四种引用

    在Java中,虽然不需要程序员手动去管理对象的生命周期,但是如果希望某些对象具备一定的生命周期的话(比如内存不足时JVM就会自动回收某些对象从而避免OutOfMemory的错误)就需要用到软引用和弱引 ...