享元模式(Flyweight Pattern)
来源:互联网 发布:孙振耀。知乎 编辑:程序博客网 时间:2024/06/09 19:39
- 享元模式的关键是使用一个称作享元的对象为其他对象提供共享的数据,而且能够保证使用享元的对象不能修改享元中的数据。
常见的三种角色
1.享元接口:定义了享元对外公开内部数据的方法和接受外部数据的方法
2.具体的享元:实现享元接口的类,这个类的实例称作享元对象或者享元。
3.享元工厂:负责创建和管理享元,可以通过一个散列表来管理享元。享元接口
public interface Flyweight { public double getHeight(); public double getWidth(); public double getLength(); public void printMess(String mess);}
享元工厂和享元类
public class FlyweightFactory { private HashMap<String, Flyweight> hashMap; static FlyweightFactory factory=new FlyweightFactory(); private FlyweightFactory(){ hashMap=new HashMap<String, Flyweight>(); } public static FlyweightFactory getFactory(){ return factory; } public synchronized Flyweight getFlyweight(String key){ if(hashMap.containsKey(key)){ return hashMap.get(key); }else{ double width=0,height=0,length=0; String[] str=key.split("#"); width=Double.parseDouble(str[0]); height=Double.parseDouble(str[1]); length=Double.parseDouble(str[2]); Flyweight ft=new ConcreteFlyweight(height, width, length); hashMap.put(key, ft); return ft; } } class ConcreteFlyweight implements Flyweight{ private double height; private double widht; private double lenght; public ConcreteFlyweight(double height, double widht, double lenght) { super(); this.height = height; this.widht = widht; this.lenght = lenght; } public double getHeight() { return height; } public double getWidth() { return widht; } public double getLength() { return this.lenght; } public void printMess(String mess) { P.p(mess); P.p("宽度:"+widht); P.p("高度"+height); P.p("长度"+lenght); } }}
其他对象
public class Car { Flyweight flyweight; int power; String name,color; Car(Flyweight flyweight, int power, String name, String color) { super(); this.flyweight = flyweight; this.power = power; this.name = name; this.color = color; } public void print(){ P.p("color:"+color); P.p("power:"+power); P.p("name:"+name); P.p("width_"+flyweight.getWidth()); P.p("height_"+flyweight.getHeight()); P.p("length_"+flyweight.getLength()); P.p(); }}
测试
public class Application { public static void main(String args[]){ FlyweightFactory factory=FlyweightFactory.getFactory(); double width=1.82,height=1.47,length=5.12; String key=""+width+"#"+height+"#"+length; Flyweight flyweight=factory.getFlyweight(key); Car a6=new Car(flyweight, 128, "mazida", "red"); Car a7=new Car(flyweight,190, "bieke", "blue"); a6.print(); a7.print(); }}
结果
color:redpower:128name:mazidawidth_1.82height_1.47length_5.12color:bluepower:190name:biekewidth_1.82height_1.47length_5.12
阅读全文
0 0
- 享元模式(Flyweight Pattern)
- 享元模式(Flyweight Pattern)
- 享元模式(Flyweight pattern)
- 享元模式 Flyweight Pattern
- 享元模式(Flyweight Pattern)
- 享元模式(Flyweight Pattern)
- 享元模式(Flyweight Pattern)
- 享元模式( Flyweight Pattern)
- flyweight pattern -- 享元模式
- 享元模式(Flyweight Pattern)
- 享元模式(Flyweight Pattern)
- 享元模式 Flyweight Pattern
- 享元模式 | Flyweight Pattern
- 享元模式(Flyweight Pattern)
- 享元模式(Flyweight Pattern)
- 设计模式【享元模式Flyweight Pattern】
- 享元模式(Flyweight Pattern)
- 享元模式(Flyweight Pattern)
- 安卓 反编译工具
- AV同步
- Spring框架总结——第一部分
- vue.js中v-for的使用及索引获取
- tomcat 阿里云启动慢的解决方法
- 享元模式(Flyweight Pattern)
- windows, mac,linux 环境下搭建python,如果你遇到坑,请查阅此处。
- 操作系统内存管理之 ---堆和栈的区别
- RedHat/CentOS6.4---永久关闭iptables
- 用Hexo制作自己的静态博客
- 用Shell脚本编写程序,将100以内能被3或7整除的数的和显示出来.
- linux新系统调整综合实施方案
- 从Cassandra到分布式存储系统
- 在客户端上登录微软邮箱时提示您输出的用户名或密码不起作用的解决办法