[原]享元模式-复合享元

[关键字]:java,design pattern,设计模式,《Java与模式》学习,Flyweight Pattern,享元模式
[环境]:StarUML5.0 + JDK6
[作者]:Winty ([email protected]) http://wintys.blogjava.net
[正文]:

享元模式-复合享元:


[原]享元模式-复合享元_第1张图片
 

 

package pattern.flyweight.unsharable;
import java.util.*;

/**
 * 复合的享元模式:
 * 复合享元是不能共享的,所以叫Unsharable Flyweight。
 * 但是复合享元是可以分解为可共享的单纯享元。
 *
 * @version 2009-6-25
 * @author Winty([email protected])
 */
public class UnsharableFlyweightTest{
    public static void main(String[] args){
        FlyweightFactory f = new FlyweightFactory();
        
        Flyweight fly = f.factory("aba");
        fly.operation("ex");

        f.checkFlyweight();
    }
}

/**
 * 抽象享元
 */
abstract class Flyweight{
    public abstract void operation(String extrinsicState);
}


/**
 * 具体享元
 */
class ConcreteFlyweight extends Flyweight{
    private Character intrinsicState;

    public ConcreteFlyweight(Character intrinsicState){
        this.intrinsicState = intrinsicState;
    }

    /**
     * 外蕴状态改变方法行为,但不会改变内蕴状态
     */
    @Override
    public void operation(String extrinsicState){
        String str = "intrinsic:" + intrinsicState;
        str += "   extrinsic:" + extrinsicState;
        System.out.println(str);
    }
}


/**
 * 复合享元
 */
class UnsharableFlyweight extends Flyweight{
    private Map<Character , Flyweight> map;

    public UnsharableFlyweight(){
        map = new HashMap<Character , Flyweight>();
    }

    public void add(Character c , Flyweight fly){
        map.put(c , fly);
    }

    @Override
    public void operation(String extrinsicState){
        Iterator<Map.Entry<Character , Flyweight>> it
            = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<Character , Flyweight> entry = it.next();
            Flyweight fly = entry.getValue();
            fly.operation(extrinsicState);
        }
    }
}


/**
 * 享元工厂
 */
class FlyweightFactory{
    private Map<Character , Flyweight> map;

    public FlyweightFactory(){
        map = new HashMap<Character , Flyweight>();
    }

    /**
     * 单纯享元工厂
     */
    public Flyweight factory(Character state){
        Flyweight flyweight = null;

        if(map.containsKey(state)){
            flyweight = map.get(state);
        }
        else{
            flyweight = new ConcreteFlyweight(state);
            map.put(state , flyweight);
        }

        return flyweight;
    }

    /**
     * 复合享元工厂
     * 此处Character的复合类型恰好是String,
     * 当无此巧合时,可使用List等聚集类型传入.
     */
    public Flyweight factory(String compositeState){
        UnsharableFlyweight composite = new UnsharableFlyweight();
        
        for(int i = 0 ; i < compositeState.length(); i++){
            Character c = new Character(compositeState.charAt(i));
            composite.add(c , this.factory(c));
        }

        return composite;
    }

    /**
     * 辅助方法
     */
    public void checkFlyweight(){
        Iterator<Map.Entry<Character , Flyweight>> it
            = map.entrySet().iterator();
        System.out.println("check flyweight:");
        while(it.hasNext()){
            Map.Entry<Character , Flyweight> entry = it.next();
            Character key = entry.getKey();
            System.out.println("key:" + key);
        }
    }
}



运行结果:
intrinsic:b   extrinsic:ex
intrinsic:a   extrinsic:ex
check flyweight:
key:b
key:a

你可能感兴趣的:(设计模式,C++,c,C#,Gmail)