I am doing an UML diagram for my Java Application, and I came across with the following doubt:
I have a class called Information, its attributes depends on what kind of Node
it is.
For example:
If Node = TrafficLight
, Information will contain the color of the light.
But if Node = Signal
, it will contain the maximum speed.
How can I implement that, both in UML and code?
Thank you all for your answers, I will try some of them and I will post the solution!
I'm going to assume that Node is a child of the Information object. In this case, you would need an object for each of the possible outcomes which Node could be. The best way I can explain this is...
package.name.Node
|
|_package.name.Node.Information
|
|_Information.TrafficLight(String color)
|
|
|_Information.Signal(Double speed)
I hope that helps. Keep in mind that I've not programmed in Java in a long while but I happened across this and remembered doing something similar in college ^_^; since it's just UML you should be able to get an idea of where to go with your project from his, assuming I'm not just making a fool of myself. Like I said, just trying to help.
You can invoke method overloading on constructors in java.
public class myClass{
public myClass(){
//do something
}
public myClass(String someInput){
this.someInput=someInput;
}
}
That is what I finally did:
Create a method called addNode(nodeType)
addNode(nodeType){
switch (nodeType){
case trafficlight:
new TrafficLight //call one constructor
case signal:
new Signal //call other constructor
}
}
Thanks for your help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.