-1

Yesterday I made this question: Java function on function

For help and get marked as Duplicate but I think I didn't get understand there what I want and now I try again.

I want methods can be only called on methods for example we have the class Roads and on the road we will go a Way.

Roads.Way1()

After we choose the Way1 we will go to Path1

Roads.Way1().Path1()

But if we choose Way2

Roads.Way2()

We are not able to go to Path1() cause Way2() goes to Garden1() so

Roads.Way2().Garden1()

So what I try to say you can only use the methods(functions) in a wanted way and I saw this on different API or Library. So for the good understanding

Way1 goes to Path1 and ISN't able to go to Garden1

Way2 goes to Garden1 and ISN't able to go to Path1

So how to manager that I can make different roads that has there own ways so I could make like

Roads.Way1().
/* 
Goes to: 
Path1()
Fountain()
Market()
*/

And Way to cant access them and can only use there own destinations.

Community
  • 1
  • 1

1 Answers1

3

I think what you are asking for is: how can I express "control flow" using "language features". And well, there would be ways to get there: you would need a lot of different classes (or maybe interfaces); so something that is a "Way2" would simply not offer a "Path1" method.

But: doing so sounds like bad idea. It will work fine initially, but as soon as you start extending your system, you will be running into problems all the time:

"Hmm, I need to change Way2; it should now allow to go Path1; but uups; there are some Way2-thingies that should not allow Path1; so I actually need a Way3-thingy" and so on. Chances are extremely high that maintaining this code will turn into a nightmare very soon.

I know, this is just an opinion, but my gut feeling is: you are looking for the wrong solution to your problem. Instead, you should spent time on identifying what your actual problem is; and then think about better ways to solve that problem!

GhostCat
  • 137,827
  • 25
  • 176
  • 248