This discussion is archived
4 Replies Latest reply: Dec 11, 2012 1:16 AM by meandmycode RSS

Connect configuration data with operations

meandmycode Newbie
Currently Being Moderated
Hi,

We have the following design issue that I want your view on.

We have a number of resources that we should test. We store the configuration data in xml files and read the data into resource objects.
So the Resource class contains a reference to a ConfigurationData object ( see below).

//Not the complete class content but just to illustrate the
public class Resource {

private ConfigurationData data;

public Resource(ConfigurationData data){
this.data = data;
}

public ConfigurationData getConfigurationData(){
return data;
}

//Return resource type X,Y,Z .....
public String getType(){
return data.getType();
}

}

So there will be a number of instances of resource objects since we have many different types of resources.

So in the code we get all the resources like:

Resource [] resource = ResourceManager.getAllResources();

So now to the issue we have and want to discuss:

On all resources we would like to perform operations like:

- boot()
- setup()
- config()
- setupssh()

there will be more operations in the future....

So we have a simple NodeOperation class for this:

public abstract class NodeOperation{
public void boot(){
//impl code
}
public void setup(){
//impl code
}
public void config(){
//impl code
}
public void setupssh(){
//impl code
}


}

But not all operations will work on all resources. So the idea we have is to create subclasses for each type ( X,Y,Z) of resource where we define:


public class XNodeOperation extends NodeOperation{

public void setupssh(){
throw new UnsupportedOperationException();
}


}

We have designed an object called node that will contain a resource object.

pulbic class Node {

private Resource resource;

public Node (Resource resource){
this.resource = resource;
}

}

It is on this class that we will make operations on like:

Node node = new Node(resource);
node.setupssh();

How can we make sure that XNodeOperation class is used for Node of Type X when
type:

node.setupssh();


br,

//mike
  • 1. Re: Connect configuration data with operations
    EJP Guru
    Currently Being Moderated
    Change your design. Separate NodeOperation into as many interfaces as are required for the various methods. For example:
    interface Bootable
    {
       void boot();
    }
    Then at runtime you can use `instanceof` to see whether the `boot()` operation is supported, and in any case trying to cast something to Bootable that isn't will fail.
  • 2. Re: Connect configuration data with operations
    meandmycode Newbie
    Currently Being Moderated
    Hi,

    Do you mean that the Node class should implement the interfaces?

    The supported methods depends on the type of Resource used in the Node class.

    Is it possible to show how you mean using the Node class?

    br,

    //mike
  • 3. Re: Connect configuration data with operations
    EJP Guru
    Currently Being Moderated
    No, I mean there should be as many node implementation classes as you need that do or don't implement the various interfaces.
  • 4. Re: Connect configuration data with operations
    meandmycode Newbie
    Currently Being Moderated
    ok if I have two classes like:
    public class NodeX  implements Bootable{
    
    
        pulblic NodeX(Resource resource){
           this.resource = resource;
    
       }
    
    
       public void boot(){
          //impl of boot
    
       }
    
    
    
    
    
    }
    
    
    public class NodeY implements Bootable,Upgradable {
       
        pulblic NodeY(Resource resource){
           this.resource = resource;
    
       }
    
    
        public void boot(){
          //impl of boot
    
        }
    
        public void config(){
           //impl of setup
        }
    
        public void setup(){
           //impl of setup
        }
    
    
    }
    So when I have a resource of type X then should I create the NodeX? Is there a simple way of doing this? Should I use a Factory to create the different subclasses ( subclass of Node)?


    Just a small note. What if resource X and Z have the same impl. of the boot() how shall I handle that? Do I need to make the impl. twice?

    br,

    //mike

    Edited by: meandmycode on Dec 11, 2012 1:14 AM

    Edited by: meandmycode on Dec 11, 2012 1:15 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points