Advertisement
JustCaused

SP - Command Pattern

Dec 8th, 2022
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. package ZCOMM1;
  2.  
  3. // Задатак ZCOMM1: Додати програм на месту три тачке како би се добила порука:
  4. // Danas je lep dan.
  5.  
  6. class Client{  
  7.     Receiver rec;
  8.     Command cm;
  9.    
  10.     Client(){
  11.         rec = new Receiver();
  12.         cm = new ConcreteCommand(rec);
  13.     }
  14.    
  15.     Command getCommand(){return cm;}
  16.     public static void main(String[] args) {
  17.         Client cl = new Client();
  18.         Invoker inv = new Invoker(new ConcreteCommand(cl.rec));
  19.         inv.Execute();
  20.     }
  21. }
  22.  
  23. class Invoker{
  24.     Command com;
  25.  
  26.     Invoker(Command com1) {
  27.         com = com1;
  28.     }
  29.  
  30.     void Execute() {
  31.        com.Execute();
  32.     }
  33. }
  34.  
  35. interface Command{
  36.   void Execute();
  37. }
  38.  
  39. class ConcreteCommand implements Command{
  40.     Receiver rec;
  41.  
  42.     ConcreteCommand(Receiver rec1) {
  43.         rec = rec1;
  44.     }
  45.    
  46.     @Override
  47.     public void Execute() {
  48.         rec.Action();
  49.     }
  50. }
  51.  
  52. class Receiver{
  53.     void Action(){System.out.println("Danas je lep dan.");}
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement