daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Jan 20th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <services>
  2.  <service type="0">...</service>
  3.  <service type="1">...</service>
  4.  <service type="1">...</service>
  5. </services>
  6.    
  7. public enum Type {
  8.  FILE("0","File"){
  9.     @Override
  10.     public Service createServiceInstance(ServiceAttributes attributes){
  11.       return FileService.createInstance(attributes);
  12.     }
  13.  },
  14.  NETWORK("1","Network"){
  15.     // overridden createServiceInstance method
  16.  }
  17.  // declaration of string type variables for typeCode and typeName
  18.  // constructor declaration with two arguments typeCode and typeName
  19.  public abstract Service createServiceInstance(ServiceAttribtes attributes);
  20. }
  21.  
  22. public class Service {
  23.   //declaration of variables representing common data;
  24.   private Type type;
  25.   // getters and setters
  26. }
  27.    
  28. public class NetworkService extends Service {
  29.   private Link link;
  30.   // getters and setters
  31.   public static Service createInstance(ServiceAttributes attributes) {
  32.      NetworkService ns = new NetworkService();
  33.      ns.setName(attributes.getName());
  34.      ns.setType(attributes.getType())
  35.      ns.setLink(attributes.getLink())
  36.      return ns;
  37.   }
  38. }
  39.    
  40. public class ServiceTypeAdapter extends XmlAdapter<ServiceTypeAdapter.ServiceAttributes, Service> {
  41.  public ServiceAttributes marshal(Service service) throws Exception {
  42.    // currently I need only the unmarshalling method
  43.    // And I know this will never be required. What should I do here?
  44.    // return null or throw new UnsupportedOperationException() ?
  45.    return null;
  46.  }
  47.  public Service unmarshal(ServiceAttributes serviceAttributes) {
  48.     Type serviceType = serviceAttributes.getType();
  49.     return serviceType.createServiceInstance(serviceAttributes);
  50.  }
  51.  
  52.  public class ServiceAttributes {
  53.    private String name;
  54.    @XmlAttribute
  55.    private Type type;
  56.    // declaration of attributes that will be present in
  57.    // each of service classes
  58.  
  59.    // getters and setters
  60.  }
  61. }
RAW Paste Data
Top