Question: Modify the provided code to add 2 decorator pattern classes. You will have an ArmoredWarriorDecorator Class which builds off of the AggressiveWarrior class, and a

Modify the provided code to add 2 decorator pattern classes. You will have an ArmoredWarriorDecorator Class which builds off of the AggressiveWarrior class, and a StrongWarriorDecorator for DefensiveWarriors.
See code snippet for examples:
Warrior warrior1= new ArmoredWarriorDecorator(new AggressiveWarrior.Builder(1).build());
Warrior warrior2= new StrongWarriorDecorator(new AggressiveWarrior.Builder(1).build());
Armored Warriors have their defense field multiplied by 2. This should affect all method calculations. Strong Warriors have their attack field multiplied by 2. This should affect all method calculations.
public class Warrior {
// Immutable fields to hold the level, attack, and defense values
private final int level;
private final int attack;
private final int defense;
// Constructor is protected to prevent direct instantiation
protected Warrior(Builder builder){
this.level = builder.level;
this.attack = builder.attack;
this.defense = builder.defense;
}
// Getter methods to access the field values
public int getLevel(){
return level;
}
public int getAttack(){
return attack;
}
public int getDefense(){
return defense;
}
// Template method: defines the overall algorithm
public double calculatePower(){
return calculateAttack()+ calculateDefense()+ calculateBoost();
}
// Abstract methods to be overridden by subclasses
protected int calculateAttack(){
return attack;
}
protected int calculateDefense(){
return defense;
}
protected double calculateBoost(){
return 0;
}
// Builder class to create Warrior instances
public static class Builder {
private final int level;
private int attack =0;
private int defense =0;
public Builder(int level){
if (level <0){
throw new IllegalArgumentException("Level cannot be negative");
}
this.level = level;
}
public Builder attack(int attack){
if (attack <0){
throw new IllegalArgumentException("Attack cannot be negative");
}
this.attack = attack;
return this;
}
public Builder defense(int defense){
if (defense <0){
throw new IllegalArgumentException("Defense cannot be negative");
}
this.defense = defense;
return this;
}
public Warrior build(){
return new Warrior(this);
}
}
}
public class AggressiveWarrior extends Warrior {
// Constructor is private to enforce Builder pattern
private AggressiveWarrior(Builder builder){
super(builder);
}
// Overrides the calculateAttack method from the base class
@Override
protected int calculateAttack(){
// Attack calculation: attack field +(2* level field)
return getAttack()+2* getLevel();
}
// Overrides the calculateDefense method from the base class
@Override
protected int calculateDefense(){
// Defense calculation: defense field + level field
return getDefense()+ getLevel();
}
// Overrides the calculateBoost method from the base class
@Override
protected double calculateBoost(){
// Boost calculation: attack field /2
return (double) getAttack()/2;
}
// Builder class for AggressiveWarrior
public static class Builder extends Warrior.Builder {
public Builder(int level){
super(level);
// Initial attack and defense values for AggressiveWarrior
this.attack(3).defense(2);
}
@Override
public AggressiveWarrior build(){
return new AggressiveWarrior(this);
}
}
}
public class DefensiveWarrior extends Warrior {
// Constructor is private to enforce Builder pattern
private DefensiveWarrior(Builder builder){
super(builder);
}
// Overrides the calculateAttack method from the base class
@Override
protected int calculateAttack(){
// Attack calculation: attack field + level field
return getAttack()+ getLevel();
}
// Overrides the calculateDefense method from the base class
@Override
protected int calculateDefense(){
// Defense calculation: defense field +(2* level field)
return getDefense()+2* getLevel();
}
// Overrides the calculateBoost method from the base class
@Override
protected double calculateBoost(){
// Boost calculation: defense field /2
return (double) getDefense()/2;
}
// Builder class for DefensiveWarrior
public static class Build

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!