package com.ruanyun;
import java.util.HashMap;
import java.util.Map;
//*
* @Auther: maxw
* @Date: 2018/11/21 09:47
* @Description:享元模式:“享”是分享的意思,是指某样东西被大家共享。 这是这个模式的最终目的。
*共享元模式类似于单模式,但都只生成一个对象并共享使用。 这里有问题。 那是对共享对象的修改。 为了避免出现这种情况,请提取这些对象的公共部分或不变部分以形成一个对象。 这个对象可以避免修改的问题。
*体验的目的是减少不必要的内存消耗,并集中对同一对象的多种访问。 您不需要为每个访问者创建单独的对象以减少内存消耗。
*在共享模式下使用的场景:项目中创建许多对象,如果这些对象中存在许多相同的模块,请提取这些相同的模块以在共享模式下生成一个对象,然后使用这些对象创建许多以前的对象
*/
公共类测试9 {
publicstaticvoidmain (string args [ ] ) {
String yundong='足球';
for(intI=0; i5; I ) {
tiyuguantiyuguan=建珠factory.gettyg (yundong );
tiYuGuan.setName (合肥体育馆);
tiYuGuan.setShape ('椭圆形);
tiYuGuan.setYongtu ('比赛');
tiYuGuan.use (;
}
}
}
//定义建筑界面
界面建珠{
void use (;
}
//建造体育馆
classtiyuguanimplementsjianzhu {
私有字符串名称;
私有字符串形状;
私有字符串;
私有字符串;
PublictiyuGuan(stringYundong ) {
this.yundong=yundong;
}
公共字符串获取永驻() )。
return yongtu;
}
publicvoidsetyongtu (字符串yongtu )。
this.yongtu=yongtu;
}
公共字符串获取名称
return name;
}
公共void set name (string name ) {
this.name=name;
}
公共字符串获取形状
return shape;
}
publicvoidsetshape(stringshape ) {
this.shape=shape;
}
公共字符串获取yundong ()
返回云东;
}
publicvoidsetyundong (字符串偏转)。
this.yundong=yundong;
}
@Override
公共void use () {
System.out.println (该体育馆用于“yongtu”,项目为:' yundong”,站点形状为:' shape ),站点名称为:' name,对象为33: '
}
}
工厂类建筑工厂需要生产体育馆等建筑物
class JianZhuFactory{
privatestaticfinalmaptygs=new hashmap (;
公共统计信息(string yundong ) {
tiyuguantyg=tygs.get(Yundong );
if (tyg==空值) {
tygs.put(Yundong,newtiyuGuan ) Yundong );
}
returntygs.get(yundong;
}
公共静态获取
return tygs.size (;
}
}