⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flyweightfactory.java

📁 这个文件里面包含了java设计模式的一些例子讲解
💻 JAVA
字号:
package com.javapatterns.flyweight.composite;

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 Flyweight factory(String compositeState)
	{ 
		ConcreteCompositeFlyweight compositeFly = new ConcreteCompositeFlyweight();
		
		int length = compositeState.length();
        Character state = null;

		for(int i = 0; i < length; i++)
		{ 
			state = new Character(compositeState.charAt(i) );
            System.out.println("factory(" + state + ")");
			compositeFly.add( state, this.factory( state) );
		}
		return compositeFly;
    }

	public 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("\n==========checkFlyweight()=============");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -