- Notifications
You must be signed in to change notification settings - Fork86
Rust examples for all 23 classic GoF design patterns, and even a little more
License
fadeevab/design-patterns-rust
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This repository containsRust 🦀 examples forall 23 classic GoF designpatterns, and even a little more.
All examples are designed to introducepractical applicability in theRust language. There areconceptual andreal-world examples.In both cases, Rust idiomatic ways of code development and all the specificsare taken into account.
The repository is developed to be a part of theRefactoring.Guru project.
These examples have been tested with astablerustc 1.82
(2021 edition).
All examples can be launched via the command line, usingcargo
as follows:
cargo run --bin adapter
Each target name can be found inCargo.toml
of each example:
[[bin]]name ="adapter"path ="main.rs"
Also, the examples contain aREADME.md with instructions and additional explanations.
cargo run --bin chain-of-responsibilitycargo run --bincommandcargo run --bin iteratorcargo run --bin mediator-top-downcargo run --bin mediator-rc-refcellcargo run --bin mementocargo run --bin memento-serdecargo run --bin observercargo run --bin statecargo run --bin strategycargo run --bin strategy-funccargo run --bin template-methodcargo run --bin visitorcargo run --bin abstract-factorycargo run --bin abstract-factory-dyncargo run --bin buildercargo run --bin factory-method-maze-gamecargo run --bin factory-method-render-dialogcargo run --bin prototypecargo run --bin simple-factorycargo run --bin singleton-localcargo run --bin singleton-lazycargo run --bin singleton-mutexcargo run --bin singleton-oncecargo run --bin singleton-loggercargo run --bin static-creation-methodcargo run --bin adaptercargo run --bin bridgecargo run --bin compositecargo run --bin decoratorcargo run --bin facadecargo run --bin flyweightcargo run --bin proxy
Some examples have visual output.
Flyweight | State | Command |
---|---|---|
![]() | ![]() |
Interestingly, in Rust:
- Almost allstructural andcreational patterns can be implementedusing generics, hence,static dispatch.
- Mostbehavioral patterns can NOT be implemented using static dispatch,instead, they can be implemented only viadynamic dispatch.
A well-thought pattern classification fits the Rust language design perfectlyas "behavior" is dynamic in nature and "structure" is static.
Some patterns are really easy to implement in Rust, mostlycreational ones, e.g.Prototype,Static Creation Method.
TheMediatorbehavioral patternis the hardest to implement with Rust, considering Rust's specific ownershipmodel with strict borrow checker rules.
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
Authors: Alexander Fadeev (@fadeevab).
About
Rust examples for all 23 classic GoF design patterns, and even a little more
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.