Prototype Pattern Lab
Lab: Enhancing the Game Character Application
Objective:
In this lab, you will extend the provided Game Character application. The existing application includes GameCharacter
as the prototype, Warrior
and Mage
as concrete prototypes, and a CharacterStore
to store and clone these characters. You will add a new game character called "Archer" to the existing system.
Instructions:
Start by examining the existing
GameCharacter
,Warrior
,Mage
, andCharacterStore
classes to understand how the Prototype design pattern is implemented.Create a new class called
Archer
that extendsGameCharacter
. Define theplay()
method forArcher
to print a message indicating that the Archer is shooting arrows.In the
CharacterStore
class, add theArcher
object to thecharacterMap
in theloadCache()
method.Update the
PrototypePatternDemo
class to demonstrate the use of the newArcher
class. Create cloned objects of theArcher
class and invoke theplay()
method on them.Test your application to ensure that it behaves as expected.
Bonus:
For extra credit, consider the following enhancements:
Add attributes such as
healthPoints
,attackPower
, anddefensePower
to theGameCharacter
class and their corresponding getter and setter methods. Make sure these attributes are correctly cloned in each prototype.Implement different behavior for the
play()
method based on these attributes.
Tips:
Remember that the Prototype pattern is used to create a new instance of a class from an existing instance. This can be useful when object creation is a costly operation and you need multiple similar objects.
Submission:
You should submit your completed Java files for GameCharacter
, Warrior
, Mage
, Archer
, CharacterStore
, and PrototypePatternDemo
.
Remember, the goal of this lab is to understand the Prototype pattern and how it can be used to efficiently create new objects when object creation is expensive or complex.