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

在有大量对象时,有可能造成内存溢出,把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重复创建。(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. 域名的MX设置及校验方法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  2. 远程重启IIS服务

    方法一: $UserName = "administrator" $serverpass = "pass" $server = "10.4.19.60 ...

  3. 理解shared_ptr<T> ---2

    1.引用计数字段不能放在资源管理类中.我们的解决办法是,把引用计数和资源绑在一起,进行二次封装.但是这样存在一个大问题,不同类型的资源管理类不能兼容.也就是说,shared_ptr<Dog> ...

  4. Indesign多媒体富交互插件【MagBuilder】与iOS app 【MagViewer】介绍

    [写在前面]进园子有一段时间了,从来都是看别人的文章,自己的一点东西都记在本地笔记里,现在想把一些东西拿来出分享,希望能够认识一些志同道合的朋友和老师. 学习Adobe插件开发的初衷是为了给PS做插件 ...

  5. flash builder 4.7 debug via usb device iPhone 4s - device not found

    http://forums.adobe.com/message/4865192 Please provide more info on the above issue: 1.What is the m ...

  6. linux定时执行文件

     #!/bin/bashcurl "http://172.16.201.24:8080/?app_act=timer/automic_integral" -b -q -o /dev ...

  7. Distributed locks with Redis--官方

    原文:http://redis.io/topics/distlock Distributed locks with Redis Distributed locks are a very useful ...

  8. SQL Abstraction and Object Hydration

    SQL Abstraction and Object Hydration In the last chapter, we introduced database abstraction and a n ...

  9. CI框架中自定义view文件夹位置

    要想自定义view文件夹的位置,首先要了解CI框架时如何加载view文件夹的. CI中默认调用view的方法是: $this->load->view(); //这一行代码的原理是什么呢?请 ...

  10. 业界最有价值的 ASP.NET 博文汇总

    ASP.NET凭借它丰富的控件,强大的适应性及良好的封装性,成为业界开发的一门巨匠,它大大缩短了网站开发的时间,降低开发成本.并且可以运行在Web应用软件开发者的全部平台上.本电子书汇集了业界最有价值 ...