Question: Below is the code given. Your job is to add a new class that utilizes the Decorator Pattern. Code must continue to be done in

Below is the code given. Your job is to add a new class that utilizes the Decorator Pattern. Code must continue to be done in java, and the Warrior and Builder Classes must remain abstract and untouched. Create a new decorator class named ArmoredWarriorDecorator which has the defense field multiplied by 2. This change should affect all method calculations. You can confirm it is working correctly by running this line of code.
Warrior warrior = new ArmoredWarriorDecorator(new AggressiveWarrior.Builder(1).defense(10).build());
public abstract class Warrior {
private final int level;
private final int attack;
private final int defense;
protected Warrior(Builder builder){
this.level = builder.level;
this.attack = builder.attack;
this.defense = builder.defense;
}
public int getLevel(){return this.level;}
public int getAttack(){return this.attack;}
public int getDefense(){return this.defense;}
public final double calculatePower(){
return calculateAttack()+ calculateDefense()+ calculateBoost();
}
protected int calculateAttack(){
return this.attack;
}
protected int calculateDefense(){
return this.defense;
}
protected double calculateBoost(){
return 0;
}
public static abstract class Builder {
private final int level;
private int attack;
private int defense;
public Builder(int level){
this.level = level;
}
public Builder attack(int attack){
this.attack = attack;
return this;
}
public Builder defense(int defense){
this.defense = defense;
return this;
}
public abstract Warrior build();
}
}
public class AggressiveWarrior extends Warrior {
private AggressiveWarrior(Builder builder){
super(builder);
}
@Override
protected int calculateAttack(){
return (getLevel()*2)+ getAttack();
}
@Override
protected int calculateDefense(){
return getLevel()+ getDefense();
}
@Override
protected double calculateBoost(){
return (double)getAttack()/2;
}
public static class Builder extends Warrior.Builder {
public Builder(int level){
super(level);
this.attack(3).defense(2);
}
@Override
public AggressiveWarrior build(){
return new AggressiveWarrior(this);
}
}
}

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!