Skip to content
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
65 changes: 40 additions & 25 deletions Behavioral/ChainOfResponsibility/chain_of_responsibility.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,67 @@
// Package chain_of_responsibility is an example of the Chain Of Responsibility Pattern.
//
// In the following example, we are processing requests through a chain of handlers.
// Each handler either handles the request or passes it to the next handler in the chain.
//
// This usage example demonstrates:
// 1. Creating a chain of handlers (A -> B -> C)
// 2. Each handler processes messages with a specific value (1, 2, or 3)
// 3. Requests propagate through the chain until handled
//
// The example is meant to show how requests travel through the chain
// and how each handler decides to process or forward the request.
package chain_of_responsibility

// Handler provides a handler interface.
// Handler defines the interface for processing requests in the chain.
type Handler interface {
// SendRequest processes a message or forwards it to the next handler.
SendRequest(message int) string
}

// ConcreteHandlerA implements handler "A".
type ConcreteHandlerA struct {
// HandlerA handles messages with value 1.
type HandlerA struct {
next Handler
}

// SendRequest implementation.
func (h *ConcreteHandlerA) SendRequest(message int) (result string) {
// SendRequest processes message 1 or forwards to the next handler.
func (h *HandlerA) SendRequest(message int) string {
if message == 1 {
result = "Im handler 1"
} else if h.next != nil {
result = h.next.SendRequest(message)
return "Handler A processed message 1"
}
return
if h.next != nil {
return h.next.SendRequest(message)
}
return ""
}

// ConcreteHandlerB implements handler "B".
type ConcreteHandlerB struct {
// HandlerB handles messages with value 2.
type HandlerB struct {
next Handler
}

// SendRequest implementation.
func (h *ConcreteHandlerB) SendRequest(message int) (result string) {
// SendRequest processes message 2 or forwards to the next handler.
func (h *HandlerB) SendRequest(message int) string {
if message == 2 {
result = "Im handler 2"
} else if h.next != nil {
result = h.next.SendRequest(message)
return "Handler B processed message 2"
}
if h.next != nil {
return h.next.SendRequest(message)
}
return
return ""
}

// ConcreteHandlerC implements handler "C".
type ConcreteHandlerC struct {
// HandlerC handles messages with value 3.
type HandlerC struct {
next Handler
}

// SendRequest implementation.
func (h *ConcreteHandlerC) SendRequest(message int) (result string) {
// SendRequest processes message 3 or forwards to the next handler.
func (h *HandlerC) SendRequest(message int) string {
if message == 3 {
result = "Im handler 3"
} else if h.next != nil {
result = h.next.SendRequest(message)
return "Handler C processed message 3"
}
if h.next != nil {
return h.next.SendRequest(message)
}
return
return ""
}
32 changes: 23 additions & 9 deletions Behavioral/Command/command.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
// Package command is an example of the Command Pattern.
//
// In the following example, we are implementing a simple toggle system.
// Commands encapsulate requests as objects, allowing parameterization
// and queueing of operations.
//
// This usage example demonstrates:
// 1. Command interface for executing operations
// 2. Concrete commands (ToggleOn, ToggleOff) that call receiver methods
// 3. Receiver that performs the actual work
// 4. Invoker that stores and executes commands
//
// The example is meant to show how commands decouple the sender (Invoker)
// from the receiver, and how multiple commands can be queued and executed.
package command

// Command provides a command interface.
Expand All @@ -11,7 +24,7 @@ type ToggleOnCommand struct {
receiver *Receiver
}

// Execute command.
// Execute calls ToggleOn on the receiver.
func (c *ToggleOnCommand) Execute() string {
return c.receiver.ToggleOn()
}
Expand All @@ -21,43 +34,44 @@ type ToggleOffCommand struct {
receiver *Receiver
}

// Execute command.
// Execute calls ToggleOff on the receiver.
func (c *ToggleOffCommand) Execute() string {
return c.receiver.ToggleOff()
}

// Receiver implementation.
// Receiver performs the actual work when commands are executed.
type Receiver struct {
}

// ToggleOn implementation.
// ToggleOn returns string indicating device turned on.
func (r *Receiver) ToggleOn() string {
return "Toggle On"
}

// ToggleOff implementation.
// ToggleOff returns string indicating device turned off.
func (r *Receiver) ToggleOff() string {
return "Toggle Off"
}

// Invoker implementation.
// Invoker stores and executes commands.
// It acts as the sender that triggers command execution.
type Invoker struct {
commands []Command
}

// StoreCommand adds command.
// StoreCommand adds a command to the execution queue.
func (i *Invoker) StoreCommand(command Command) {
i.commands = append(i.commands, command)
}

// UnStoreCommand removes command.
// UnStoreCommand removes the last command from the queue.
func (i *Invoker) UnStoreCommand() {
if len(i.commands) != 0 {
i.commands = i.commands[:len(i.commands)-1]
}
}

// Execute all commands.
// Execute runs all stored commands in order.
func (i *Invoker) Execute() string {
var result string
for _, command := range i.commands {
Expand Down
53 changes: 33 additions & 20 deletions Behavioral/Iterator/iterator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
// Package iterator is an example of the Iterator Pattern.
//
// In the following example, we are implementing a book shelf that can be
// traversed with an iterator. The iterator provides a way to access elements
// of a collection sequentially without exposing the underlying structure.
//
// This usage example demonstrates:
// 1. Iterator interface with navigation methods (Next, Prev, Reset, End)
// 2. Aggregate interface for creating iterators
// 3. Concrete collection (BookShelf) that stores books
// 4. Concrete iterator (BookIterator) that traverses the collection
//
// The example is meant to show how clients can iterate over a collection
// without knowing its internal representation.
package iterator

// Iterator provides a iterator interface.
// Iterator provides an interface for traversing a collection.
type Iterator interface {
Index() int
Value() interface{}
Expand All @@ -12,80 +25,80 @@ type Iterator interface {
End()
}

// Aggregate provides a collection interface.
// Aggregate provides an interface for creating an iterator.
type Aggregate interface {
Iterator() Iterator
}

// BookIterator implements the Iterator interface.
// BookIterator implements the Iterator interface for BookShelf.
type BookIterator struct {
shelf *BookShelf
index int
internal int
index int // current position for external access
internal int // internal position for navigation
}

// Index returns current index
// Index returns the current position in the collection.
func (i *BookIterator) Index() int {
return i.index
}

// Value returns current value
// Value returns the current book at the iterator position.
func (i *BookIterator) Value() interface{} {
if i.index < 0 || i.index >= len(i.shelf.Books) {
return nil
}
return i.shelf.Books[i.index]
}

// Has implementation.
// Has checks if the current internal position is valid.
func (i *BookIterator) Has() bool {
if i.internal < 0 || i.internal >= len(i.shelf.Books) {
return false
}
return true
return i.internal >= 0 && i.internal < len(i.shelf.Books)
}

// Next goes to the next item.
// Next moves the iterator forward by one position.
func (i *BookIterator) Next() {
i.internal++
if i.Has() {
i.index++
}
}

// Prev goes to the previous item.
// Prev moves the iterator backward by one position.
func (i *BookIterator) Prev() {
i.internal--
if i.Has() {
i.index--
}
}

// Reset resets iterator.
// Reset positions the iterator at the beginning.
func (i *BookIterator) Reset() {
i.index = 0
i.internal = 0
}

// End goes to the last item.
// End positions the iterator at the last element.
func (i *BookIterator) End() {
i.index = len(i.shelf.Books) - 1
i.internal = i.index
}

// BookShelf implements the Aggregate interface.
// BookShelf implements the Aggregate interface and stores books.
type BookShelf struct {
Books []*Book
}

// Iterator creates and returns the iterator over the collection.
// Iterator creates a new iterator for the book shelf.
func (b *BookShelf) Iterator() Iterator {
return &BookIterator{shelf: b}
}

// Add adds an item to the collection.
// Add appends a book to the collection.
func (b *BookShelf) Add(book *Book) {
b.Books = append(b.Books, book)
}

// Book implements a item of the collection.
// Book represents a single book in the collection.
type Book struct {
Name string
}
Loading