S/O - Proxy
- Category: structural
- Type: object
- Motivation: a proxy is a wrapper to forward the real object request and/or provide additional logic
do {
let proxy = ProxyInternet()
proxy.connect("hello.com")
proxy.connect("foo.com")
}
protocol Internet
{
func connect(_ host: String)
}
class RealInternet: Internet
{
func connect(_ host: String)
{
print("conecting to \(host)")
}
}
class ProxyInternet: Internet
{
let internet = RealInternet()
func connect(_ host: String)
{
if ["foo.com"].contains(host) { // the work of proxy
print("Access Denied")
return
}
internet.connect(host)
}
}