Skip to content

Change Rust source files to includes #7

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

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions docs/_includes/patterns
124 changes: 1 addition & 123 deletions docs/_posts/2013-06-11-abstract-factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,129 +78,7 @@ factory works the same way. In programmer terminology, each factory has the same
View [abstract_factory.rs][github] on GitHub

{% highlight rust %}

/*
* Abstract Factory Design Pattern
* http://joshldavis.com/rust-design-patterns/abstract-factory/
*/

/*
* Core Trait that defines a Phone
*/
trait Phone {
fn call(&self);
}

/*
* Core Trait that defines a Tablet
*/
trait Tablet {
fn play_games(&self);
}

/*
* Core Trait that defines a Factory
*/
trait Factory<P: Phone, T: Tablet> {
fn new_phone(&self) -> P;
fn new_tablet(&self) -> T;
}


/*
* Define our Apple products. Normally these structs would contain a lot more
* data.
*/
struct iPhone;

impl Phone for iPhone {
fn call(&self) {
println("Look! I'm calling on an iPhone!");
}
}

struct iPad;

impl Tablet for iPad {
fn play_games(&self) {
println("Just playing some games on my iPad.");
}
}

/*
* Create AppleFactory and implement it for our Apple devices
*/
struct AppleFactory;

impl Factory<iPhone, iPad> for AppleFactory {
fn new_phone(&self) -> iPhone {
return iPhone;
}

fn new_tablet(&self) -> iPad {
return iPad;
}
}

/*
* Define our Google products. Like with Apple's products, these are
* simplified.
*/

struct Nexus4;

impl Phone for Nexus4 {
fn call(&self) {
println("Look! I'm calling on a Nexus 4!");
}
}

struct Nexus10;

impl Tablet for Nexus10 {
fn play_games(&self) {
println("Just playing some games on my Nexus 10.");
}
}

/*
* Create GoogleFactory and implement it for our Google devices
*/
struct GoogleFactory;

impl Factory<Nexus4, Nexus10> for GoogleFactory {
fn new_phone(&self) -> Nexus4 {
return Nexus4;
}

fn new_tablet(&self) -> Nexus10 {
return Nexus10;
}
}


fn main() {
// Create our two different factories
let apple = AppleFactory;
let google = GoogleFactory;

// Both factories use the same interface, so let's just use them

// Test out creating phones
let phone = apple.new_phone();
phone.call();

let phone = google.new_phone();
phone.call();

// Test out creating tablets
let tablet = apple.new_tablet();
tablet.play_games();

let tablet = google.new_tablet();
tablet.play_games();
}

{% include patterns/abstract_factory.rs %}
{% endhighlight %}

[github]: https://github.com/jdavis/rust-design-patterns/blob/master/patterns/abstract_factory.rs
149 changes: 1 addition & 148 deletions docs/_posts/2013-06-11-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,154 +92,7 @@ and wide.
View [adapter.rs][github] on GitHub

{% highlight rust %}

/*
* Core Trait that defines a basic Rocket Ship
*/
trait RocketShip {
fn turn_on(&self);
fn turn_off(&self);
fn blast_off(&self);
fn fly(&self);
}

/*
* Basic struct for a NASA Ship
*/
struct NASAShip;

/*
* Implement RocketShip trait to add functionality to NASAShip
*/
impl RocketShip for NASAShip {
fn turn_on(&self) {
println("NASA Ship is turning on.")
}

fn turn_off(&self) {
println("NASA Ship is turning off.")
}

fn blast_off(&self) {
println("NASA Ship is blasting off.")
}

fn fly(&self) {
println("NASA Ship is flying away.")
}
}

/*
* Uh oh, here is our problem. It's the amazingly advanced SpaceX ship that our
* astronaut doesn't know how to pilot.
*/
trait SpaceXShip {
fn ignition(&self);
fn on(&self);
fn off(&self);
fn launch(&self);
fn fly(&self);
}

/*
* Basic struct for a SpaceX Dragon rocket ship
*/
struct SpaceXDragon;

/*
* Implement the SpaceX trait to add functionality to the Space X Dragon
*/
impl SpaceXShip for SpaceXDragon {
fn ignition(&self) {
println("Turning Dragon's ignition.")
}

fn on(&self) {
println("Turning on the Dragon.")
}

fn off(&self) {
println("Turing off the Dragon.")
}

fn launch(&self) {
println("Launching the Dragon")
}

fn fly(&self) {
println("The Dragon is flying away.")
}
}

/*
* Uh oh, the new SpaceXDragon doesn't implement the RocketShip interface. We
* need to create an adapter that does.
*/

/*
* Adapter to adapt anything that implements SpaceXShip to the RocketShip trait
*/
struct SpaceXAdapter {
ship: SpaceXDragon
}

/*
* SpaceX Adapter that adds RocketShip traits to any SpaceXShip
*/
impl RocketShip for SpaceXAdapter {
fn turn_on(&self) {
self.ship.ignition();
self.ship.on();
}

fn turn_off(&self) {
self.ship.off();
}

fn blast_off(&self) {
self.ship.launch();
}

fn fly(&self) {
self.ship.fly();
}
}

/*
* Basic function to pilot ships that implement the RocketShip trait
*/
fn pilot<S: RocketShip>(ship: &S) {
ship.turn_on();
ship.blast_off();
ship.fly();
ship.turn_off();
print("\n");
}

fn main() {
// Create a new NASAShip
let saturn5 = NASAShip;

// Let's fly our NASAShip
println("Piloting the Saturn 5.");
pilot(&saturn5);

// Create a Dragon
let dragon = SpaceXDragon;

// Uh oh, our pilot function doesn't recognize this ship...
// pilot(&dragon); <-- Gives a compile time error.

// Let's Adapt our SpaceXDragon ship
let dragon_adapter = SpaceXAdapter {
ship: dragon
};

// Now we can pilot the Dragon!
println("Piloting the Dragon Adapter.");
pilot(&dragon_adapter);
}

{% include patterns/adapter.rs %}
{% endhighlight %}

[github]: https://github.com/jdavis/rust-design-patterns/blob/master/patterns/adapter.rs