For this lab, you will introduce the Decorator Pattern into the bees and garden system you wrote for the previous lab. Each team member must introduce at least one decorator, but a team member can implement additional decorators if they like. Be sure to fix issues identified in the previous lab as well. The intent is that this would be done using the same groups as the previous lab; if this would be difficult for you, talk to your instructor as soon as possible.
Each team member will apply the Decorator Pattern to the code from the previous lab in a distinct way. For example, one might decorate flowers while another decorates bees, or one might decorate the bee that follows a pattern while the other decorates the bee that targets a flower.
All decorators must have the following properties:
There must be at least two types of decorators for each type of object that gets decorated.
Decorators must modify the image visibly so users can tell which bees or flowers should have changed behavior.
Decorators must impact behavior in an observable way; the user must be able to see that decorated objects behave differently. They must do this by providing “new” implementations of methods in the object’s interface. For example, adding new behavior to a Bee’s movement or a Flower’s impact on bees that land.
You must not modify the decorated object while implementing a decorator. You may realize that you should have refactored a class to promote some operation to the domain level - for example, moving the code to visit a flower into a method called
visit, but the promoted operation must use terminology that makes sense to a domain expert such as a gardener.It must be possible to “stack” decorators; that is, it must be meaningful to decorate a previously-decorated object, and it must be possible to identify how a double-decorated object differs in behavior from a single-decorated object.
There must be a way for the user to control how decorators are applied. One model is to allow the user to enable or disable specific decorators and apply the decorators randomly: say a button to add a specific decorator to a random subset of the objects. Another is the user could have an interface allowing them to create a specific number of objects with collections of decorators.
Sample Decorations
Here are some ideas. But we encourage students to be creative in identifying their own ideas:
Make decorated bees move faster or slower.
Make decorated bees zig-zag along their intended path, with extra decorations resulting in bigger zig-zag patterns.
Increase the effect of poisonous or nectar-producing flowers.
Add armor to bees that peals off when they hit a poisonous flower, giving them an opportunity to survive more negative hits.
Add a “I’m not here” decorator to a flower so bees just pass by the flowers for a period.
Make specific bees buzz happily when they arrive at nectar-producing flowers. Multiple decorators might result in higher-pitched buzzing.
Make some bees into pollinators that make flowers they visit “go to seed”, taking twice as much energy as the flowers offer and making the visited flowers useless to other bees.
Talk to your instructor about your ideas.
Implementation Details
The intent is that all decorators would be determined at object creation time. It is possible to use the decorator pattern with objects that change state, but this introduces significant complexities. In particular, you will need a “dummy” object that holds the primary reference to the decorated object. If you are interested in doing the extra work, discuss it with your instructor.
Each student in the group must implement at least one, distinct decorator.
When applying the Decorator Pattern, you must use the word Decorator in
the class names of all decorators; this allows instructors to identify
where you have applied the pattern. In addition, follow the notes and make
sure the class being decorated is an interface class. This introduces two
issues:
Many solutions derive the
BeeandFlowerclasses from an abstract garden object class. You will want to introduce (say) aBeeInterfaceclass that is not derived from garden object since you cannot derive an interface from a non-interface class. Then your abstractBeeclass would extend the garden object but implement the bee interface.If you have multiple types of bee objects that can be decorated, making them implement a bee interface leads to duplicated code between the bee objects. Likewise, the same happens if you have multiple types of flower objects derived from a flower interface class. Fix this by creating an abstract bee or flower that implements the flower or bee interface, and putting all common code into the abstract class.
Submission
Ensure that your user experience is still appropriate. This includes making it easy for your instructor to verify the new features in a minute or so.
Checked that issues identified in the previous lab have been fixed. Talk to your instructor if an issue is too difficult to fix without an extensive rewrite,
As for the previous lab, meet your instructor’s coding standard. At a minimum, your classes must include documentation about their responsibilities.
Push your changes to your Git repository, continuing to work on the main branch.
When you are finished, create a
PDFcapturing what you accomplished. Your instructor may have specific requirements to meet, but if the instructor does not specify then your PDF must contain the following:- A minimal-solution diagram capturing the domain level classes (with attributes and operations) and including how you implemented the Decorator Pattern. As before, do not include the
Mainclass or JavaFX classes, and do not reverse-engineer your solution to create this diagram. - Screen shots of your solution in action. You do not have to capture every behavior, but do capture that bees move in the correct directions and that they change energy levels. Be sure to describe what the screen shots show.
- A paragraph describing who implemented which decorator or decorators and your experience with doing that implementation.
- A discussion of any known problems, either in the implementation or the design. Typically, documenting known problems reduces the penalty. You can use MSWord or similar tools to create the PDF. Upload it to Canvas when done.
- A minimal-solution diagram capturing the domain level classes (with attributes and operations) and including how you implemented the Decorator Pattern. As before, do not include the
See Canvas for any additional submission instructions.