@@ -32,6 +32,190 @@ Flyweight模式的有效性很大程度上取决于如何使用它以及在何
3232
3333## 实例分析
3434
35+ ![ ] ( https://raw.githubusercontent.com/JamesZBL/java_design_patterns/develop/flyweight/uml/Weapon.png )
3536
37+ 出售武器的商店里摆满了各式的枪。许多枪的型号是一样的,所以不需要为每一个都创建新的对象。相反,一个对象实例可以表示多个货架项目,因此内存占用空间很小。
3638
39+ 所有的枪都可以发出射击这个动作,因此定义一个枪的接口 Shooting
40+ ```
41+ public interface Shooting {
42+
43+ void shoot();
44+ }
45+ ```
46+
47+ 商店里的枪可以大致分为手枪、步枪、狙击枪和冲锋枪,分别定义四种枪类,它们都实现 Shooting 接口
48+ ```
49+ /**
50+ * 手枪
51+ */
52+ public class HandGun implements Shooting {
53+
54+ private static final Logger LOGGER = LoggerFactory.getLogger(HandGun.class);
55+
56+ @Override
57+ public void shoot() {
58+ LOGGER.info("手枪开火了(Hash={})", System.identityHashCode(this));
59+ }
60+ }
61+ ```
62+ ```
63+ /**
64+ * 步枪
65+ */
66+ public class Musket implements Shooting {
67+
68+ private static final Logger LOGGER = LoggerFactory.getLogger(Musket.class);
69+
70+ @Override
71+ public void shoot() {
72+ LOGGER.info("步枪开火了(Hash={})", System.identityHashCode(this));
73+ }
74+ }
75+ ```
76+
77+ ```
78+ /**
79+ * 狙击枪
80+ */
81+ public class Sniper implements Shooting {
82+
83+ private static final Logger LOGGER = LoggerFactory.getLogger(Sniper.class);
84+
85+ @Override
86+ public void shoot() {
87+ LOGGER.info("狙击枪开火了(Hash={})", System.identityHashCode(this));
88+ }
89+ }
90+ ```
91+
92+ ```
93+ /**
94+ * 冲锋枪
95+ */
96+ public class Submachine implements Shooting {
97+
98+ private static final Logger LOGGER = LoggerFactory.getLogger(Submachine.class);
99+
100+ @Override
101+ public void shoot() {
102+ LOGGER.info("冲锋枪开火了(Hash={})", System.identityHashCode(this));
103+ }
104+ }
105+ ```
106+
107+ 武器的生成由武器工厂类完成,同一种枪只存在一个对象
108+ ```
109+ /**
110+ * 武器工厂
111+ */
112+ public class GunFactory {
113+
114+ private Map<GunType, Shooting> gunRepo;
115+
116+ public GunFactory() {
117+ gunRepo = new EnumMap<GunType, Shooting>(GunType.class);
118+ }
119+
120+ public Shooting createGun(GunType type) {
121+ Shooting gun = gunRepo.get(type);
122+ if (null == gun) {
123+ switch (type) {
124+ case HANDGUN: {
125+ gun = new HandGun();
126+ gunRepo.put(HANDGUN, gun);
127+ break;
128+ }
129+ case MUSKET: {
130+ gun = new Musket();
131+ gunRepo.put(MUSKET, gun);
132+ break;
133+ }
134+ case SNIPER: {
135+ gun = new Sniper();
136+ gunRepo.put(SNIPER, gun);
137+ break;
138+ }
139+ case SUBMACHINE: {
140+ gun = new Submachine();
141+ gunRepo.put(SUBMACHINE, gun);
142+ break;
143+ }
144+ }
145+ }
146+ return gun;
147+ }
148+ }
149+ ```
150+
151+ 武器商店使用武器工厂获得各种枪,放到两个不同的货架上
152+ ```
153+ /**
154+ * 武器商店
155+ */
156+ public class WeaponShop {
157+
158+ private static final Logger LOGGER = LoggerFactory.getLogger(WeaponShop.class);
159+
160+ private List<Shooting> shelfA;
161+ private List<Shooting> shelfB;
162+
163+ public WeaponShop() {
164+ shelfA = new ArrayList<>();
165+ shelfB = new ArrayList<>();
166+ fillShelves();
167+ }
168+
169+ private void fillShelves() {
170+ GunFactory factory = new GunFactory();
171+
172+ shelfA.add(factory.createGun(HANDGUN));
173+ shelfA.add(factory.createGun(HANDGUN));
174+ shelfA.add(factory.createGun(MUSKET));
175+ shelfA.add(factory.createGun(MUSKET));
176+ shelfA.add(factory.createGun(SNIPER));
177+ shelfA.add(factory.createGun(SNIPER));
178+ shelfA.add(factory.createGun(MUSKET));
179+ shelfA.add(factory.createGun(HANDGUN));
180+
181+ shelfB.add(factory.createGun(SUBMACHINE));
182+ shelfB.add(factory.createGun(SUBMACHINE));
183+ shelfB.add(factory.createGun(SUBMACHINE));
184+ shelfB.add(factory.createGun(SNIPER));
185+ }
186+
187+ public final List<Shooting> getGunsOnShelfA() {
188+ return Collections.unmodifiableList(shelfA);
189+ }
190+
191+ public final List<Shooting> getGunOnShelfB() {
192+ return Collections.unmodifiableList(shelfB);
193+ }
194+
195+ public void enumrateShelves() {
196+ enumerateShelfA();
197+ enumerateShelfB();
198+ }
199+
200+ private void enumerateShelfA() {
201+ LOGGER.info("从A货架上拿走所有武器");
202+ for (Shooting gun : shelfA) {
203+ gun.shoot();
204+ }
205+ }
206+
207+ private void enumerateShelfB() {
208+ LOGGER.info("从B货架上拿走所有武器");
209+ for (Shooting gun : shelfB) {
210+ gun.shoot();
211+ }
212+ }
213+ }
214+ ```
215+
216+ 使用
217+ ```
218+ WeaponShop shop = new WeaponShop();
219+ shop.enumrateShelves();
220+ ```
37221## 效果
0 commit comments