享元模式Flyweight(结构型)
来源:互联网 发布:蜂窝移动数据怎么清零 编辑:程序博客网 时间:2024/04/27 16:25
参考文档:
1.设计模式书籍;
2.http://blog.csdn.net/hguisu/article/details/7535792(设计模式(十)享元模式Flyweight(结构型))
3.http://www.jdon.com/designpatterns/flyweight.htm(设计模式之Flyweight(享元) FlyWeight模式)
Flyweight理论方面的知识,可以查看参考文档中的两篇博客;
我这里,使用Flyweight实现了两个案例,欢迎拍砖;
案例1(来自参考文档3):
以唱片CD为例,在一个XML文件中,存放了多个CD的资料.
每个CD有三个字段:
1.出片日期(year)
2.歌唱者姓名等信息(artist)
3.唱片曲目 (title)
其中,歌唱者姓名有可能重复,也就是说,可能有同一个演唱者的多个不同时期 不同曲目的CD.我们将"歌唱者姓名"作为可共享的ConcreteFlyweight.其他两个字段作为UnsharedConcreteFlyweight.
<?xml version="1.0"?><collection><cd><title>Another Green World</title><year>1978</year><artist>Eno, Brian</artist></cd><cd><title>Greatest Hits</title><year>1950</year><artist>Holiday, Billie</artist></cd><cd><title>Taking Tiger Mountain (by strategy)</title><year>1977</year><artist>Eno, Brian</artist></cd>.......</collection>
代码实现:
Client.java:
package com.rick.designpattern.flyweight2;/** * Created by MyPC on 2017/6/7. */public class Client { /*** * * * <?xml version="1.0"?> <collection> <cd> <title>Another Green World</title> <year>1978</year> <artist>Eno, Brian</artist> </cd> <cd> <title>Greatest Hits</title> <year>1950</year> <artist>Holiday, Billie</artist> </cd> <cd> <title>Taking Tiger Mountain (by strategy)</title> <year>1977</year> <artist>Eno, Brian</artist> </cd> ....... </collection> * * */ public static void main(String[] args) { ArtistFactory factory = new ArtistFactory(); CD cd = new CD("Another Green World","1978",factory.getFlyweights("Eno, Brian")); }}CD.java:
package com.rick.designpattern.flyweight2;/** * Created by MyPC on 2017/6/7. */public class CD { private String title; private String year; private Artist artist; public CD() { } public CD(String title, String year, Artist artist) { this.title = title; this.year = year; this.artist = artist; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public Artist getArtist() { return artist; } public void setArtist(Artist artist) { this.artist = artist; }}Artist.java:
package com.rick.designpattern.flyweight2;/** * Created by MyPC on 2017/6/7. */public class Artist { //内部状态 private String name; public Artist() { } public Artist(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}ArtistFactory.java:
package com.rick.designpattern.flyweight2;import java.util.Hashtable;/** * Created by MyPC on 2017/6/7. */public class ArtistFactory { private Hashtable<String, Artist> flyweights = new Hashtable(); public Artist getFlyweights(String key) { Artist artist = (Artist) flyweights.get(key); if (null == artist) { artist = new Artist(key); flyweights.put(key, artist); } return artist; }}
案例2(参考文档2的复合享元模式案例):
Client.java:
package com.rick.designpattern.flyweight3;/** * Created by MyPC on 2017/6/7. */public class Client { public static void main(String[] args){ FlyweightFactory flyweightFactory = new FlyweightFactory(); Flyweight flyweight= flyweightFactory.getFlyweight("A"); flyweight.operation("a"); Flyweight flyweight1 = flyweightFactory.getFlyweight("B"); flyweight1.operation("b"); UnsharedConcreteFlyweight unsharedConcreteFlyweight = (UnsharedConcreteFlyweight) flyweightFactory.getFlyweight(new String[]{"C","D"}); unsharedConcreteFlyweight.operation("c,d"); }}
FlyweightFactory.java:
package com.rick.designpattern.flyweight3;import java.lang.reflect.Array;import java.util.Hashtable;import java.util.List;import java.util.Set;/** * Created by MyPC on 2017/6/7. */public class FlyweightFactory { private Hashtable<Object, ConcreteFlyweight> flyweights = new Hashtable<Object, ConcreteFlyweight>(); public Flyweight getFlyweight(Object state) { if (state instanceof String[]) { UnsharedConcreteFlyweight unsharedConcreteFlyweight = new UnsharedConcreteFlyweight(); String[] list = (String[]) state; for (Object obj : list) { unsharedConcreteFlyweight.add(getFlyweight(obj)); } return unsharedConcreteFlyweight; } else if (state instanceof String) { ConcreteFlyweight concreteFlyweight = flyweights.get(state); if (null == concreteFlyweight) { concreteFlyweight = new ConcreteFlyweight(state); flyweights.put(state, concreteFlyweight); } return concreteFlyweight; } else { return null; } }}Flyweight.java:
package com.rick.designpattern.flyweight3;/** * Created by MyPC on 2017/6/7. */public interface Flyweight { public void operation(Object state);}ConcreteFlyweight.java:
package com.rick.designpattern.flyweight3;/** * 具体享元角色 */public class ConcreteFlyweight implements Flyweight { private String intrinsicState; public ConcreteFlyweight(Object state) { this.intrinsicState = String.valueOf(state); } @Override public void operation(Object state) { System.out.println("ConcreteFlyweight operation, Intrinsic State = " + intrinsicState + "; Extrinsic State = " + String.valueOf(state) + ";<br />"); }}UnsharedConcreteFlyweight.java:
package com.rick.designpattern.flyweight3;import com.rick.designpattern.facade.ConcreteFacade;import java.util.ArrayList;import java.util.List;/** * Created by MyPC on 2017/6/7. */public class UnsharedConcreteFlyweight implements Flyweight { private List<Flyweight> flyweights; public UnsharedConcreteFlyweight() { this.flyweights = new ArrayList<Flyweight>(); } @Override public void operation(Object state) { for (Flyweight concreteFlyweight : flyweights) { concreteFlyweight.operation(state); } } public void add(Flyweight concreteFlyweight) { flyweights.add(concreteFlyweight); }}
阅读全文
0 0
- 结构型模式:Flyweight(享元)
- 享元模式Flyweight(结构型)
- 结构型-享元模式(Flyweight)
- 结构型模式-Flyweight ( 享元模式 )
- 结构型模式之享元模式(Flyweight)
- 结构型模式-----享元模式(Flyweight)
- 设计模式--享元模式FlyWeight(结构型)
- 结构型设计模式---享元模式(Flyweight)
- 结构型模式之享元模式(FlyWeight)
- 十九、享元模式Flyweight(结构型模式)
- (结构型模式)FLYWEIGHT——享元模式
- 设计模式-结构型-享元模式(Flyweight)
- 结构型模式之享元模式(FlyWeight)
- 【结构型模式】flyweight(享元)
- 结构型模式之享元FLYWEIGHT
- 结构型模式-享元(flyweight)
- 结构型模6:享元模式(Flyweight Pattern)
- java享元模式(Flyweight)-结构型
- Paint.getTextBounds (String text, int start, int end, Rect bounds)
- android4.0 MediaPlayer的notify监听机制的全面剖析
- java开发中一些层或者结构的知识
- [JAVA学习笔记-78]lockInterruptibly
- Java中Calendar类的使用
- 享元模式Flyweight(结构型)
- 设计模式之原型模式
- java中condition使用遇到的问题
- Python内存管理机制
- Web应用开发周期
- CSRF攻击原理
- openSUSE网络配置(老版本)
- hostdare-7.5折优惠码/KVM/C3机房/$3.74/756M内存/17G硬盘/1T流量/Windows
- repo sync配合manifest下载旧版本代码