享元模式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);    }}