享元模式Flyweight(结构型)

参考文档:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值