享元模式
来源:互联网 发布:有什么好的护肤品知乎 编辑:程序博客网 时间:2024/05/22 07:02
享元模式
运用共享技术有效地支持大量细粒度的对象。
一 、 概述
一个类中的成员变量表明该类所创建的对象所具有的属性,在某些程序设计中我们可能用一个类创建若干个对象,但是我们发现这些对象的一个共同特点是它们有一部分属性的取值必须是完全相同的。二、享元模式的结构与使用
模式的结构中包括三种角色:
模式的UML类图
模式的结构的描述与使用'
1.享元接口(Flyweight):Flyweight.java
public interface Flyweight{
public double getHeight();
public double getWidth();
public double getLength();
public void printMess(String mess);
}
2.享元工厂(FlyweightFactory)与具体享元_1:FlyweightFactory.java
import java.util.HashMap;
public class FlyweightFactory{
private HashMap<String,Flyweight> hashMap;
static FlyweightFactory factory=newFlyweightFactory();
private FlyweightFactory(){
hashMap=newHashMap<String,Flyweight>();
}
public static FlyweightFactorygetFactory(){
returnfactory;
}
publicsynchronized 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(width,height,length);
hashMap.put(key,ft);
return ft;
}
}
2.享元工厂(FlyweightFactory)与具体享元_2:FlyweightFactory.java
class ConcreteFlyweight implementsFlyweight{
private double width;
private double height;
private double length;
private ConcreteFlyweight(double width,doubleheight,double length){
this.width=width;
this.height=height;
this.length=length;
}
public double getHeight(){
return height;
}
public double getWidth(){
return width;
}
public double getLength(){
return length;
}
public void printMess(String mess){
System.out.print(mess);
System.out.print("宽度:"+width);
System.out.print("高度:"+height);
System.out.println("长度:"+length);
}
}
}
3.应用_1: Car.java
public class Car{
Flyweight flyweight;
String name,color;
int power;
Car(Flyweight flyweight,Stringname,Stringcolor,int power){
this.flyweight=flyweight;
this.name=name;
this.color=color;
this.power=power;
}
public void print(){
System.out.print("名称:"+name);
System.out.print("颜色:"+color);
System.out.print("功率:"+power);
System.out.print("宽度:"+flyweight.getWidth());
System.out.print("高度:"+flyweight.getHeight());
System.out.println("长度:"+flyweight.getLength());
}
}
3.应用_2: Application.java
public class Application{
public static void main(String args[]) {
FlyweightFactory factory=FlyweightFactory.getFactory();
doublewidth=1.82,height=1.47,length=5.12;
Stringkey=""+width+"#"+height+"#"+length;
Flyweight flyweight=factory.getFlyweight(key);
Car audiA6One=newCar(flyweight,"奥迪A6","黑色",128);
Car audiA6Two=newCar(flyweight,"奥迪A6","灰色",160);
audiA6One.print();
audiA6Two.print();
width=1.77;
height=1.45;
length=4.63;
key=""+width+"#"+height+"#"+length;
flyweight=factory.getFlyweight(key);
Car audiA4One=newCar(flyweight,"奥迪A4","蓝色",126);
Car audiA4Two=newCar(flyweight,"奥迪A4","红色",138);
flyweight.printMess("名称:奥迪A4 颜色:蓝色 功率:126");
flyweight.printMess("名称:奥迪A4 颜色:红色 功率:138");
}
}
三、享元模式的优点
- 深入浅出享元模式
- 享元模式
- 享元(FlyWeight)模式
- 享元模式
- 享元模式
- 享元模式
- 享元模式
- 享元模式Flyweight
- 享元模式
- 享元模式
- 享元模式
- FlyWeight 享元模式
- 享元模式(Flyweight)
- 享元模式
- 享元模式
- 享元模式
- 享元模式
- 享元模式 ---flyweight
- 缓存处理
- 用来理解 Java 编程语言的 8 个图表
- error LNK2026: 模块对于 SAFESEH 映像是不安全的
- Oracle 查询出来的数据取第一条
- OCP-1Z0-051-V9.02-98题
- 享元模式
- 深度隔离的界面
- WinForm中Treeview实现根节点无选择框而子节点存在
- 将博客搬至CSDN
- tomcat 使用及基本发布web
- 获取android设备密度的三种方法
- 常量函数返回值
- ubuntu userdel删除用户时候提示:userdel: user ** is currently logged in
- UIView的动画