Now we can create orders. Let's make it so they are automatically sent to the cook.
There are many different ways to implement this functionality.
Read about the Observer pattern at http://en.wikipedia.org/wiki/Observer_pattern
It's already implemented in Java, and we're going to use it.
1. Create a Cook class in the kitchen package. It's going to handle the cooking. The constructor will take the cook's name, which will be output by the toString method.
2. Tablet creates orders, and Cook prepares them. Properly assign Observer and Observable to Tablet and Cook.
3. The void update(Observable observable, Object arg) method, which must be implemented, has two parameters.
- observable is the object that sent us the value
- arg is the value itself—in our case, an Order object
At this stage, we'll only simulate preparing the orders by displaying "Start cooking - " + order
3. Let's write the main method.
For the Observable object, add our own Observer object. See item 2 and the description of the pattern in Wikipedia
We give the cook a name. The name doesn't affect the tests. In my version, the cook's name is Amigo. :)
Let's check the console output. Example of my output:
Your order: [Soup] from Tablet{number=5}
Start cooking - Your order: [Soup] from Tablet{number=5}
4. Don't forget to do the following immediately after creating the order and displaying it on the console (find this place in the code):
4.1. Set the flag using setChanged()
4.2. Send the order to the observer using notifyObservers(order);
5. We'll also make a small change. Make it so that the createOrder method returns the current order, or null if the order couldn't be created.
- The Cook class constructor must take a string as an argument and initialize the cook's name (name field).
- The Cook class's toString method must return the name field.
- The Cook class must implement the Observer interface.
- The Tablet class must be a descendant of the Observable class.
- The Tablet class's createOrder method must call the setChanged method.
- The Tablet class's createOrder method must call the notifyObservers method with the current order as the argument.
- The Cook class's update method should display a string formatted according to the task conditions.
- The Restaurant class's main method should create a new cook and use the addObserver method to add it the tablet as an observer.