Question: Write a java code about following problem: A design pattern is a general reusable solution to a commonly occurring problem in software design. Design patterns
Write a java code about following problem:
A design pattern is a general reusable solution to a commonly occurring problem in software design. Design patterns are not finished products but rather represent best practices used by experienced OO software developers. One such common pattern is the "Observer Pattern". The UML diagram for the Observer pattern is shown below:
observers
Subject (also known as an Observable) - an interface (or abstract class) that defines operations for registering and deregistering observers
ConcreteSubject (also known as a ConcreteObservable) - concrete Subject class that maintains the state of the object and when a change in the state occurs it notifies the attached Observers. This is where the data comes from.
Observer - an interface (or abstract class) that defines what method(s) are used to notify this object
ConcreteObserver - concrete implementations of the Observer interface.
In this assignment you are to use the Observer Pattern to simulate a hospital message broadcast system. Since we can't really deal with the networking aspects we'll pretend a bit.
When running, your program can have several frames visible. All but one of these frames are identical and display a set of channels (which is configured when they are set up). Let's call these frames "monitors" (pretending that these frames each represent an output- only TV monitor). These have no user interactivity (i.e. buttons, menus, etc.) since users dont interact with them.
| < Subject |
| registerObserver() removeObserver() notifyObservers() |
| < Observer |
| update() |
subject
The four participants in this pattern are:
| ConcreteSubject |
| registerObserver() removeObserver() notifyObservers() getState() setState() |
| ConcreteObserver |
| update() // other Observer-specific methods |
A single frame represents the communications station of the hospital - let's call it the message console. A user interacts with the message console to send a message to a given channel. The message console frame should use a menu structure to select the channel that a given message will be sent to. On-screen, it should have a JTextArea where a message can be typed, and a SEND button to send it. There should be a JLabel to show the currently selected channel (by name). Instead it contains a single button labeled "Create monitor" to create a new monitor frame (as described in the previous paragraph).
The program will have a series of channels:
0: EMERGENCY ALL-CALL MEDICAL 1: Doctors Messaging (medical) 2: Nursing Station Messaging (medical) 3: Respiratory Services Messaging (medical) 4: Lab Messaging (non-medical)
5: Custodial Staff Messaging (non-medical) When the program starts, it should have monitors as follows:
A monitor in the Doctors Lounge, monitoring Channels 1 and 2 A monitor at each of the three Nurses Stations, monitoring Channel 2 A monitor in Respiratory Services, monitoring Channels 2 and 3 A monitor in the Lab, monitoring Channel 4 only A monitor in the Custodians Bay, monitoring Channel 5 only
So, all monitors in medical service areas will monitor and process channel 0 messages, and display them with asterisks (***Like this***), as well as any other channels defined.
With respect to the Observer Pattern, the message console is the ConcreteSubject and the monitors are the ConcreteObservers. When each monitor frame is instantiated, it should register itself as an observer with the message console. The message console will store references to all registered observers. When the user changes something on the message console, the notifyObservers method should be called to walk through the list of registered observers, sending each a notification via its update method. Although the UML diagram is intentionally vague to allow flexibility, you will need to send a channel ID and text as a parameter(s) to the update method. Consider creating a class to encapsulate this data.
The use of the Subject and Observer interfaces (or abstract classes) is mandatory for the assignment. Many patterns are defined with several interfaces included. The purpose is one of good design. If our system were to expand in the future to include other types of message consoles or other types of observers, we'd be ready for them (as long as you define things in terms of the interface types).
Note that, in this assignment, we have no real need for the removeObserver method in the Subject, but implement it nonetheless, since we want to be ready for future expansion.
Hints:
You will find that a JTextArea is good for both typing messages, and for displaying them. Recall that it has a .append method; you will also want to use the .setLineWrap method. A good size for all JFrames is 500 x 300; a good size for JTextAreas is 10,80
I recommend you extend JPanel, not JComponent, as JComponent doesn't always talk properly to JTextAreas that have been added to it - I know the author has a penchant for JComponents, but I do not.
Here's what my Hospital Comms system looks like:
Note that HospitalCommsViewer contains the main method.
The MessageConsole creates the Monitor objects. My monitor objects are set up to "call back" the MessageConsole and register themselves to the list of Observer objects. They observe the MessageConsole, which is Observable. These concepts are defined at the beginning of the
problem; the MessageConsole is a concrete subject, and Observable is the subject interface. Monitor is the concrete observer object, and Observer is the associated interface. MessageData is the thing which holds messages sent from the MessageConsole to the Monitors - when a new MessageData object is created and made available, the Monitors are notified to pick up the data and process it.
Finally, here's a picture of the program in action.
Working In Pairs
You may work in a group of size <= 2. You may not work in a size > 2.
BOTH people must work on BOTH problems (you cannot split the assignment by problems, you each have to do work on each problem.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
