Question: Please write code that follows the UML in java. You will be implementing the Observer Pattern to create a scenario with watchman and city folk.

Please write code that follows the UML in java.

Please write code that follows the UML in java. You will be

You will be implementing the Observer Pattern to create a scenario with watchman and city folk.

Subject:

Our subject is the watchman. They are hanging out at the watch tower. When they see trouble coming they play the trumpet. One trumpet call is a moderate warning, two calls is a sever warning. Before notifying the observers they display one of the following messages.

"WARNING: 1 trumpet was played!"

"WARNING: 2 trumpets were played!"

Observers:

Our observers are the town folk. We have a Teacher, a Shop Owner, and a Knight. When the watchman issue trumpet calls, they are going to act based on how many calls they hear. They will display the corresponding messages.

Teacher:

1 Trumpet Call: "Teacher: Helps get every kid home safe"

2 Trumpet Calls: "Teacher: Brings all students to the underground shelter"

Shop Owner:

1 Trumpet Call: "Shop Owner: Close down shop and head home"

2 Trumpet Calls: "Shop Owner: Drops everything and find nearest hideout"

Knight:

1 Trumpet Call: "Knight: Helps everyone get home safe"

2 Trumpet Calls: "Knight: Prepares for battle"

DRIVER:

package observer;

public class TownDriver { public static void main(String[] args) { System.out.println("A Happy Little City");

Watchman watchman = new Watchman(); Observer fredrick = new ShopOwner(watchman); Observer lillian = new Teacher(watchman); Observer monte = new Knight(watchman);

System.out.println(" ----------------------- ");

watchman.issueWarning(1);

System.out.println(" ----------------------- ");

watchman.issueWarning(2); } }

OUTPUT EXAMPLE:

A Happy Little City

-----------------------

WARNING: 1 trumpet was played! Shop Owner: Close down shop and head home Teacher: Helps get every kid home safe Knight: Helps everyone get home safe

-----------------------

WARNING: 2 trumpets were played! Shop Owner: Drops everything and find nearest hideout Teacher: Brings all students to the underground shelter Knight: Prepares for battle

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!