$29
In this exercise we will modify the Module 6 solution for the HappyBirthday application to create a stream (“magic pipeline”) to process our birthday cards. We will also change our main application class to act as a singleton object.
The rules for creating a singleton:
1. Ensure that only one instance of the class exists by declaring a static member variable (instead of instantiating it in the main method as a local variable)
2. Restrict access to only that instance by declaring all constructors of the class to be private
3. Provide a public static method which returns the singleton instance
private static HappyBirthdayApp hba = new HappyBirthdayApp(); private HappyBirthdayApp() { }
public static HappyBirthdayApp getApp() { return hba;
}
We implement the stream by wrapping a queue (linked list) and use the stream in the application's main method to iterate through the birthday cards in the queue.
• Use a Queue<LinkedList> to act as message queue for the dispatcher private Queue<BirthdayCard> queue = new LinkedList<BirthdayCard>(); private Stream<BirthdayCard> stream = queue.stream();
A generic Dispatch interface has been created which is responsible for dispatching cards (adding them to the queue). This interface is stored in a new package subcomponent called “dispatch”.
• Dispatcher.java
• D. Singletary
• 2/15/23
• A generic interface for dispatching messages.
package edu.fscj.cop3330c.dispatch;
public interface Dispatcher<T> {
public void dispatch(T t);
}
The sendCard method in the HappyBirthApp class has been changed to dispatch the cards using a lambda expression:
public void sendCard(BirthdayCard bc) { Dispatcher<BirthdayCard> d = (c)-> {
this.queue.add(c);
};
d.dispatch(bc);
}
At the end of the main method, we process the stream using the forEach method:
// process the stream
System.out.println("starting forEach"); hba.stream.forEach(System.out::print);
Sample output (no changes from module 6, since we are only modifying internals for this version):
Here are the birthdays:
Miles Bennell:
Sorry, today is not their birthday.
Becky Driscoll:
Birthday card for Becky Driscoll
bdc-child.jpg(JPG)
sending email to Becky.Driscoll@email.test
Jack Belicec:
Birthday card for Jack Belicec
|--------------------------------------------------------
|
| Happy Birthday! Hope your day is as awesome as you are |
|
and all your birthday wishes come true!
|
|--------------------------------------------------------
|
sending email to Jack.Belicec@email.test
Theodora Belicec:
Birthday card for Theodora Belicec
bdc-adult.jpg(JPG)
sending email to Theodora.Belicec.@email.test
Sally Withers:
Birthday card for Sally Withers
|------------------------------------------------------------
|
|
Happy Birthday!
|
|
May this special day bring you cherished memories, joy
|
|
in the present, and hopes for the future.
|
|
Wishing all of your birthday dreams and wishes come true! |
|------------------------------------------------------------
|
sending email to Sally.Withers@email.test