mercredi 26 août 2020

What Design Principle is Violated, and Which Design Pattern can Fix It

In one recent exam, I was asked 2 questions regarding the code snippet below... The questions are as follows

  1. Identify the design principle violated by the code snippet

  2. Describe the design pattern that solves the design principle violated.

  3. Provide the UML class diagram of the design pattern described in (2) above (Optional)

  public class AI{
     public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        String choice = input.nextLine();
        
        if(choice.equals("some text"){
           // do something
        }
        else if(choice.equals("another text"){
           // do something
        }
        else if(choice.equals("extra text"){
           // do something
        }
        ...
        else{
           // do default
        }

     }
  }

Aucun commentaire:

Enregistrer un commentaire