Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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 warrior new ArmoredWarriorDecoratornew AggressiveWarrior.Builderbuild;
Warrior warrior new StrongWarriorDecoratornew AggressiveWarrior.Builderbuild;
Armored Warriors have their defense field multiplied by This should affect all method calculations. Strong Warriors have their attack field multiplied by 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 WarriorBuilder 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 ;
Builder class to create Warrior instances
public static class Builder
private final int level;
private int attack ;
private int defense ;
public Builderint level
if level
throw new IllegalArgumentExceptionLevel cannot be negative";
this.level level;
public Builder attackint attack
if attack
throw new IllegalArgumentExceptionAttack cannot be negative";
this.attack attack;
return this;
public Builder defenseint defense
if defense
throw new IllegalArgumentExceptionDefense cannot be negative";
this.defense defense;
return this;
public Warrior build
return new Warriorthis;
public class AggressiveWarrior extends Warrior
Constructor is private to enforce Builder pattern
private AggressiveWarriorBuilder builder
superbuilder;
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 level field
return getDefense getLevel;
Overrides the calculateBoost method from the base class
@Override
protected double calculateBoost
Boost calculation: attack field
return double getAttack;
Builder class for AggressiveWarrior
public static class Builder extends Warrior.Builder
public Builderint level
superlevel;
Initial attack and defense values for AggressiveWarrior
this.attackdefense;
@Override
public AggressiveWarrior build
return new AggressiveWarriorthis;
public class DefensiveWarrior extends Warrior
Constructor is private to enforce Builder pattern
private DefensiveWarriorBuilder builder
superbuilder;
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 level field
return getDefense getLevel;
Overrides the calculateBoost method from the base class
@Override
protected double calculateBoost
Boost calculation: defense field
return double getDefense;
Builder class for DefensiveWarrior
public static class Build
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started