Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create broadcast page and link to main Readme #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A curated collection of idiomatic design & application patterns for Go language.
| [Bridge](/structural/bridge.md) | Decouples an interface from its implementation so that the two can vary independently | ✘ |
| [Composite](/structural/composite.md) | Encapsulates and provides access to a number of different objects | ✘ |
| [Decorator](/structural/decorator.md) | Adds behavior to an object, statically or dynamically | ✔ |
| [Facade](/structural/facade.md) | Uses one type as an API to a number of others | |
| [Facade](/structural/facade.md) | Uses one type as an API to a number of others | |
| [Flyweight](/structural/flyweight.md) | Reuses existing instances of objects with similar/identical state to minimize resource usage | ✘ |
| [Proxy](/structural/proxy.md) | Provides a surrogate for an object to control it's actions | ✔ |

Expand Down Expand Up @@ -63,7 +63,7 @@ A curated collection of idiomatic design & application patterns for Go language.
|:-------:|:----------- |:------:|
| [N-Barrier](/concurrency/barrier.md) | Prevents a process from proceeding until all N processes reach to the barrier | ✘ |
| [Bounded Parallelism](/concurrency/bounded_parallelism.md) | Completes large number of independent tasks with resource limits | ✔ |
| [Broadcast](/concurrency/broadcast.md) | Transfers a message to all recipients simultaneously | |
| [Broadcast](/concurrency/broadcast.md) | Transfers a message to all recipients simultaneously | |
| [Coroutines](/concurrency/coroutine.md) | Subroutines that allow suspending and resuming execution at certain locations | ✘ |
| [Generators](/concurrency/generator.md) | Yields a sequence of values one at a time | ✔ |
| [Reactor](/concurrency/reactor.md) | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers | ✘ |
Expand Down
49 changes: 49 additions & 0 deletions concurrency/broadcast.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Broadcast Pattern

In Go, you can create a broadcast concurrency pattern using goroutines and channels. This pattern allows you to send a message to multiple recipients simultaneously.

## Implementation

```go
// Broadcast sends a message to all recipients simultaneously
func Broadcast(message string, recipients ...chan string) {
var wg sync.WaitGroup
for _, recipient := range recipients {
wg.Add(1)
go func(recipient chan string) {
defer wg.Done()
recipient <- message
}(recipient)
}
wg.Wait()
}
```

## Usage

```go
func main() {
// Create channels for recipients
recipient1 := make(chan string)
recipient2 := make(chan string)
recipient3 := make(chan string)

// Start goroutines to receive messages
go func() {
fmt.Println("Recipient 1 received:", <-recipient1)
}()
go func() {
fmt.Println("Recipient 2 received:", <-recipient2)
}()
go func() {
fmt.Println("Recipient 3 received:", <-recipient3)
}()

// Broadcast a message to all recipients
Broadcast("Hello, World!", recipient1, recipient2, recipient3)
}
```

1. The *Broadcast* function takes a message and a variable number of recipient channels.
1. It uses a *sync.WaitGroup* to wait for all goroutines to finish sending the message.
1. Each recipient channel receives the message simultaneously through a goroutine.
75 changes: 75 additions & 0 deletions structural/facade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Facade Pattern

[Façade pattern](https://en.wikipedia.org/wiki/Facade_pattern) provides a simplified interface to a complex subsystem.

## Implementation

`LogDecorate` decorates a function with the signature `func(int) int` that
manipulates integers and adds input/output logging capabilities.

```go
// Subsystem1 represents a complex subsystem
type Subsystem1 struct{}

func (s *Subsystem1) Operation1() {
fmt.Println("Subsystem1: Operation1")
}

// Subsystem2 represents another complex subsystem
type Subsystem2 struct{}

func (s *Subsystem2) Operation2() {
fmt.Println("Subsystem2: Operation2")
}

// Subsystem3 represents yet another complex subsystem
type Subsystem3 struct{}

func (s *Subsystem3) Operation3() {
fmt.Println("Subsystem3: Operation3")
}

// Facade provides a simplified interface to the complex subsystems
type Facade struct {
subsystem1 *Subsystem1
subsystem2 *Subsystem2
subsystem3 *Subsystem3
}

func NewFacade() *Facade {
return &Facade{
subsystem1: &Subsystem1{},
subsystem2: &Subsystem2{},
subsystem3: &Subsystem3{},
}
}

func (f *Facade) OperationA() {
fmt.Println("Facade: OperationA")
f.subsystem1.Operation1()
f.subsystem2.Operation2()
}

func (f *Facade) OperationB() {
fmt.Println("Facade: OperationB")
f.subsystem2.Operation2()
f.subsystem3.Operation3()
}
```

## Usage

```go
func main() {
facade := NewFacade()
facade.OperationA()
facade.OperationB()
}
```

1. _Subsystem1_, _Subsystem2_, and _Subsystem3_ represent complex subsystems with their own operations.
1. The _Facade_ struct provides a simplified interface to these subsystems.
1. The _NewFacade_ function initializes the subsystems and returns a new instance of the Facade.
1. The _Facade_ struct has methods _OperationA_ and _OperationB_ that internally call the operations of the subsystems, providing a simplified interface to the client

This way, the client code can interact with the Facade instead of dealing with the complexities of the subsystems directly