Τρίτη, 14 Ιουλίου 2020

Mediator Pattern (OOP)

The idea here is to have objects inside Mediator which communicate with Mediator, using events. ConcreteMediator has all the logic to work with Colleagues objects. We pass a special function, which called like it is code of module. The function AnyName when executed, has the name of module, so can see anything and change it too. We have to use Local if we want a variable to shadow any variable with same name, global or local. We can't use names which are used for static variables in same module.

Events have signatures to obey. We can attach two or more functions in an Event. All of them have to read the same type of parameters. These events use object types events. M2000 also have light events, which are controlled by named groups, defined WithEvents, and always call one function (with no signature like the objects events). We use one light event in Mediator group.




class Colleague {
      module takethis (ev1, ev2) {
            error "not implemented yet"
      }
}
class ConcreteColleague as Colleague {
      x=2000
      module takethis (ev1, ev2){
            call event ev1, 100+.x
            call event ev2, &.x
      }
}
class Mediator {
      module mediate {
            error "not implemented yet"
      }
}
\\ this ConcreteMediator works with Colleague objects
class ConcreteMediator as Mediator {
      event "finished"
      myacc=0
      module work1 (&that as Colleague) {
            that.takethis .one, .two
      }
      event one {
            read x
      }
      event two {
            read &x
      }
      Function do1(New n) {
            Print "n=";n
            .myacc+=n
      }
      Function do2(New &n) {
            n+=20000
      }
      module Mediate (&C as Colleague) {
            do
                  .work1 &C
            Until .myacc>=2400
            call event "finished"
      }
class:
      Module ConcreteMediator (&m()) {
            event .one new &.do1(), m()
            event .two new &.do2()
      }
}
acc=0
Function AnyName {
      Read New k
      print "from AnyName:", k
      acc+=k
}
concreteColleague=ConcreteColleague()
Group WithEvents Mediator=ConcreteMediator(Lazy$(&Anyname()))
Function Mediator_finished {
      Print "Finished ", acc
}
Mediator.Mediate &concreteColleague
Print Mediator.myacc=24200
Print acc=24200


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου