• Post Reply Bookmark Topic Watch Topic
  • New Topic

Design creativity problem - A kingdom this time  RSS feed

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This time I have a kingdom design creativity problem the specifications of which are as follows.

There is one king and the king does two things - 1. Listen to common man's issues.
2. Provide solutions to common man's issues.


There are five officers - and exactly five officers - who provide common men the access to king.

And there are many common men.

Common men cannot directly approach the king for their issues. They have to approach the king only via an officer. Each officer deals with a specific issue that determines which officer a common man must approach. The officers are unbiased in providing the common men the access to king. The king is unbiased towards the officers in handling requests. For simplicity we can choose anything as issues, anything as solutions, and any logic to map problems to solutions. Besides the above stated requirements, we can use abstraction and imagination for the rest of the things.

The only hint I have is - there are more than one ways of solving this problem and it can be solved by using simple concepts.

I've figured that King should be a singleton class and 'listen to common man's issues' and 'provide solution' are going to be synchronized methods.

I can't figure anything else :-) Are there going to be advanced threading concepts involved? I don't know much about them yet.

Should Officers be something like the following?



Am I going correct? To be honest, I have no idea what classes I should have or if they should be interfaces/abstract classes. And I have no clue on how to enforce that common men can get king access only via officers.

Any pointers/suggestions would be a great help.

Thanks,
Chan.


 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to use Threading. You would think that the King would solve only one issue at a time, so the King will be the bottleneck.

You can easily implement this by having 5 queues; one for each officer. The King can pick an issue from each queue in a round robin fashion.
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about having an Officer class. Then for the rest of the 5 officers you can subclass from the Officer class and use polymorphism to get to their different functions or roles.


 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestions. I really appreciate the kind gesture.

Why do you need to use Threading. You would think that the King would solve only one issue at a time, so the King will be the bottleneck.


Yes, I got confused because there is just one king but I didn't realize the processing is going to be sequential for the king.

You can easily implement this by having 5 queues; one for each officer. The King can pick an issue from each queue in a round robin fashion.


Even I've planned to implement it using queues. I'm still deciding whether 5 queues is going to be better than 10 queues. Now since I am going to have queues, it seems it doesn't matter if the officer isAvailable = true.



Thanks for the suggestion. It just didn't occur to me. I will give it some more thought. But then I thought that the different officers don't differ in the basic structure. So I could have them all as objects with different state ( like department = health, their request queue and/or response queue ). But I will give more thought to that suggestion and see it can simplify things further.

Now the specific bits that I'm having difficulty in planning are the interaction bits ( methods -- parameters and return types ) and how to ensure that only officers get to interact with the king and that only officers get to interact with common men. Also since there are going to be request queues, I'll need some mechanism to track which common man had the issue. Or may be this thing won't work out or may be there are simpler ways.. May be I'm just complicating it. Perhaps it needs more thought.
I'll try to work it out. Just it seems a little complicated.

Chan.

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestions.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about coding for now. Do it on paper. If you were to do this in real life, would you put the common man in the queue? or will you ask the common man write down the issue on paper and give it to the officer, and officer puts the issue in queue? If the common man is in queue, it's pretty easy for the king to give the solution to the common man, because the king has direct access to the man. However, you might run into problem if a man comes with 2 issues. OTH, if you put the issue on paper and pass the paper to the king via the queue, and the king solves the issue, how will the kings' solution get back to the common man?

Think about how you will do it in real life. There used to be a "brainstorming" technique that became semi popular in the 90s where people will get in the room and try to "act out" the problem. For example, one person can be the king, 2 people can be officers and other 4 people can be the common men. Of course, you need a lot of people to run this play, but it can be effective. The way I do it is imagine little gnomes jumping around. Anthromorphizing your components (pretending that they are people) is a good way to design, plus it's fun. You have a pretty good start with the problem statement itself here. It's already anthromorphized. You just need to use your imagination.
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One basic thing you can note down here:

what functions king will perform will be methods like:
hearProblemFunction();
givingSolutionFunction();

the state will become in which King/officers are present like:
busy, free



So if you will see the functions performed by someone are actually methods and states in which someone is present become variables.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good points, Jayesh. And they've really helped giving shape to some of my relatively vague ideas. I greatly appreciate it.

I think it will not be a very good idea to have common men's reference copied all along. It'll make all the components very tightly coupled. I think it'd be a lot better if the officers also maintained a mechanism to track the common men and their issue count in a separate data structure.

But still there are somethings about this problem that are still vague in my mind and I'm working on them. Like you said, I haven't yet begun with writing code cause I've only been working it out on a piece of paper, drawing those boxes ( simulating that gnome like exercise with the help of boxes ). Hopefully I will have a better status on this design problem in the coming days.

I'm still working on it.

Thanks and greetings,
Chan.

Now I see why you said that breaking a problem into tiny problems is a skill that needs to be developed over time. You cannot start abstracting out tiny bits without having a picture of the concrete.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!