-
Notifications
You must be signed in to change notification settings - Fork 3
/
Adapter.ps1
37 lines (29 loc) · 825 Bytes
/
Adapter.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Radio {
[string] Hear() {
return "$this connect to a radio channel"
}
}
class TV {
[string] View() {
return "$this is view a TV show about music"
}
}
class Phone {
[string] Play() {
return "$this play music from internet"
}
}
class Adapter {
[object] static Adapt($obj, $alias, $referenced) {
# Should use Invoke-Expression, because first part of pipeline don't works alone
return ("`$obj | Add-Member -PassThru -Force -MemberType ScriptMethod -Name $alias -Value {`$this.$referenced}") | Invoke-Expression
}
}
# Main code
$targetObjects = @()
$targetObjects+= [Radio]::new()
$o=[TV]::new()
$targetObjects+=[Adapter]::Adapt($o, "Hear", "View()")
$o=[Phone]::new()
$targetObjects+=[Adapter]::Adapt($o, "Hear", "Play()")
$targetObjects.Hear()