Question: There is a key design patterns that are used in the code above. State the pattern and describe which classes play which roles in the

There is a key design patterns that are used in the code above. State the pattern and describe which classes play which roles in the pattern. Also Draw a class diagram depicting the classes and their relationships.
Beef.java
public class Beef implements Protein {
public String add(){
return "Protein added is Beef";
}
}
BeefDish.java
public class BeefDish extends Food {
public BeefDish(Protein protein){
super(protein);
}
@Override
public String cook(){
return "BeefDish cooked. "+ protein.add();
}
}
Chicken.java
public class Chicken implements Protein {
public String add(){
return "Protein added is Chicken";
}
}
ChickenDish.java
public class ChickenDish extends Food {
public ChickenDish(Protein protein){
super(protein);
}
@Override
public String cook(){
return "ChickenDish cooked. "+ protein.add();
}
}
Food.java
public abstract class Food {
protected Protein protein;
//standard constructors
abstract public String cook();
public Food(Protein protein){
this.protein = protein;
}
}
Protein.java
public interface Protein {
String add();
}
Test.java
public class Test {
public static void main(String[] args){
//a dish with beef
Food dish1= new BeefDish(new Beef());
System.out.println(dish1.cook());
//a dish with protein
Food dish2= new ChickenDish(new Chicken());
System.out.println(dish2.cook());
}
}

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 Programming Questions!