Spring实现Flyweight模式

时间:2015-11-11 06:05:34

标签: spring design-patterns javabeans flyweight-pattern

我在没有Spring的java编程中有一个Flyweight模式实现。请建议如何将其作为Spring托管Bean。它有静态方法调用和Inner类。目的是最初从CRM加载所有OptionSet以在应用程序中共享相同的OptionSet。这可以防止昂贵的CRM调用。用户被限制创建任何新的OptionSets,因此内部类。首先需要实现作为Spring bean,然后让它成为ApplicationContextAware来引用其他bean。我主要使用Spring XML配置进行bean定义。

public class OptionSetFactory{
    private static Map <String, Object>optionSets = new HashMap();

    //Inner class to restrict users creating OptionSet
    private class OptionSet implements IOptionSet{
        private String entityName;
        private String attributeName;
        private Hashtable<Integer, String> options;
        private IOrganizationService service;
        private static final String GUID_EMPTY = "00000000-0000-0000-0000-000000000000";
        private ApplicationContext context;

        OptionSet(String entityName, String attributeName){
            this.entityName = entityName;
            this.attributeName = attributeName;
            //this.options = options;   

            OrganizationRequest request = new OrganizationRequest();            
            request.setRequestName("RetrieveAttribute");            
            Guid guid = new Guid();
            guid.setValue(GUID_EMPTY);                  
            ParameterCollection paramCol = new ParameterCollection();
            KeyValuePairOfstringanyType kv0 = new KeyValuePairOfstringanyType();
            kv0.setKey("MetadataId");
            kv0.setValue(guid);
            paramCol.getKeyValuePairOfstringanyTypes().add(kv0);

            KeyValuePairOfstringanyType kv1 = new KeyValuePairOfstringanyType();
            kv1.setKey("EntityLogicalName");
            kv1.setValue(entityName);
            paramCol.getKeyValuePairOfstringanyTypes().add(kv1);
            KeyValuePairOfstringanyType kv2 = new KeyValuePairOfstringanyType();
            kv2.setKey("LogicalName");
            kv2.setValue(attributeName);
            paramCol.getKeyValuePairOfstringanyTypes().add(kv2);
            KeyValuePairOfstringanyType kv3 = new KeyValuePairOfstringanyType();
            kv3.setKey("RetrieveAsIfPublished");
            kv3.setValue(true);
            paramCol.getKeyValuePairOfstringanyTypes().add(kv3);
            request.setParameters(paramCol);
            try {
                OrganizationResponse response=service.execute(request);
                PicklistAttributeMetadata pickListAttrMetadata = (PicklistAttributeMetadata)response.getResults().getKeyValuePairOfstringanyTypes().get(0).getValue();
                OptionSetMetadata optionSetMetadata = pickListAttrMetadata.getOptionSet();
                for(OptionMetadata optionMetaData : optionSetMetadata.getOptions().getOptionMetadatas()){
                    //TODO populate OptionSet from optionsetMetadata
                }
            } catch (IOrganizationServiceExecuteOrganizationServiceFaultFaultFaultMessage e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public String getEntityName() {
            return entityName;
        }

        @Override
        public String getAttributeName() {
            return attributeName;
        }

        @Override
        public Hashtable<Integer, String> getOptions() {
            return options;
        }
    }
    //static block to load predefined OptionSets in HashMap
    static{
        OptionSetFactory factory = new OptionSetFactory();
        optionSets.put("dsl_type", factory.new OptionSet("dsl_type", "dsl_operationalstructure"));

    }
    //Static method calls for user to fetch OptionSet based on inputs
    public static IOptionSet getOptionSet(String entityName, String attributeName){
        return (IOptionSet) optionSets.get(entityName+"."+attributeName);
    }

    public static IOptionSet getOptionSet(String attributeName){
        return (IOptionSet) optionSets.get(attributeName.toLowerCase());        
    }
}

0 个答案:

没有答案