享元模式-AbstractMap结构

享元模式: 共享数据的模式。

例子:创建定制的Map-->FlyweightMap.

1. 默认填充DATA元数据,避免每次使用FlyweightMap时创建新的数据。

2. Map的数据结构

package designpattern.flyweight;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Countries {
	
	//元数据
	public static final String[] [] DATA = {{"CHINA", "BeiJing"},{"AMERICA", "Washington"},
		{"Canada", "canada"}, {"Japan", "dongjing"},
		{"Australia", "Sydney"}, {"Korea", "souer"}};
	
	private static class FlyweightMap extends AbstractMap<String, String> {
		
		private static Set<Map.Entry<String, String>> entries = new EntrySet(DATA.length);
		
		@Override
		public Set<Map.Entry<String, String>> entrySet() {
			return entries;
		}
		
		//定制的Entry,访问元数据
		private static class Entry implements Map.Entry<String, String> {
			
			int index;
			
			Entry(int index) {
				this.index = index;
			}

			@Override
			public int hashCode() {
				return DATA[index][0].hashCode();
			}

			@Override
			public boolean equals(Object obj) {
				return DATA[index][0].equals(obj);
			}

			@Override
			public String getKey() {
				return DATA[index][0];
			}

			@Override
			public String getValue() {
				return DATA[index][1];
			}

			@Override
			public String setValue(String arg0) {
				throw new UnsupportedOperationException();
			}
			
		}
		
		//定制的EntrySet,访问定制的Entry
		static class EntrySet extends AbstractSet<Map.Entry<String, String>> {
			
			private int size;
			
			public EntrySet(int size) {
				if(size < 0) {
					size = 0;
				} else if(size > DATA.length) {
					size = DATA.length;
				}
				this.size = size;
			}
			
			@Override
			public Iterator<Map.Entry<String, String>> iterator() {
				return new Iter();
			}

			@Override
			public int size() {
				return size;
			}
			
			private class Iter implements Iterator<Map.Entry<String, String>> {
				
				//该Entry访问元数据
				private Entry entry = new Entry(-1);
				
				@Override
				public boolean hasNext() {
					return entry.index < size-1;
				}

				@Override
				public Map.Entry<String, String> next() {
					entry.index ++;
					return entry;
				}

				@Override
				public void remove() {
					throw new UnsupportedOperationException();
				}

			}
			
		}
		
	}
	
	
	static Map<String, String> select(final int size) {
		return new FlyweightMap() {
			@Override
			public Set<java.util.Map.Entry<String, String>> entrySet() {
				return new EntrySet(size);
			}
		};
	}
	
	static Map<String, String> map = new FlyweightMap();
	
	public static Map<String, String> capitals() {
		return map;
	}
	
	public static Map<String, String> capitals(int size) {
		return select(size);
	}
	
	static List<String> names = new ArrayList<String>(map.keySet());
	
	public static List<String> names() {
		return names;
	}
	
	public static List<String> names(int size) {
		return new ArrayList<String>(select(size).keySet());
	}
	
	public static void main(String[] args) {
		System.out.println(capitals(10));
		System.out.println(names(10));
		System.out.println(new HashMap<String, String>(capitals(3)));
		System.out.println(new LinkedHashMap<String, String>(capitals(3)));
		
	}
}
//output
{CHINA=BeiJing, AMERICA=Washington, Canada=canada, Japan=dongjing, Australia=Sydney, Korea=souer}
[CHINA, AMERICA, Canada, Japan, Australia, Korea]
{Canada=canada, CHINA=BeiJing, AMERICA=Washington}
{CHINA=BeiJing, AMERICA=Washington, Canada=canada}

享元模式-AbstractMap结构

上一篇:仿百度文库文档上传页面的多级联动分类选择器


下一篇:libevent源码分析--epoll中的几个函数 epoll_init epoll_add epoll_dispatch