Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

Account for intercompany transactions of a consolidated group.

Answered: 1 week ago

Question

=+can you write alternative statements that are better?

Answered: 1 week ago