Advertisement
STANAANDREY

open closed ex

Sep 17th, 2023
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1.  
  2. class Footballer {
  3.     String name;
  4.     Role role;
  5.  
  6.     public Footballer(String name, Role role) {
  7.         this.name = name;
  8.         this.role = role;
  9.     }
  10.  
  11.     void executeMove() {
  12.         this.role.executeMove();
  13.     }
  14. }
  15.  
  16. interface Role {
  17.     void executeMove();
  18. }
  19.  
  20. class Gatekeeper implements Role {
  21.     @Override
  22.     public void executeMove() {
  23.         System.out.println("dive");
  24.     }
  25. }
  26.  
  27. class Defender implements Role {
  28.     @Override
  29.     public void executeMove() {
  30.         System.out.println("dispossess");
  31.     }
  32. }
  33.  
  34. class Midfielder implements Role {
  35.     @Override
  36.     public void executeMove() {
  37.         System.out.println("pass");
  38.     }
  39. }
  40.  
  41. class Striker implements Role {
  42.     @Override
  43.     public void executeMove() {
  44.         System.out.println("shoot");
  45.     }
  46. }
  47.  
  48. public class Main {
  49.  
  50.     public static void main(String[] args) {
  51.         Footballer f1 = new Footballer("Tatarusanu", new Gatekeeper());
  52.         Footballer f2 = new Footballer("Ronaldo", new Striker());
  53.         f1.executeMove();
  54.         f2.executeMove();
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement