Skip to content

Commit 258eb79

Browse files
committed
Merge pull request ochococo#16 from tomkowz/patch-1
Added Virtual and Protection proxy patterns.
2 parents d5cdbed + 1079969 commit 258eb79

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

README.markdown

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,85 @@ println("Cost : \(someCoffee.getCost()); Ingredients: \(someCoffee.getIngredient
467467

468468
##🚧 Proxy
469469

470+
**Virtual Proxy**
471+
472+
**Source:**
473+
```swift
474+
protocol P {
475+
func action()
476+
}
477+
478+
class A: P {
479+
func action() {
480+
println("A: action performed")
481+
}
482+
}
483+
484+
class AProxy: P {
485+
lazy private var a: A = {
486+
return A()
487+
}()
488+
489+
func action() {
490+
println("AProxy: Some action performed in proxy.")
491+
self.a.action()
492+
}
493+
}
494+
```
495+
496+
**Usage:**
497+
```swift
498+
var p = AProxy()
499+
p.action()
500+
```
501+
502+
503+
**Protection Proxy**
504+
505+
**Source:**
506+
```swift
507+
protocol P {
508+
func action()
509+
}
510+
511+
class A: P {
512+
func action() {
513+
println("A: Action")
514+
}
515+
}
516+
517+
class AProxy: P {
518+
private var a: A!
519+
520+
func authenticate(pass: String) -> Bool {
521+
if pass != "pass" {
522+
return false
523+
}
524+
525+
a = A()
526+
return true
527+
}
528+
529+
func action() {
530+
if (a == nil) {
531+
println("Action denied")
532+
return;
533+
}
534+
535+
println("Action allowed")
536+
a.action()
537+
}
538+
}
539+
```
540+
541+
**Usage:**
542+
```swift
543+
var proxy = AProxy()
544+
proxy.authenticate("pass")
545+
proxy.action()
546+
```
547+
548+
470549
#Behavioral
471550

472551
>In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.

0 commit comments

Comments
 (0)