享元模式

 

参考:http://www.cnblogs.com/java-my-life/archive/2012/04/26/2468499.html

享元模式的结构
  享元模式采用一个共享来避免大量拥有相同内容对象的开销。这种开销最常见、最直观的就是内存的损耗。享元对象能做到共享的关键是区分内蕴状态(Internal State)和外蕴状态(External State)。

  一个内蕴状态是存储在享元对象内部的,并且是不会随环境的改变而有所不同。因此,一个享元可以具有内蕴状态并可以共享。

  一个外蕴状态是随环境的改变而改变的、不可以共享的。享元对象的外蕴状态必须由客户端保存,并在享元对象被创建之后,在需要使用的时候再传入到享元对象内部。外蕴状态不可以影响享元对象的内蕴状态,它们是相互独立的。

  享元模式可以分成单纯享元模式和复合享元模式两种形式。

单纯享元模式  
  在单纯的享元模式中,所有的享元对象都是可以共享的。

  单纯享元模式所涉及到的角色如下:

  ●  抽象享元(Flyweight)角色 :给出一个抽象接口,以规定出所有具体享元角色需要实现的方法。

  ●  具体享元(ConcreteFlyweight)角色:实现抽象享元角色所规定出的接口。如果有内蕴状态的话,必须负责为内蕴状态提供存储空间。

  ●  享元工厂(FlyweightFactory)角色 :本角色负责创建和管理享元角色。本角色必须保证享元对象可以被系统适当地共享。当一个客户端对象调用一个享元对象的时候,享元工厂角色会检查系统中是否已经有一个符合要求的享元对象。如果已经有了,享元工厂角色就应当提供这个已有的享元对象;如果系统中没有一个适当的享元对象的话,享元工厂角色就应当创建一个合适的享元对象。

/DesignPatterns/src/com/louis/flyweight/Flyweight.java

/**
 * Project Name:DesignPatterns
 * File Name:Flightweight.java
 * Package Name:com.louis.flyweight
 * Date:2017年11月2日下午8:28:48
 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved.
 *
*/

package com.louis.flyweight;
/**
 * ClassName:Flightweight 
 * Function: TODO ADD FUNCTION. 
 * Reason:     TODO ADD REASON. 
 * Date:     2017年11月2日 下午8:28:48 
 * @author   michael
 * @version  
 * @since    JDK 1.7
 * @see      
 */
public interface Flyweight {
    //一个示意性方法,参数state是外蕴状态
    public void operation(String state);
}

/DesignPatterns/src/com/louis/flyweight/ConcreteFlyweight.java

/**
 * Project Name:DesignPatterns
 * File Name:ConcreteFlyweight.java
 * Package Name:com.louis.flyweight
 * Date:2017年11月2日下午8:34:40
 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved.
 *
*/

package com.louis.flyweight;
/**
 * ClassName:ConcreteFlyweight 
 * Function: TODO ADD FUNCTION. 
 * Reason:     TODO ADD REASON. 
 * Date:     2017年11月2日 下午8:34:40 
 * @author   michael
 * @version  
 * @since    JDK 1.7
 * @see      
 */
public class ConcreteFlyweight implements Flyweight {

    private Character intrinsicStateCharacter = null;
    //构造函数,内蕴状态作为参数输入
    public ConcreteFlyweight(Character state){
        this.intrinsicStateCharacter = state;
    }

    //外蕴状态作为参数传入方法中,改变方法的行为,但并不改变对象的内蕴状态u
    public void operation(String state) {
        System.out.println("Intrinsic State = " + this.intrinsicStateCharacter);
        System.out.println("Extrinsic State = " + state);

    }

}

/DesignPatterns/src/com/louis/flyweight/FlyweightFatory.java

/**
 * Project Name:DesignPatterns
 * File Name:FlyweightFatory.java
 * Package Name:com.louis.flyweight
 * Date:2017年11月2日下午8:41:41
 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved.
 *
*/

package com.louis.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:FlyweightFatory 
 * Function: TODO ADD FUNCTION. 
 * Reason:     TODO ADD REASON. 
 * Date:     2017年11月2日 下午8:41:41 
 * @author   michael
 * @version  
 * @since    JDK 1.7
 * @see      
 */
public class FlyweightFatory {
    private Map<Character, Flyweight> files = new HashMap<Character, Flyweight>();
    
    public Flyweight factory(Character state){
        //先从缓存中查找对象
        Flyweight fly = files.get(state);
        if(fly == null){
            //如果对象不存在则创建一个新的flyweight对象
            fly = new ConcreteFlyweight(state);
            //吧这个新的flyweight 对象添加到缓存中
            files.put(state, fly);
        }
        return fly;
    }
    
}

/DesignPatterns/src/com/louis/flyweight/Client.java

/**
 * Project Name:DesignPatterns
 * File Name:Client.java
 * Package Name:com.louis.flyweight
 * Date:2017年11月2日下午8:49:58
 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved.
 *
 */

package com.louis.flyweight;

/**
 * ClassName:Client Function: TODO ADD FUNCTION. Reason: TODO ADD REASON. Date:
 * 2017年11月2日 下午8:49:58
 * 
 * @author michael
 * @version
 * @since JDK 1.7
 * @see
 */
public class Client {
    public static void main(String[] args) {
        FlyweightFatory factory = new FlyweightFatory();
        Flyweight flyweight = factory.factory(new Character('a'));
        flyweight.operation("first call");

        flyweight = factory.factory(new Character('b'));
        flyweight.operation("Second Call");

        flyweight = factory.factory(new Character('a'));
        flyweight.operation("Third Call");
    }
}

