Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions