Skip to content

Commit

Permalink
Merge pull request ochococo#69 from ennioma/master
Browse files Browse the repository at this point in the history
Tried to make the Mediator pattern more generic
  • Loading branch information
ochococo authored Jul 5, 2017
2 parents a2c9004 + e1f63cb commit b6e4877
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 39 deletions.
Binary file modified Design-Patterns.playground.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -311,41 +311,50 @@ The mediator pattern is used to reduce coupling between classes that communicate

### Example
*/
struct Programmer {
protocol Receiver {
associatedtype MessageType
func receive(message: MessageType)
}

let name: String
protocol Sender {
associatedtype MessageType
associatedtype ReceiverType: Receiver

var recipients: [ReceiverType] { get }

func send(message: MessageType)
}

struct Programmer: Receiver {
let name: String

init(name: String) {
self.name = name
}

func receive(message: String) {
print("\(name) received: \(message)")
}
}

protocol MessageSending {
func send(message: String)
}

final class MessageMediator: MessageSending {

private var recipients: [Programmer] = []

final class MessageMediator: Sender {
internal var recipients: [Programmer] = []

func add(recipient: Programmer) {
recipients.append(recipient)
}

func send(message: String) {
for recipient in recipients {
recipient.receive(message: message)
}
}
}

/*:
### Usage
*/
func spamMonster(message: String, worker: MessageSending) {
func spamMonster(message: String, worker: MessageMediator) {
worker.send(message: message)
}

Expand Down
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,44 +365,53 @@ The mediator pattern is used to reduce coupling between classes that communicate

```swift

struct Programmer {
protocol Receiver {
associatedtype MessageType
func receive(message: MessageType)
}

let name: String
protocol Sender {
associatedtype MessageType
associatedtype ReceiverType: Receiver

var recipients: [ReceiverType] { get }

func send(message: MessageType)
}

struct Programmer: Receiver {
let name: String

init(name: String) {
self.name = name
}

func receive(message: String) {
print("\(name) received: \(message)")
}
}

protocol MessageSending {
func send(message: String)
}

final class MessageMediator: MessageSending {

private var recipients: [Programmer] = []

final class MessageMediator: Sender {
internal var recipients: [Programmer] = []

func add(recipient: Programmer) {
recipients.append(recipient)
}

func send(message: String) {
for recipient in recipients {
recipient.receive(message: message)
}
}
}

```

### Usage

```swift

func spamMonster(message: String, worker: MessageSending) {
func spamMonster(message: String, worker: MessageMediator) {
worker.send(message: message)
}

Expand Down
35 changes: 22 additions & 13 deletions source/behavioral/mediator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,50 @@ The mediator pattern is used to reduce coupling between classes that communicate

### Example
*/
struct Programmer {
protocol Receiver {
associatedtype MessageType
func receive(message: MessageType)
}

let name: String
protocol Sender {
associatedtype MessageType
associatedtype ReceiverType: Receiver

var recipients: [ReceiverType] { get }

func send(message: MessageType)
}

struct Programmer: Receiver {
let name: String

init(name: String) {
self.name = name
}

func receive(message: String) {
print("\(name) received: \(message)")
}
}

protocol MessageSending {
func send(message: String)
}

final class MessageMediator: MessageSending {

private var recipients: [Programmer] = []

final class MessageMediator: Sender {
internal var recipients: [Programmer] = []

func add(recipient: Programmer) {
recipients.append(recipient)
}

func send(message: String) {
for recipient in recipients {
recipient.receive(message: message)
}
}
}

/*:
### Usage
*/
func spamMonster(message: String, worker: MessageSending) {
func spamMonster(message: String, worker: MessageMediator) {
worker.send(message: message)
}

Expand Down

0 comments on commit b6e4877

Please sign in to comment.