虽然客户端申请了三个享元对象,但是实际创建的享元对象只有两个,这就是共享的含义。运行结果如下:
复合享元模式
  在单纯享元模式中,所有的享元对象都是单纯享元对象,也就是说都是可以直接共享的。还有一种较为复杂的情况,将一些单纯享元使用合成模式加以复合,形成复合享元对象。这样的复合享元对象本身不能共享,但是它们可以分解成单纯享元对象,而后者则可以共享。

  复合享元角色所涉及到的角色如下:

  ●  抽象享元(Flyweight)角色 :给出一个抽象接口,以规定出所有具体享元角色需要实现的方法。

  ●  具体享元(ConcreteFlyweight)角色:实现抽象享元角色所规定出的接口。如果有内蕴状态的话,必须负责为内蕴状态提供存储空间。

  ●   复合享元(ConcreteCompositeFlyweight)角色 :复合享元角色所代表的对象是不可以共享的,但是一个复合享元对象可以分解成为多个本身是单纯享元对象的组合。复合享元角色又称作不可共享的享元对象。

  ●   享元工厂(FlyweightFactory)角色 :本角 色负责创建和管理享元角色。本角色必须保证享元对象可以被系统适当地共享。当一个客户端对象调用一个享元对象的时候,享元工厂角色会检查系统中是否已经有 一个符合要求的享元对象。如果已经有了,享元工厂角色就应当提供这个已有的享元对象;如果系统中没有一个适当的享元对象的话,享元工厂角色就应当创建一个 合适的享元对象。

/DesignPatterns/src/com/louis/flyweight/FlyweightCompositeFactory.java

/**
 * Project Name:DesignPatterns
 * File Name:FlyweightCompositeFactory.java
 * Package Name:com.louis.flyweight
 * Date:2017年11月2日下午9:14:12
 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved.
 *
*/

package com.louis.flyweight;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:FlyweightCompositeFactory 
 * Function: TODO ADD FUNCTION. 
 * Reason:     TODO ADD REASON. 
 * Date:     2017年11月2日 下午9:14:12 
 * @author   michael
 * @version  
 * @since    JDK 1.7
 * @see      
 */
public class FlyweightCompositeFactory {
    private Map<Character, Flyweight> files = new HashMap<Character, Flyweight>();
    
    //复合享元工厂方法
    public Flyweight factory(List<Character> compositeState){
        ConcreteCompositeFlyweight compositeFly = new ConcreteCompositeFlyweight();
        
        for(Character state : compositeState){
            compositeFly.add(state,this.factory(state));
        }
        
        return compositeFly;
    }
    
    /**
     * 单纯享元工厂方法
     */
    public Flyweight factory(Character state){
        //先从缓存中查找对象
        Flyweight fly = files.get(state);
        if(fly == null){
            //如果对象不存在则创建一个新的Flyweight对象
            fly = new ConcreteFlyweight(state);
            //把这个新的Flyweight对象添加到缓存中
            files.put(state, fly);
        }
        return fly;
    }
}

/DesignPatterns/src/com/louis/flyweight/ConcreteCompositeFlyweight.java

/**
 * Project Name:DesignPatterns
 * File Name:ConcreteCompositeFlyweight.java
 * Package Name:com.louis.flyweight
 * Date:2017年11月2日下午9:08:25
 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved.
 *
*/

package com.louis.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:ConcreteCompositeFlyweight 
 * Function: TODO ADD FUNCTION. 
 * Reason:     TODO ADD REASON. 
 * Date:     2017年11月2日 下午9:08:25 
 * @author   michael
 * @version  
 * @since    JDK 1.7
 * @see      
 */
public class ConcreteCompositeFlyweight implements Flyweight {


    private Map<Character, Flyweight> filesMap = new HashMap<Character, Flyweight>();
    
    //增加一个新的单纯享元对象到聚集中
    public void add(Character key, Flyweight flyweight){
        filesMap.put(key, flyweight);
    }
    
    //外蕴状态作为参数传入到方法中
    @Override
    public void operation(String state) {
        Flyweight  flyweight = null;
        for (Object object : filesMap.keySet()) {
            flyweight = filesMap.get(object);
            flyweight.operation(state);
        }

    }

}

/DesignPatterns/src/com/louis/flyweight/ClientComposite.java

/**
 * Project Name:DesignPatterns
 * File Name:ClientComposite.java
 * Package Name:com.louis.flyweight
 * Date:2017年11月2日下午9:19:24
 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved.
 *
*/

package com.louis.flyweight;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:ClientComposite 
 * Function: TODO ADD FUNCTION. 
 * Reason:     TODO ADD REASON. 
 * Date:     2017年11月2日 下午9:19:24 
 * @author   michael
 * @version  
 * @since    JDK 1.7
 * @see      
 */
public class ClientComposite {
    public static void main(String[] args) {
        List<Character> compositeState = new ArrayList<Character>();
        compositeState.add('a');
        compositeState.add('b');
        compositeState.add('c');
        compositeState.add('a');
        compositeState.add('b');
        
        FlyweightCompositeFactory flyweightCompositeFactory = new FlyweightCompositeFactory();
        Flyweight compositeFly1 = flyweightCompositeFactory.factory(compositeState);
        Flyweight compositeFly2 = flyweightCompositeFactory.factory(compositeState);
        compositeFly1.operation("Composite Call");
        
        System.out.println("---------------------------------");        
        System.out.println("复合享元模式是否可以共享对象:" + (compositeFly1 == compositeFly2));
        
        Character state = 'a';
        Flyweight fly1 = flyweightCompositeFactory.factory(state);
        Flyweight fly2 = flyweightCompositeFactory.factory(state);
        
        System.out.println("单纯享元模式是否可以共享对象:" + (fly1 == fly2));
        
    }

    
}

 

posted on 2017-11-02 21:45  Michael2397  阅读(103)  评论(0编辑  收藏  举报

导航