You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A delegate is a type that holds references to methods or functions with a particular signature of parameters and return type, and any method or function compatible with the delegate signature can be associated with the instance of that delegate and can be called by invoking the delegate instance.
12
+
</p>
13
+
<h3>Defining the delegate</h3>
14
+
<p>
15
+
A delegate is constructed by extending the abstract class Delegate, defining the method signature with an empty body, and having the same name as the name of the defined delegate class, as shown in the code example below.
16
+
</p>
17
+
<pre><codeclass="language-php"><?php
18
+
19
+
namespace Application;
20
+
21
+
use DevNet\System\Delegate;
22
+
23
+
class MyDelegate extends Delegate
24
+
{
25
+
// Define the delegate signature.
26
+
public function myDelegate(array $args): string
27
+
{
28
+
// The body is left empty.
29
+
}
30
+
}
31
+
</code></pre>
32
+
<br>
33
+
<h3>Defining the handler</h3>
34
+
<p>
35
+
The following code example represents a handler class with two methods that will be associated with the delegate introduced in the previous example, and they must have the same signature of that delegate.
36
+
</p>
37
+
<pre><codeclass="language-php"><?php
38
+
39
+
namespace Application;
40
+
41
+
class MyHandler
42
+
{
43
+
// Define a method with the same signature as the delegate.
44
+
public function handle(array $args): string
45
+
{
46
+
return serialize($args);
47
+
}
48
+
}
49
+
</code></pre>
50
+
<br>
51
+
<h3>Using the delegate</h3>
52
+
<p>
53
+
When the delegate is instantiated, it can encapsulate any function or method with a compatible signature, and when the call is made to the delegate instance, it will be passed to the associated function, passing along the same arguments and returning the same value.
54
+
</p>
55
+
<pre><codeclass="language-php"><?php
56
+
57
+
// Register the method 'handle' of the Handler class
58
+
$myAction = new MyDelegate([new MyHandler(), 'handle']);
59
+
60
+
// Or register in-line closure function of the same signature
61
+
$myAction = new MyDelegate(function(array $args): string {
62
+
return serialize($args);
63
+
});
64
+
65
+
$args = ['value1', 'value2'];
66
+
67
+
// Invoke the delegate instance.
68
+
$result = $myAction($args);
69
+
</code></pre>
70
+
<br>
71
+
<h4>Multicast Delegate</h4>
72
+
<p>
73
+
A multicast delegate is a delegate that holds references to multiple methods or functions, and when the delegate instance is called, it invokes all of the associated functions, and the return value is that of the last function executed, unless you invoke each function through iteration to deal with each returned value.
// Invoke the multicast delegate and store the last returned value
88
+
$result = $myAction($args);
89
+
90
+
// Invoke each function of the multicast delegate through iteration to store all the returned values
91
+
$results = [];
92
+
foreach($myAction as $action) {
93
+
$results[] = $action->invoke($args);
94
+
}
95
+
</code></pre>
96
+
<br>
97
+
<h3>Event</h3>
98
+
<p>
99
+
An event is a special type of delegate that enables an object to notify other objects when something of interest occurs, where the class that sends or raises the event is called the publisher (other names: sender, subject), and the classes that receive or handle the event are called subscribers (other names: receivers, observers, listeners, handlers).
100
+
</p>
101
+
<h5>Event Handler</h5>
102
+
<p>
103
+
DevNet provides an easy way to subscribe to the event by using <code>DevNet\System\Event\EventHandler</code>, which has the following delegate signature <code>function(object $sender, EventArgs $args): void</code>
104
+
</p>
105
+
<ul>
106
+
<li><b>$sender:</b> (<code>object</code>) represents the publisher that raises the event, and the reason for passing the sender as a parameter to the handler, is because the handler can be subscribed to multiple publishers, and sometimes we need to know which publisher raises the event.</li>
107
+
<li><b>$args:</b> (<code>DevNet\System\Event\EventArgs</code>) represents the event arguments to be handled by the handler.</li>
108
+
</ul>
109
+
<p>
110
+
The example below represents a <code>Button</code> class as a publisher with two events: The <code>click</code> and <code>keypress</code> events, which can be associated with event listners.
111
+
</p>
112
+
<pre><codeclass="language-php"><?php
113
+
114
+
namespace Application\Components;
115
+
116
+
use DevNet\System\Event\EventArgs;
117
+
use DevNet\System\Event\EventHandler;
118
+
119
+
class Button
120
+
{
121
+
public string $Title;
122
+
private EventHandler $click;
123
+
private EventHandler $keypress;
124
+
125
+
public function __construct(string $title)
126
+
{
127
+
$this->Title = $title;
128
+
$this->click = new EventHandler();
129
+
$this->keypress = new EventHandler();
130
+
}
131
+
132
+
public function addListner(string $event, callable $handler): void
133
+
{
134
+
switch($event) {
135
+
case 'click':
136
+
$this->click[] = $handler;
137
+
break;
138
+
case 'keypress':
139
+
$this->keypress[] = $handler;
140
+
break;
141
+
default:
142
+
throw \Exception("The event {$event} does not exist!");
143
+
}
144
+
}
145
+
146
+
public function click(): void
147
+
{
148
+
$this->click->invoke($this, new EventArgs);
149
+
}
150
+
151
+
public function keypress(): void
152
+
{
153
+
$this->keypress->invoke($this, new EventArgs);
154
+
}
155
+
}
156
+
</code></pre>
157
+
<p>
158
+
And the following example represents a <code>Window</code> class as a subscriber that can subscribe handlers to the <code>Button</code> events.
0 commit comments