design-patterns-rust

repository·main·Indexed 21 days ago

https://github.com/fadeevab/design-patterns-rust

A collection of practical Rust implementations for all 23 classic Gang of Four (GoF) design patterns. The project demonstrates idiomatic Rust development, covering behavioral patterns such as Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, and State, with specific examples on handling ownership, dynamic dispatch with Box, and runtime mutability using Rc<RefCell<..>>.

Tokens
27.1K
Snippets
107
Records
135
Agent score
74%

What's inside design-patterns-rust

  1. Explore Creational Design Patterns

    main

    This repository contains implementations of Creational design patterns in Rust. Creational patterns focus on object creation mechanisms to increase code flexibility and reuse.

    Each pattern implementation is contained within its own directory and includes a dedicated README.md with specific instructions, usage examples, and implementation details.

  2. Explore Structural Design Patterns

    main

    The structural/ directory contains implementations of structural design patterns in Rust. These patterns focus on how to assemble objects and classes into larger, flexible, and efficient structures.

    Each pattern implementation is self-contained and includes its own README.md with specific instructions, usage examples, and implementation details. To use a specific pattern, navigate to its respective subdirectory.

  3. Understand the Singleton pattern via the Logger example

    main

    This example demonstrates how the Rust logging subsystem is implemented using the Singleton pattern. It consists of four main components:

    • log.rs: A lightweight logging facade that encapsulates a static logger object (the Singleton).
    • simple_logger.rs: A concrete logger implementation that prints to stdout.
    • app.rs: Example application code that interacts with the logging facade.
    • main.rs: The entry point responsible for application initialization and setting up the logger.
  4. Explore Behavioral Design Patterns

    main

    This repository provides implementations of Behavioral design patterns in Rust. Behavioral patterns focus on algorithms and the assignment of responsibilities between objects.

    To use a specific pattern, navigate to its individual directory. Each pattern implementation is self-contained and includes its own README.md with specific instructions, usage examples, and implementation details.

  5. What is the Observer pattern in Rust?

    main

    The Observer is a behavioral design pattern that allows objects (publishers) to notify other objects (subscribers) about changes in their state.

    In this Rust implementation, subscribers are defined as callable objects. You can use:

    • Lambda functions (closures).
    • Explicit functions.

    Explicit function objects can be unsubscribed from the event publisher, though limitations may apply depending on the specific function type used.

  6. What is the Flyweight pattern

    main
    The Flyweight (Cache) is a structural design pattern used to support vast quantities of objects while keeping memory consumption low. It works by using an internal cache (hidden behind a Facade-like API) that stores shared parts of objects. Instead of every object containing all its data, multiple objects reference a single shared instance of the common parts.
  7. What is the Proxy design pattern?

    main

    The Proxy is a structural design pattern that provides an object acting as a substitute for a real service object. The proxy receives client requests and performs additional work—such as access control or caching—before passing the request to the actual service object.

    Conceptual Model: Nginx Proxy

    A common real-world example is a web server like Nginx acting as a proxy for an application server. It provides:

    • Controlled access to the application server.
    • Rate limiting.
    • Request caching.

    Expected Execution Output

    When running the example, you will see simulated HTTP requests and responses demonstrating access control (e.g., 403 Not Allowed) and successful service calls:

    Url: /app/status
    HttpCode: 200
    Body: Ok
    
    Url: /app/status
    HttpCode: 200
    Body: Ok
    
    Url: /app/status
    HttpCode: 403
    Body: Not Allowed
    
    Url: /create/user
    HttpCode: 201
    Body: User Created
    
    Url: /create/user
    HttpCode: 404
    Body: Not Ok
  8. What is the Singleton pattern in Rust

    main

    The Singleton pattern ensures that only one instance of an object exists and provides a global access point to it.

    In Rust, a Singleton is typically a static mut item, which requires unsafe blocks for reading or writing. Because of this, it is recommended to wrap the singleton in a safe API that hides the unsafe implementation details.

    Commonly used tools to implement safe Singletons include:

    • once_cell::sync::OnceCell
    • lazy_static::lazy_static
    • std::sync::Mutex
  9. What is the Abstract Factory pattern

    main

    The Abstract Factory pattern solves the problem of creating entire families of related products without specifying their concrete classes.

    In this implementation, a GUI framework is organized into three layers:

    1. A gui library defining interfaces (traits) for all components.
    2. Platform-specific libraries (e.g., windows-gui, macos-gui) providing concrete implementations of those interfaces.
    3. A client app that uses the abstract interfaces to interact with GUI elements, ensuring the application code remains decoupled from specific platform types.