Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <services>
- <service type="0">...</service>
- <service type="1">...</service>
- <service type="1">...</service>
- </services>
- public enum Type {
- FILE("0","File"){
- @Override
- public Service createServiceInstance(ServiceAttributes attributes){
- return FileService.createInstance(attributes);
- }
- },
- NETWORK("1","Network"){
- // overridden createServiceInstance method
- }
- // declaration of string type variables for typeCode and typeName
- // constructor declaration with two arguments typeCode and typeName
- public abstract Service createServiceInstance(ServiceAttribtes attributes);
- }
- public class Service {
- //declaration of variables representing common data;
- private Type type;
- // getters and setters
- }
- public class NetworkService extends Service {
- private Link link;
- // getters and setters
- public static Service createInstance(ServiceAttributes attributes) {
- NetworkService ns = new NetworkService();
- ns.setName(attributes.getName());
- ns.setType(attributes.getType())
- ns.setLink(attributes.getLink())
- return ns;
- }
- }
- public class ServiceTypeAdapter extends XmlAdapter<ServiceTypeAdapter.ServiceAttributes, Service> {
- public ServiceAttributes marshal(Service service) throws Exception {
- // currently I need only the unmarshalling method
- // And I know this will never be required. What should I do here?
- // return null or throw new UnsupportedOperationException() ?
- return null;
- }
- public Service unmarshal(ServiceAttributes serviceAttributes) {
- Type serviceType = serviceAttributes.getType();
- return serviceType.createServiceInstance(serviceAttributes);
- }
- public class ServiceAttributes {
- private String name;
- @XmlAttribute
- private Type type;
- // declaration of attributes that will be present in
- // each of service classes
- // getters and setters
- }
- }
Add Comment
Please, Sign In to add comment