java - How to pass params into a Factory for object creation? -


i have recipienttypesfactory going create objects of type recipienttype. recipienttypes object have followin hierarchy:

public interface recipienttype{     public abstract object accept(recipienttypevisitor v); }  public class dynamicgrouptype implemetns recipienttype{      private integer dynamicgroupid;      public object accept(recipienttypevisitor visitor){         return visitor.visit(this);     }     //get, set }  public class staticgrouptype implements recipienttype{      private integer staticgroupid;      public object accept(recipienttypevisitor visitor){         return visitor.visit(this);     }     //get, set } 

recipienttypesfactory looks follows:

public enum recipienttypeenum {     static_group, dynamic_group }  public class recipienttypesfactory{     private map<recipienttypeenum, recipienttypecreator> creators;      public recipienttype createrecipienttype(recipienttypeenum t){         return creators.get(t).create();     } } 

i'm not going provide actual definition of recipienttypecreator , hierarchy because don't think it's important.

now have controller:

public class createmailingcontroller{     private recipienttypesfactory recipienttypesfactory;     private integer dynamicgroupid;     private integer staticgroupid;     private recipienttypeenum selectedtype;      //get, set, other staff      public void createmailing(){         type t = recipienttypesfactory.createrecipienttype(selectedtype);         //how initialize t's field appropriate value?     } } 

the thing recipienttypesfactory , creators know nothing createmailingcontroller's dynamicgroupid , staticgroupid values. values setting user web-interface. therefore factory cannot initialize corresponding field of type create these values.

recipienttypesfactory , creators spring beans.

question: how can pass values of dynamicgroupid , staticgroupid factory in flexible way , avoid wiriting switch-case code? possible?

maybe there's patter purposes. in fact factory creating prototype of object.

you can use map avoid switch cases,like below:

private static final map<string, recipienttype> factorymap = collections             .unmodifiablemap(new hashmap<string, recipienttype>() {                 {                     put("dynamicgroupid", new recipienttype() {                         public recipienttype accept() {                             return new dynamicgrouptype();                         }                     });                     put("staticgroupid", new recipienttype() {                         public recipienttype accept() {                             return new staticgrouptype();                         }                     });                 }             });      public recipienttype createrecipienttype(string type) {         recipienttype factory = factorymap.get(type);         if (factory == null) {          }         return factory.accept();     }  

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -