结构模式-享元模式

享元模式:以共享的方式高翔的支持大量的细粒度对象

享元对象能够做到共享的关键是区分内蕴状态和外蕴状态,他们之前互不影响,相互独立。

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

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

享元模式分为:单独享元模式和复合式享元模式

 

享元工厂:

package com.javapatterns.flyweight.simple;

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

public class FlyweightFactory {
	private HashMap flies = new HashMap();
	/**
	 * @link aggregation
	 * @directed
	 * @clientRole Flyweights
	 */
	private Flyweight lnkFlyweight;

	public FlyweightFactory() {
	}

	public synchronized Flyweight factory(Character state) {
		if (flies.containsKey(state)) {
			return (Flyweight) flies.get(state);
		} else {
			Flyweight fly = new ConcreteFlyweight(state);
			flies.put(state, fly);
			return fly;
		}
	}

	public void checkFlyweight() {
		Flyweight fly;
		int i = 0;

		System.out.println("\n==========checkFlyweight()=============");
		for (Iterator it = flies.entrySet().iterator(); it.hasNext();) {
			Map.Entry e = (Map.Entry) it.next();
			System.out.println("Item " + (++i) + " : " + e.getKey());
		}
		System.out.println("==========checkFlyweight()=============");
	}

}

 抽象享元角色:

package com.javapatterns.flyweight.simple;

abstract public class Flyweight
{
    abstract public void operation(String state);
}

 

具体享元角色:

package com.javapatterns.flyweight.simple;

public class ConcreteFlyweight extends Flyweight
{
    private Character intrinsicState = null;

	public ConcreteFlyweight(Character state)
	{ 
		this.intrinsicState = state;
	}
	
	public void operation(String state)
	{ 
		System.out.print( "\nIntrinsic State = " + intrinsicState +
            ", Extrinsic State = " + state);
	}
}

 

 

客户端:

package com.javapatterns.flyweight.simple;

public class Client
{
    private static FlyweightFactory factory;

    static public void main(String[] args)
	{ 
		factory = new FlyweightFactory();

		Flyweight fly = factory.factory(new Character('a'));
		fly.operation("First Call");
		
		fly = factory.factory(new Character('b'));
		fly.operation("Second Call");
		
		fly = factory.factory(new Character('a'));
		fly.operation("Third Call");
		
		// intrinsic Flyweight
		factory.checkFlyweight();
	} 
}

 

 

复合式享元模式:

抽象享元角色:

package com.javapatterns.flyweight.composite;

abstract public class Flyweight
{
    abstract public void operation(String state); 
}

 

具体享元角色:

package com.javapatterns.flyweight.composite;

public class ConcreteFlyweight extends Flyweight
{
    private Character intrinsicState = null;

	public ConcreteFlyweight(Character state)
	{ 
		this.intrinsicState = state;
	}
	
	public void operation(String state)
	{ 
		System.out.print( "\nInternal State = " +
            intrinsicState + " Extrinsic State = " + state );
	}
}

 

复合享元角色:

package com.javapatterns.flyweight.composite;

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

public class ConcreteCompositeFlyweight extends Flyweight 
{
    private HashMap flies = new HashMap(10);

    /**
     * @link aggregation
     * @directed
     * @clientRole Composite
     */
    private Flyweight flyweight;

	public ConcreteCompositeFlyweight()
	{ 
	}
	
	public void add(Character key, Flyweight fly)
	{ 
		flies.put(key, fly);
	}
	
	public void operation(String extrinsicState)
	{
        Flyweight fly =  null;

		for ( Iterator it = flies.entrySet().iterator() ; it.hasNext() ;  )
        {
			Map.Entry e = (Map.Entry) it.next();
            fly = (Flyweight) e.getValue();

            fly.operation(extrinsicState);
        }
	}
}

 客户端:

package com.javapatterns.flyweight.composite;

public class Client
{
    private static FlyweightFactory factory;

    public static void main(String[] args)
	{ 
		factory = new FlyweightFactory();

		Flyweight fly;
		
        fly = factory.factory("abc");
        fly.operation("Composite Call");
		
		factory.checkFlyweight();
	} 
}

 

你可能感兴趣的:(享元模式)