Skip to content

Commit c59cfcf

Browse files
committed
享元模式
1 parent 240520f commit c59cfcf

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.anxpp.designpattern.flyweight;
2+
//享元接口
3+
public interface IWeather {
4+
void printWeather();
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.anxpp.designpattern.flyweight;
2+
import java.util.HashMap;
3+
//简单的享元模式
4+
public class SimpleFlyweight {
5+
public static void main(String args[]){
6+
FlyweightFactory factory = new FlyweightFactory();
7+
IFlyweight flyweight1,flyweight2,flyweight3,flyweight4;
8+
flyweight1 = factory.getFlyweight("value1");
9+
flyweight2 = factory.getFlyweight("value1");
10+
flyweight3 = factory.getFlyweight("value1");
11+
flyweight4 = factory.getFlyweight("value2");
12+
flyweight1.doSomething();
13+
flyweight2.doSomething();
14+
flyweight3.doSomething();
15+
flyweight4.doSomething();
16+
System.out.println(factory.size());
17+
}
18+
}
19+
//享元接口
20+
interface IFlyweight{
21+
void doSomething();
22+
}
23+
//具体享元
24+
class Flyweight implements IFlyweight{
25+
private String value;
26+
public Flyweight(String value){
27+
this.value = value;
28+
}
29+
@Override
30+
public void doSomething() {
31+
System.out.println(value);
32+
}
33+
}
34+
//享元工厂
35+
class FlyweightFactory{
36+
HashMap<String, IFlyweight> flyweights = new HashMap<String, IFlyweight>();
37+
IFlyweight getFlyweight(String value){
38+
IFlyweight flyweight = flyweights.get(value);
39+
if(flyweight == null){
40+
flyweight = new Flyweight(value);
41+
flyweights.put(value, flyweight);
42+
}
43+
return flyweight;
44+
}
45+
public int size(){
46+
return flyweights.size();
47+
}
48+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.anxpp.designpattern.flyweight;
2+
public class TestUse {
3+
public static void main(String args[]){
4+
WeatherFactory factory = new WeatherFactory();
5+
IWeather weather1,weather2,weather3,weather4,weather5,weather6,weather7,weather8;
6+
weather1 = factory.getFlyWeight("多云",15);
7+
weather2 = factory.getFlyWeight("晴",23);
8+
weather3 = factory.getFlyWeight("多云",16);
9+
weather4 = factory.getFlyWeight("阴",10);
10+
weather5 = factory.getFlyWeight("多云",15);
11+
weather6 = factory.getFlyWeight("多云",15);
12+
weather7 = factory.getFlyWeight("多云",15);
13+
weather8 = factory.getFlyWeight("多云",15);
14+
weather1.printWeather();
15+
weather2.printWeather();
16+
weather3.printWeather();
17+
weather4.printWeather();
18+
weather5.printWeather();
19+
weather6.printWeather();
20+
weather7.printWeather();
21+
weather8.printWeather();
22+
System.out.println("实际对象个数" + factory.getFlyweightSize());
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.anxpp.designpattern.flyweight;
2+
//具体享元
3+
public class Weather implements IWeather{
4+
private String weather;
5+
private Integer temperature;
6+
public Weather(String weather,int temperature){
7+
this.weather = weather;
8+
this.temperature = temperature;
9+
}
10+
@Override
11+
public void printWeather() {
12+
System.out.print("天气:" + weather);
13+
System.out.println(" 温度:" + temperature);
14+
}
15+
@Override
16+
public boolean equals(Object obj) {
17+
Weather weather = (Weather)obj;
18+
return weather.weather.equals(this.weather)&&weather.temperature==temperature;
19+
}
20+
@Override
21+
public int hashCode() {
22+
return (weather.hashCode()+temperature.hashCode())/2;
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.anxpp.designpattern.flyweight;
2+
3+
import java.util.HashMap;
4+
//享元工厂
5+
public class WeatherFactory {
6+
private HashMap<IWeather, IWeather> weathers;
7+
public WeatherFactory(){
8+
weathers = new HashMap<IWeather, IWeather>();
9+
}
10+
public IWeather getFlyWeight(String weather,int temperature){
11+
Weather objectWeather = new Weather(weather, temperature);
12+
IWeather flyweight = weathers.get(objectWeather);
13+
if(flyweight == null){
14+
flyweight = objectWeather;
15+
weathers.put(objectWeather, flyweight);
16+
}
17+
else objectWeather = null;//方便gc回收
18+
return flyweight;
19+
}
20+
public int getFlyweightSize(){
21+
return weathers.size();
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @author Administrator
3+
* 享元模式
4+
*/
5+
package com.anxpp.designpattern.flyweight;

0 commit comments

Comments
 (0)