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

在有大量对象时,有可能造成内存溢出,把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重复创建。(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. Dev 等待提示 WaitDialogForm 升级版

    本文转载:http://www.cnblogs.com/VincentLuo/archive/2011/12/24/2298916.html   一.Dev的等待提示框                 ...

  2. iOS 强制横屏

    // // AAAAViewController.m // hengp // // Created by 朱信磊 on 15/2/13. // Copyright (c) 2015年 niit. Al ...

  3. CentOS安装Nginx安装详解

    一.准备工作,安装依赖包,缺一不可!(推荐先用命令查看自己是否已经安装一下软件,确认没有再安装) --------------------------------------------------- ...

  4. [Practical Git] Remove unnecessary git tracking with .gitignore files

    Most projects have automatically generated files or folders from the operating system, applications, ...

  5. Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  6. 10 Technologies That will Shape Future Education--reference

    http://dizyne.net/technologies-that-will-shape-future-education/ Technology is on the rise and with ...

  7. python--字典工厂函数dict()

    dic = {"name" : "wangmo" ,"age" : 18} #dic.clear() #清空字典 print(dic) #{ ...

  8. Android_AsyncTask_json

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  9. gitHub添加公钥

    gitHub添加公钥 gitHub是通过SSH公钥连接到现有仓库的 设置SSH Key ssh-keygen -t rsa -C "your_email@example.com"G ...

  10. MATLAB的基本元素

    MALTAB程序的基本数据单元是数组,MATLAB 的变量名必须以字母开头,后面可以跟字母,数字和下划线(_).只有前31个字符是有效的:如果超过了31 个字符,基余的字符将被忽略.如果声明两个变量, ...