Question: a ) Identify the pattern present in the code and name the roles of each class in the code. b ) Draw a class diagram

a) Identify the pattern present in the code and name the roles of each class in the code. b) Draw a class diagram connecting the classes. c) Briefly describe two scenarios when you want to use this design pattern.
AA.java
public interface AA {
void foo1(SO so);
void foo2(SE se);
void foo3(CO co);
}
App.java
public class App {
public static void main(String[] args){
UN co = new CO(
new SE(new SO(), new SO(), new SO()),
new SE(new SO(), new SO(), new SO())
);
co.xx(new BB());
co.xx(new DD());
co.xx(new CC());
}
}
BB.java
public class BB implements AA {
@Override
public void foo1(SO so){
System.out.println("Greetings so");
}
@Override
public void foo2(SE se){
// Do nothing
}
@Override
public void foo3(CO co){
// Do nothing
}
}
CC.java
@Override
public void foo1(SO so){
// Do nothing
}
@Override
public void foo2(SE se){
// Do nothing
}
@Override
public void foo3(CO co){
System.out.println("Good to see you co");
}
}
CO.java
public class CO extends UN {
public CO(UN... children){
super(children);
}
@Override
public void xx(AA aa){
aa.foo3(this);
super.xx(aa);
}
@Override
public String toString(){
return "commander";
}
}
DD.java
public class DD implements AA {
@Override
public void foo1(SO so){
// Do nothing
}
@Override
public void foo2(SE se){
System.out.println("Hello se");
}
@Override
public void foo3(CO co){
// Do nothing
}
}
SE.java
public class SE extends UN {
public SE(UN... children){
super(children);
}
@Override
public void xx(AA aa){
aa.foo2(this);
super.xx(aa);
}
@Override
public String toString(){
return "sergeant";
}
}
SO.java
public class SO extends UN {
public SO(UN... children){
super(children);
}
@Override
public void xx(AA aa){
aa.foo1(this);
super.xx(aa);
}
@Override
public String toString(){
return "soldier";
}
}
UN.java
import java.util.Arrays;
public abstract class UN {
private UN[] children;
public UN(UN... children){
this.children = children;
}
public void xx(AA aa){
Arrays.stream(children).forEach(child -> child.xx(aa));
}
}

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!